读书人

图片设立圆角、圆形、投影

发布时间: 2013-12-23 14:30:52 作者: rapoo

图片设置圆角、圆形、投影
/**图片转换成圆形 */public Bitmap toRoundBitmap(Bitmap bitmap) {int width = bitmap.getWidth();int height = bitmap.getHeight();float roundPx;float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;if (width <= height) {roundPx = width / 2;left = 0;top = 0;right = width;bottom = width;height = width;dst_left = 0;dst_top = 0;dst_right = width;dst_bottom = width;} else {roundPx = height / 2;float clip = (width - height) / 2;left = clip;right = width - clip;top = 0;bottom = height;width = height;dst_left = 0;dst_top = 0;dst_right = height;dst_bottom = height;}Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);Canvas canvas = new Canvas(output);final Paint paint = new Paint();final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);final RectF rectF = new RectF(dst);paint.setAntiAlias(true);// 设置画笔无锯齿canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas// 以下有两种方法画圆,drawRounRect和drawCirclecanvas.drawRoundRect(rectF, roundPx, roundPx, paint);// 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。// canvas.drawCircle(roundPx, roundPx, roundPx, paint);paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452canvas.drawBitmap(bitmap, src, dst, paint); // 以Mode.SRC_IN模式合并bitmap和已经draw了的Circlereturn output;}/**图片转换成圆角*/public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = 50; /** 这里设置圆角度数 */ paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } /**图片设置倒影*/public static Bitmap createReflectedImage(Bitmap originalBitmap) { // 图片与倒影间隔距离 final int reflectionGap = 4; // 图片的宽度 int width = originalBitmap.getWidth(); // 图片的高度 int height = originalBitmap.getHeight(); Matrix matrix = new Matrix(); // 图片缩放,x轴变为原来的1倍,y轴为-1倍,实现图片的反转 matrix.preScale(1, -1); // 创建反转后的图片Bitmap对象,图片高是原图的一半。 Bitmap reflectionBitmap = Bitmap.createBitmap(originalBitmap, 0, height / 2, width, height / 2, matrix, false); // 创建标准的Bitmap对象,宽和原图一致,高是原图的1.5倍。 Bitmap withReflectionBitmap = Bitmap.createBitmap(width, (height + height / 2 + reflectionGap), Config.ARGB_8888); // 构造函数传入Bitmap对象,为了在图片上画图 Canvas canvas = new Canvas(withReflectionBitmap); // 画原始图片 canvas.drawBitmap(originalBitmap, 0, 0, null); // 画间隔矩形 Paint defaultPaint = new Paint(); canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint); // 画倒影图片 canvas.drawBitmap(reflectionBitmap, 0, height + reflectionGap, null); // 实现倒影效果 Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, originalBitmap.getHeight(), 0, withReflectionBitmap.getHeight(), 0x70ffffff, 0x00ffffff, TileMode.MIRROR); paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // 覆盖效果 canvas.drawRect(0, height, width, withReflectionBitmap.getHeight(), paint); return withReflectionBitmap; }

?

读书人网 >移动开发

热点推荐