android-http://blog.csdn.net/zhouyuanjing/article/details/7360508圖片bitmap特效處理

轉自: http://blog.csdn.net/zhouyuanjing/article/details/7360508


1. 圖片放縮

[java] view plaincopy
  1. // zoom 放縮  
  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.   
  7.     float scaleWidth = w / (float) width;  
  8.     float scaleHeight = h / (float) height;  
  9.     matrix.postScale(scaleWidth, scaleHeight);  
  10.   
  11.     Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 00, width, height,  
  12.             matrix, true);  
  13.     return bitmap2;  
  14. }  

2. 獲得圓角圖片

[java] view plaincopy
  1. // Round Corner Bitmap 獲得圓角圖片  
  2. public static Bitmap getRoundCornerBitmap(Bitmap bitmap, float roundPX /*圓角的半徑*/) {  
  3.     int width = bitmap.getWidth();  
  4.     int height = bitmap.getHeight();  
  5.   
  6.     Bitmap bitmap2 = Bitmap.createBitmap(width, height, Config.ARGB_8888);  
  7.     Canvas canvas = new Canvas(bitmap2);  
  8.   
  9.     final int color = 0xff424242;  
  10.     final Paint paint = new Paint();  
  11.     final Rect rect = new Rect(00, width, height);  
  12.     final RectF rectF = new RectF(rect);  
  13.   
  14.     paint.setColor(color);  
  15.     paint.setAntiAlias(true);  
  16.     canvas.drawARGB(0000);  
  17.     canvas.drawRoundRect(rectF, roundPX, roundPX, paint);  
  18.   
  19.     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  20.     canvas.drawBitmap(bitmap, rect, rect, paint);  
  21.   
  22.     return bitmap2;  
  23. }  

3. 獲得帶倒影的圖片

[java] view plaincopy
  1. // Reflect Bitmap 獲得帶倒影的圖片  
  2. public static Bitmap createReflectedBitmap(Bitmap bitmap) {  
  3.     //倒影和圖片本身的間距  
  4.     final int reflectedGap = 4;  
  5.     int width = bitmap.getWidth();  
  6.     int height = bitmap.getHeight();  
  7.   
  8.     Matrix matrix = new Matrix();  
  9.     matrix.preScale(1, -1);  
  10.   
  11.     Bitmap reflectedImage = Bitmap.createBitmap(bitmap, 0, height / 2,  
  12.             width, height / 2, matrix, false);  
  13.     Bitmap reflectedBitmap = Bitmap.createBitmap(width,  
  14.             (height + height / 2), Config.ARGB_8888);  
  15.   
  16.     Canvas canvas = new Canvas(reflectedBitmap);  
  17.     canvas.drawBitmap(bitmap, 00null);  
  18.     Paint defaultPaint = new Paint();  
  19.     canvas.drawRect(0, height, width, height + reflectedGap, defaultPaint);  
  20.     canvas.drawBitmap(reflectedImage, 0, height + reflectedGap, null);  
  21.   
  22.     Paint paint = new Paint();  
  23.     LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,  
  24.             reflectedBitmap.getHeight() + reflectedGap, 0x70ffffff,  
  25.             0x00ffffff, TileMode.CLAMP);  
  26.     paint.setShader(shader);  
  27.     paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  
  28.     canvas.drawRect(0, height, width, reflectedBitmap.getHeight()  
  29.             + reflectedGap, paint);  
  30.   
  31.     return reflectedBitmap;  
  32. }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章