android Bitmap用法总结

摘要:
位图配置。ARGB_8888:位图配置。RGB_565);Canvascans=newCanvas;//帆布设置位图;可拉伸。集合边界;可拉伸。画返回位图;}此外:Resourcesresources=getResources()//Get-ResourcesDrawabledrawable=resources。getDrawable//DrawableBitmapbitmap=drawableToBitmap;iv.setImageBitmap;2.从资源中获取BitmapResourcesres=getResources();Bitmapbmp=位图工厂。解码资源;3、 位图→byte[]privatebyte[]Bitmap2Bytes{ByteArrayOutputStreambaos=newByteArray输出流();bm.compress;returnbaos.toByteArra();}4、 字节[]→BitmapprivateBitmapBytes2Bimap{if(b.length!=0){returnBitmapFactory.decodeByteArray;}否则{returnnull;}}5.保存位图staticboolensaveBitmap2file{CompressFormatformat=Bitmap.CompressFormat.JPEG;int quality=100;OutputStreamstream=null;try{stream=newFileOutputStream;}catch{//TODO自动生成catchblocke.printStackTrace();}returnbmp。压缩;}6.根据您的要求缩放图片//图片源Bitmapbm=BitmapFactory。decodeStream//获取图片的宽度和高度intwidth=bm。获取宽度();int高度=bm。获取高度();//设置所需大小intnewWidth=320;intnewHeight=480;//计算缩放比例floatscaleWidth=/width;floatscaleHeight=/高度;//获取要缩放的矩阵参数Matrixmatrix=newMatrix();矩阵后缩放;//获取新图片Bitmapnewbm=位图。createBitmap//画布。绘图位图;相关知识链接:http://www.eoeandroid.com/thread-3162-1-1.html7.位图使用摘要BitmapFactory。Optionoption=newBitmapFactory。选项();选项inSampleSize=2;//将图片设置为其原始宽度和高度的1/2,以防止内存溢出Bitmapbm=BitmapFactory。decodeFile//文件流URLurl=newURL(“”);InputStreamis=url。打开流();Bitmapbm=位图工厂。decodeStream;Android:scaleType:Android:scaleType控制如何调整/移动图像以匹配ImageView的大小。

1、Drawable→Bitmap

public staticBitmap drawableToBitmap(Drawable drawable) {

Bitmap bitmap =Bitmap

.createBitmap(

drawable.getIntrinsicWidth(),

drawable.getIntrinsicHeight(),

drawable.getOpacity() != PixelFormat.OPAQUE ?Bitmap.Config.ARGB_8888

: Bitmap.Config.RGB_565);

Canvas canvas = newCanvas(bitmap);

//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),

drawable.getIntrinsicHeight());

drawable.draw(canvas);

returnbitmap;

}

另附:

Resources resources =getResources();  //得到Resources
Drawable drawable =resources.getDrawable(R.drawable.ic_launcher);      //得到Drawable
Bitmap bitmap =drawableToBitmap(drawable);
iv.setImageBitmap(bitmap);

2、从资源中获取Bitmap

Resources res=getResources();

Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);

3、Bitmap→byte[]

private byte[] Bitmap2Bytes(Bitmap bm){

ByteArrayOutputStream baos = newByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

returnbaos.toByteArray();

}

4、byte[]→Bitmap

private Bitmap Bytes2Bimap(byte[] b){

if(b.length!=0){

return BitmapFactory.decodeByteArray(b, 0, b.length);

}

else{

return null;

}

}

5、保存bitmap

static booleansaveBitmap2file(Bitmap bmp,String filename){

CompressFormat format=Bitmap.CompressFormat.JPEG;

int quality = 100;

OutputStream stream = null;

try{

stream = new FileOutputStream("/sdcard/" +filename);

} catch(FileNotFoundException e) {

//TODO Auto-generated catch block
e.printStackTrace();

}

returnbmp.compress(format, quality, stream);

}

6、将图片按自己的要求缩放

//图片源
Bitmap bm =BitmapFactory.decodeStream(getResources()

.openRawResource(R.drawable.dog));

//获得图片的宽高

int width =bm.getWidth();

int height =bm.getHeight();

//设置想要的大小

int newWidth = 320;

int newHeight = 480;

//计算缩放比例

float scaleWidth = ((float) newWidth) /width;

float scaleHeight = ((float) newHeight) /height;

//取得想要缩放的matrix参数
Matrix matrix = newMatrix();

matrix.postScale(scaleWidth, scaleHeight);

//得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,

true);

//放在画布上
canvas.drawBitmap(newbm, 0, 0, paint);

相关知识链接:http://www.eoeandroid.com/thread-3162-1-1.html

7、bitmap的用法小结

BitmapFactory.Options option = new BitmapFactory.Options();

option.inSampleSize = 2; //将图片设为原来宽高的1/2,防止内存溢出

Bitmap bm = BitmapFactory.decodeFile("",option);//文件流

URL url = new URL("");

InputStream is = url.openStream();

Bitmap bm = BitmapFactory.decodeStream(is);

android:scaleType:

android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType /

android:scaleType值的意义区别:

CENTER /center按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分

显示

CENTER_CROP / centerCrop按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长

(宽)

CENTER_INSIDE / centerInside将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片

长/宽等于或小于View的长/宽

Generated by Foxit PDF Creator © Foxit Software

http://www.foxitsoftware.comFor evaluation only.

FIT_CENTER / fitCenter把图片按比例扩大/缩小到View的宽度,居中显示

FIT_END / fitEnd把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置

