Android中Bitmap和Drawable

一、相关概念

1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象
2、Canvas画布,绘图的目的区域,用于绘图
3、Bitmap位图,用于图的处理
4、Matrix矩阵

二、Bitmap

1、从资源中获取Bitmap

Java代码 收藏代码
  1. Resourcesres=getResources();
  2. Bitmapbmp=BitmapFactory.decodeResource(res,R.drawable.icon);

2、Bitmap → byte[]

Java代码 收藏代码
  1. publicbyte[]Bitmap2Bytes(Bitmapbm){
  2. ByteArrayOutputStreambaos=newByteArrayOutputStream();
  3. bm.compress(Bitmap.CompressFormat.PNG,100,baos);
  4. returnbaos.toByteArray();
  5. }

3、byte[] → Bitmap

Java代码 收藏代码
  1. publicBitmapBytes2Bimap(byte[]b){
  2. if(b.length!=0){
  3. returnBitmapFactory.decodeByteArray(b,0,b.length);
  4. }else{
  5. returnnull;
  6. }
  7. }

4、Bitmap缩放

Java代码 收藏代码
  1. publicstaticBitmapzoomBitmap(Bitmapbitmap,intwidth,intheight){
  2. intw=bitmap.getWidth();
  3. inth=bitmap.getHeight();
  4. Matrixmatrix=newMatrix();
  5. floatscaleWidth=((float)width/w);
  6. floatscaleHeight=((float)height/h);
  7. matrix.postScale(scaleWidth,scaleHeight);
  8. Bitmapnewbmp=Bitmap.createBitmap(bitmap,0,0,w,h,matrix,true);
  9. returnnewbmp;
  10. }

5、将Drawable转化为Bitmap

Java代码 收藏代码
  1. publicstaticBitmapdrawableToBitmap(Drawabledrawable){
  2. //取drawable的长宽
  3. intw=drawable.getIntrinsicWidth();
  4. inth=drawable.getIntrinsicHeight();
  5. //取drawable的颜色格式
  6. Bitmap.Configconfig=drawable.getOpacity()!=PixelFormat.OPAQUE?Bitmap.Config.ARGB_8888
  7. :Bitmap.Config.RGB_565;
  8. //建立对应bitmap
  9. Bitmapbitmap=Bitmap.createBitmap(w,h,config);
  10. //建立对应bitmap的画布
  11. Canvascanvas=newCanvas(bitmap);
  12. drawable.setBounds(0,0,w,h);
  13. //把drawable内容画到画布中
  14. drawable.draw(canvas);
  15. returnbitmap;
  16. }

6、获得圆角图片

Java代码 收藏代码
  1. publicstaticBitmapgetRoundedCornerBitmap(Bitmapbitmap,floatroundPx){
  2. intw=bitmap.getWidth();
  3. inth=bitmap.getHeight();
  4. Bitmapoutput=Bitmap.createBitmap(w,h,Config.ARGB_8888);
  5. Canvascanvas=newCanvas(output);
  6. finalintcolor=0xff424242;
  7. finalPaintpaint=newPaint();
  8. finalRectrect=newRect(0,0,w,h);
  9. finalRectFrectF=newRectF(rect);
  10. paint.setAntiAlias(true);
  11. canvas.drawARGB(0,0,0,0);
  12. paint.setColor(color);
  13. canvas.drawRoundRect(rectF,roundPx,roundPx,paint);
  14. paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));
  15. canvas.drawBitmap(bitmap,rect,rect,paint);
  16. returnoutput;
  17. }

7、获得带倒影的图片

Java代码 收藏代码
  1. publicstaticBitmapcreateReflectionImageWithOrigin(Bitmapbitmap){
  2. finalintreflectionGap=4;
  3. intw=bitmap.getWidth();
  4. inth=bitmap.getHeight();
  5. Matrixmatrix=newMatrix();
  6. matrix.preScale(1,-1);
  7. BitmapreflectionImage=Bitmap.createBitmap(bitmap,0,h/2,w,
  8. h/2,matrix,false);
  9. BitmapbitmapWithReflection=Bitmap.createBitmap(w,(h+h/2),
  10. Config.ARGB_8888);
  11. Canvascanvas=newCanvas(bitmapWithReflection);
  12. canvas.drawBitmap(bitmap,0,0,null);
  13. PaintdeafalutPaint=newPaint();
  14. canvas.drawRect(0,h,w,h+reflectionGap,deafalutPaint);
  15. canvas.drawBitmap(reflectionImage,0,h+reflectionGap,null);
  16. Paintpaint=newPaint();
  17. LinearGradientshader=newLinearGradient(0,bitmap.getHeight(),0,
  18. bitmapWithReflection.getHeight()+reflectionGap,0x70ffffff,
  19. 0x00ffffff,TileMode.CLAMP);
  20. paint.setShader(shader);
  21. //SettheTransfermodetobeporterduffanddestinationin
  22. paint.setXfermode(newPorterDuffXfermode(Mode.DST_IN));
  23. //Drawarectangleusingthepaintwithourlineargradient
  24. canvas.drawRect(0,h,w,bitmapWithReflection.getHeight()
  25. +reflectionGap,paint);
  26. returnbitmapWithReflection;
  27. }

三、Drawable

1、Bitmap转换成Drawable

Java代码 收藏代码
  1. Bitmapbm=xxx;//xxx根据你的情况获取
  2. BitmapDrawablebd=newBitmapDrawable(getResource(),bm);
  3. 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。

2、Drawable缩放

Java代码 收藏代码
  1. publicstaticDrawablezoomDrawable(Drawabledrawable,intw,inth){
  2. intwidth=drawable.getIntrinsicWidth();
  3. intheight=drawable.getIntrinsicHeight();
  4. //drawable转换成bitmap
  5. Bitmapoldbmp=drawableToBitmap(drawable);
  6. //创建操作图片用的Matrix对象
  7. Matrixmatrix=newMatrix();
  8. //计算缩放比例
  9. floatsx=((float)w/width);
  10. floatsy=((float)h/height);
  11. //设置缩放比例
  12. matrix.postScale(sx,sy);
  13. //建立新的bitmap,其内容是对原bitmap的缩放后的图
  14. Bitmapnewbmp=Bitmap.createBitmap(oldbmp,0,0,width,height,
  15. matrix,true);
  16. returnnewBitmapDrawable(newbmp);
  17. }

你可能感兴趣的:(drawable)