Android中Bitmap,byte[],Drawable相互转化
一、相关概念 1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象 2、Canvas画布,绘图的目的区域,用于绘图 3、Bitmap位图,用于图的处理 4、Matrix矩阵 二、Bitmap 1、从资源中获取Bitmap Resourcesres=getResources(); Bitmapbmp=BitmapFactory.decodeResource(res,R.drawable.icon); 2、Bitmap → byte[] []Bitmap2Bytes(Bitmapbm){ ByteArrayOutputStreambaos=ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG,100,baos); baos.toByteArray(); } 3、byte[] → Bitmap BitmapBytes2Bimap([]b){ (b.length!=0){ BitmapFactory.decodeByteArray(b,0,b.length); }{ ; } } 4、Bitmap缩放 BitmapzoomBitmap(Bitmapbitmap,width,height){ w=bitmap.getWidth(); h=bitmap.getHeight(); Matrixmatrix=Matrix(); scaleWidth=(()width/w); scaleHeight=(()height/h); matrix.postScale(scaleWidth,scaleHeight); Bitmapnewbmp=Bitmap.createBitmap(bitmap,0,0,w,h,matrix,); newbmp; } 5、将Drawable转化为Bitmap BitmapdrawableToBitmap(Drawabledrawable){ w=drawable.getIntrinsicWidth(); h=drawable.getIntrinsicHeight(); Bitmap.Configconfig=drawable.getOpacity()!=PixelFormat.OPAQUE?Bitmap.Config.ARGB_8888 :Bitmap.Config.RGB_565; Bitmapbitmap=Bitmap.createBitmap(w,h,config); Canvascanvas=Canvas(bitmap); drawable.setBounds(0,0,w,h); drawable.draw(canvas); bitmap; } 6、获得圆角图片 BitmapgetRoundedCornerBitmap(Bitmapbitmap,roundPx){ w=bitmap.getWidth(); h=bitmap.getHeight(); Bitmapoutput=Bitmap.createBitmap(w,h,Config.ARGB_8888); Canvascanvas=Canvas(output); color=0xff424242; Paintpaint=Paint(); Rectrect=Rect(0,0,w,h); RectFrectF=RectF(rect); paint.setAntiAlias(); canvas.drawARGB(0,0,0,0); paint.setColor(color); canvas.drawRoundRect(rectF,roundPx,roundPx,paint); paint.setXfermode(PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap,rect,rect,paint); output; } 7、获得带倒影的图片 BitmapcreateReflectionImageWithOrigin(Bitmapbitmap){ reflectionGap=4; w=bitmap.getWidth(); h=bitmap.getHeight(); Matrixmatrix=Matrix(); matrix.preScale(1,-1); BitmapreflectionImage=Bitmap.createBitmap(bitmap,0,h/2,w, h/2,matrix,); BitmapbitmapWithReflection=Bitmap.createBitmap(w,(h+h/2), Config.ARGB_8888); Canvascanvas=Canvas(bitmapWithReflection); canvas.drawBitmap(bitmap,0,0,); PaintdeafalutPaint=Paint(); canvas.drawRect(0,h,w,h+reflectionGap,deafalutPaint); canvas.drawBitmap(reflectionImage,0,h+reflectionGap,); Paintpaint=Paint(); LinearGradientshader=LinearGradient(0,bitmap.getHeight(),0, bitmapWithReflection.getHeight()+reflectionGap,0x70ffffff, 0x00ffffff,TileMode.CLAMP); paint.setShader(shader); paint.setXfermode(PorterDuffXfermode(Mode.DST_IN)); canvas.drawRect(0,h,w,bitmapWithReflection.getHeight() +reflectionGap,paint); bitmapWithReflection; } 三、Drawable 1、Bitmap转换成Drawable Bitmapbm=xxx;BitmapDrawablebd=BitmapDrawable(getResource(),bm); 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。 2、Drawable缩放 DrawablezoomDrawable(Drawabledrawable,w,h){ width=drawable.getIntrinsicWidth(); height=drawable.getIntrinsicHeight(); Bitmapoldbmp=drawableToBitmap(drawable); Matrixmatrix=Matrix(); sx=(()w/width); sy=(()h/height); matrix.postScale(sx,sy); Bitmapnewbmp=Bitmap.createBitmap(oldbmp,0,0,width,height, matrix,); BitmapDrawable(newbmp); } 本文转自dyh7077063的博客http://dyh7077063.iteye.com/blog/970672 本文转自wauoen51CTO博客,原文链接:http://blog.51cto.com/7183397/1604493,如需转载请自行联系原作者