FIT_START / fitStart把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置

FIT_XY / fitXY把图片 不按比例 扩大/缩小到View的大小显示

MATRIX / matrix用矩阵来绘制,动态缩小放大图片来显示。

//放大缩小图片

public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){

int width = bitmap.getWidth();

int height = bitmap.getHeight();

Matrix matrix = new Matrix();

float scaleWidht = ((float)w / width);

float scaleHeight = ((float)h / height);

matrix.postScale(scaleWidht, scaleHeight);

Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,

true);

return newbmp;

}

//将Drawable转化为Bitmap

public static Bitmap drawableToBitmap(Drawable drawable){

int width = drawable.getIntrinsicWidth();

int height = drawable.getIntrinsicHeight();

Bitmap bitmap = Bitmap.createBitmap(width, height,

drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

: Bitmap.Config.RGB_565);

Canvas canvas = new Canvas(bitmap);

drawable.setBounds(0,0,width,height);

drawable.draw(canvas);

return bitmap;

Generated by Foxit PDF Creator © Foxit Software

http://www.foxitsoftware.comFor evaluation only.

}

//获得圆角图片的方法

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap

.getHeight(), Config.ARGB_8888);

Canvas canvas = new Canvas(output);

final int color = 0xff424242;

final Paint paint = new Paint();

final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

final RectF rectF = new RectF(rect);

paint.setAntiAlias(true);

canvas.drawARGB(0, 0, 0, 0);

paint.setColor(color);

canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

canvas.drawBitmap(bitmap, rect, rect, paint);

return output;

}

//获得带倒影的图片方法

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){

final int reflectionGap = 4;

int width = bitmap.getWidth();

int height = bitmap.getHeight();

Matrix matrix = new Matrix();

matrix.preScale(1, -1);

Bitmap reflectionImage = Bitmap.createBitmap(bitmap,

0, height/2, width, height/2, matrix, false);

Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2),

Config.ARGB_8888);

Canvas canvas = new Canvas(bitmapWithReflection);

canvas.drawBitmap(bitmap, 0, 0, null);

Paint deafalutPaint = new Paint();

Generated by Foxit PDF Creator © Foxit Software

http://www.foxitsoftware.comFor evaluation only.

canvas.drawRect(0, height,width,height + reflectionGap,

deafalutPaint);

canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

Paint paint = new Paint();

LinearGradient shader = new LinearGradient(0,

bitmap.getHeight(), 0, bitmapWithReflection.getHeight()

+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);

paint.setShader(shader);

// Set the Transfer mode to be porter duff and destination in

paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

// Draw a rectangle using the paint with our linear gradient

canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()

+ reflectionGap, paint);

return bitmapWithReflection;

}

}

免责声明:文章转载自《android Bitmap用法总结》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SQL Server 连接字符串和身份验证X240 Win10企业版 14279版本 电池标尺白底问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

软件项目技术点(7)——在canvas上绘制自定义图形

AxeSlide软件项目梳理   canvas绘图系列知识点整理 图形种类 目前我们软件可以绘制出来的形状有如下这几种,作为开发者我们一直想支持用户可以拖拽的类似word里面图形库,但目前还没有找到比较合适的库。 下图中所有的图形都是我们自己写代码在canvas上绘制方法出来的,可以改变实心/空心,改变颜色,大小宽高,线条弯曲度,透明度等。 父类sha...

[ext4]03 磁盘布局 – Flexible group分析

Flexible Block Groups (flex_bg),我称之为“弹性块组”,是EXT4文件系统引入的一个feature。 所谓Flexible Block Groups,就是将连续的多个物理block groups绑在一起组成一个逻辑块组,这个逻辑块组就称之为Flex_group(也就是flex_bg)。 在一个Flex_group中,第一个物理...

canvas遇到的一些问题

1、移动端无法全屏问题 问题描述:由于canvas的width和height只能设置px值,不支持rem单位,所以想在移动设备屏幕分辨率繁杂的情况下达到canvas铺满全屏的效果很困难。   解决方法:通过js获取到手机屏幕的clientWidth值,赋给canvas,以此来达到适配全屏的效果; 1 2 3 4 5 varclientWidth...

将某个div内容保存成图片,使用html2canvas截图方法(高清图并解决图片跨域问题)

首先附上html2canvas的CDN地址:http://www.bootcdn.cn/html2canvas/ ; 此方法可截取整个div的内容,包括不可视区域,并且可以实现跨域图片截图。之前看了很多关于html2canvas插件图片跨域的解决办法,大部分的答复是在服务器端配置,之后将useCORS属性设置为true,但是如果图片是保存在别人家的服务器上...

canvas及lufylegend引擎

随着对canvas的深入了解,发现canvas真是个好东西,也发现android真烂,哎! 最近在用canvas做小游戏,简单的跑酷类,打地鼠类的小游戏做了一遍,今天先写一个打地鼠的制作心得。 PS:以前用JS做过打地鼠的游戏,现在看看真心烂。 首先我使用的是lufylegend.js 1.9.1的版本(非常好用的引擎,强烈推荐)。 在制作过程中先来分析打地...

H5动效的常见制作方法

动效制作手法1;gif图片 优势:节省成本;制作效率高。 动效制作手法2:逐帧动画 动效制作手法3:css3; 动效制作手法4:svg; 缺点不支持低版本的浏览器ie8及以下 知识普及:SVG,可缩放矢量图形(Scalable Vector Graphics), 是被存成了 XML 格式的图像, 特征: 1,可被多种工具读取和修改(比如记事本) ,2,尺寸...