标签:
介绍一种简单的、另类的实现,就是把图片在显示前处理成圆角的,但是不改变存储的图片。相当于经过了图像过滤。
需要调用的图像工具类是
1 package com.gaosi.util; 2 /** 3 * @author Qianyukun 4 * 5 */ 6 import android.graphics.Bitmap; 7 import android.graphics.Bitmap.Config; 8 import android.graphics.Canvas; 9 import android.graphics.LinearGradient; 10 import android.graphics.Matrix; 11 import android.graphics.Paint; 12 import android.graphics.PixelFormat; 13 import android.graphics.PorterDuff.Mode; 14 import android.graphics.PorterDuffXfermode; 15 import android.graphics.Rect; 16 import android.graphics.RectF; 17 import android.graphics.Shader.TileMode; 18 import android.graphics.drawable.Drawable; 19 20 public class BitmapUtil { 21 // 放大缩小图片 22 public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) { 23 if (bitmap == null) 24 return null; 25 int width = bitmap.getWidth(); 26 int height = bitmap.getHeight(); 27 Matrix matrix = new Matrix(); 28 float scaleWidht = ((float) w / width); 29 float scaleHeight = ((float) h / height); 30 matrix.postScale(scaleWidht, scaleHeight); 31 Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, 32 matrix, true); 33 return newbmp; 34 } 35 36 // 将Drawable转化为Bitmap 37 public static Bitmap drawableToBitmap(Drawable drawable) { 38 if (drawable == null) 39 return null; 40 int width = drawable.getIntrinsicWidth(); 41 int height = drawable.getIntrinsicHeight(); 42 Bitmap bitmap = Bitmap.createBitmap(width, height, drawable 43 .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 44 : Bitmap.Config.RGB_565); 45 Canvas canvas = new Canvas(bitmap); 46 drawable.setBounds(0, 0, width, height); 47 drawable.draw(canvas); 48 return bitmap; 49 } 50 51 // 获得圆角图片的方法 52 public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) { 53 if (bitmap == null) 54 return null; 55 Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), 56 bitmap.getHeight(), Config.ARGB_8888); 57 Canvas canvas = new Canvas(output); 58 59 final int color = 0xff424242; 60 final Paint paint = new Paint(); 61 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 62 final RectF rectF = new RectF(rect); 63 64 paint.setAntiAlias(true); 65 canvas.drawARGB(0, 0, 0, 0); 66 paint.setColor(color); 67 canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 68 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 69 canvas.drawBitmap(bitmap, rect, rect, paint); 70 return output; 71 } 72 73 // 获得带倒影的图片方法 74 public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) { 75 if (bitmap == null) 76 return null; 77 final int reflectionGap = 4; 78 int width = bitmap.getWidth(); 79 int height = bitmap.getHeight(); 80 81 Matrix matrix = new Matrix(); 82 matrix.preScale(1, -1); 83 84 Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, 85 width, height / 2, matrix, false); 86 87 Bitmap bitmapWithReflection = Bitmap.createBitmap(width, 88 (height + height / 2), Config.ARGB_8888); 89 90 Canvas canvas = new Canvas(bitmapWithReflection); 91 canvas.drawBitmap(bitmap, 0, 0, null); 92 Paint deafalutPaint = new Paint(); 93 canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint); 94 95 canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); 96 97 Paint paint = new Paint(); 98 LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, 99 bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 100 0x00ffffff, TileMode.CLAMP); 101 paint.setShader(shader); 102 paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 103 canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() 104 + reflectionGap, paint); 105 106 return bitmapWithReflection; 107 } 108 109 }
将要显示的图片过滤成圆角,再装载到imageview中
bitmap=BitmapUtil.getRoundedCornerBitmap(bitmap, Configs.BITMAPROUNDPX);//设置圆角角度(int)
imageview.setImageBitmap(bitmap);
效果:
标签:
原文地址:http://www.cnblogs.com/qianyukun/p/4446937.html