[Android] bitmap/drawable/byte的转换和获取圆角/倒影图片

原文地址:http://blog.csdn.net/s278777851/article/details/6288985

字节转图片

Bitmap bitmap = BitmapFactory.decodeByteArray(iconData, 0, iconLen);

 

draw转bitmap

[java]  view plain copy
  1. public static Bitmap drawableToBitmap(Drawable drawable) {    
  2.     Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),  
  3.             drawable.getIntrinsicHeight(),  
  4.             drawable.getOpacity() != PixelFormat.OPAQUE ?   
  5.                     Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);    
  6.                     Canvas canvas = new Canvas(bitmap);    
  7.                     //canvas.setBitmap(bitmap);    
  8.     drawable.setBounds(00, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());    
  9.     drawable.draw(canvas);    
  10.     return bitmap;    
  11. }    

 

从资源中获取Bitmap

Resources res=getResources();

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

或者

Bitmap bmp = ((BitmapDrawable)getResources().getDrawable(R.drawable.show)).getBitmap();

Bitmap 转 byte[]

[c-sharp]  view plain copy
  1. private byte[] Bitmap2Bytes(Bitmap bm){    
  2.     ByteArrayOutputStream baos = new ByteArrayOutputStream();      
  3.     bm.compress(Bitmap.CompressFormat.PNG, 100, baos);      
  4.     return baos.toByteArray();    
  5. }    

放大缩小图片:

[c-sharp]  view plain copy
  1. //放大缩小图片      
  2. public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){      
  3.     int width = bitmap.getWidth();      
  4.     int height = bitmap.getHeight();      
  5.     Matrix matrix = new Matrix();      
  6.     float scaleWidht = ((float)w / width);      
  7.     float scaleHeight = ((float)h / height);      
  8.     matrix.postScale(scaleWidht, scaleHeight);      
  9.     Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);      
  10.     return newbmp;      
  11. }   

获得圆角图片的方法:

[java]  view plain copy
  1. //获得圆角图片的方法      
  2. public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){      
  3.              
  4.     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);      
  5.     Canvas canvas = new Canvas(output);      
  6.        
  7.     final int color = 0xff424242;      
  8.     final Paint paint = new Paint();      
  9.     final Rect rect = new Rect(00, bitmap.getWidth(), bitmap.getHeight());      
  10.     final RectF rectF = new RectF(rect);      
  11.        
  12.     paint.setAntiAlias(true);      
  13.     canvas.drawARGB(0000);      
  14.     paint.setColor(color);      
  15.     canvas.drawRoundRect(rectF, roundPx, roundPx, paint);      
  16.     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));      
  17.     canvas.drawBitmap(bitmap, rect, rect, paint);      
  18.            
  19.     return output;      
  20. }   

获得带倒影的图片方法:

[java]  view plain copy
  1. //获得带倒影的图片方法      
  2. public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){      
  3.     final int reflectionGap = 4;      
  4.     int width = bitmap.getWidth();      
  5.     int height = bitmap.getHeight();      
  6.               
  7.     Matrix matrix = new Matrix();      
  8.     matrix.preScale(1, -1);      
  9.               
  10.     Bitmap reflectionImage = Bitmap.createBitmap(bitmap,0, height/2, width, height/2, matrix, false);      
  11.                   
  12.     Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888);      
  13.                   
  14.     Canvas canvas = new Canvas(bitmapWithReflection);      
  15.     canvas.drawBitmap(bitmap, 00null);      
  16.     Paint deafalutPaint = new Paint();      
  17.     canvas.drawRect(0, height,width,height + reflectionGap,      
  18.     deafalutPaint);      
  19.          
  20.     canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);      
  21.                
  22.     Paint paint = new Paint();      
  23.     LinearGradient shader = new LinearGradient(0,      
  24.     bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff0x00ffffff, TileMode.CLAMP);      
  25.     paint.setShader(shader);      
  26.     // Set the Transfer mode to be porter duff and destination in      
  27.     paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));      
  28.     // Draw a rectangle using the paint with our linear gradient      
  29.     canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);      
  30.            
  31.     return bitmapWithReflection;      
  32. }    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值