圆角和圆形ImageView
来源:互联网 发布:非关系型数据库 编辑:程序博客网 时间:2024/05/18 13:26
※效果
※代码
/** * 转换图片成圆形 * * @param bitmap * 传入Bitmap对象 * @return */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;}/** * 圆角图片 * @param bitmap * @return */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 = 12; 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; } /*** * 设置图片倒影 * @param originalBitmap * @return */private 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; }
※Demo下载
http://download.csdn.net/detail/u010785585/7690145
0 0
- 圆角和圆形ImageView
- 圆角布局和圆形ImageView
- Android圆形ImageView和圆角矩形ImageView
- 自定义imageview中的圆角和圆形图片
- 圆角矩形和圆形ImageView的实现
- 自定义ImageView实现圆角图片和圆形图片
- ImageView圆形 和 圆弧
- iOS的圆形或者圆角ImageView
- Android ImageView实现圆角,圆形图片
- 自定义圆形或圆角imageview
- Android 圆形、圆角图片ImageView
- Android BitmapShader实现圆角、圆形ImageView
- 自定义圆角矩形或者圆形ImageView
- 重写ImageView使用Xfermode遮罩实现圆角和圆形
- Android使用Xfermode图形渲染方法实现自定义圆形、圆角和椭圆ImageView
- Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示
- android 圆形ListView实现,并附带圆角ImageView
- Android:继承ImageView 实现 圆角图片,圆形图片处理。
- 数据结构和算法设计专题之---八大内部排序
- 机会只看到房价多少了罚款时间到了数字机顶盒
- POJ The Triangle
- 通过扩展 CWebUser 增加信息到 Yii::app()->user
- mysql修改root密码
- 圆角和圆形ImageView
- UVa 140 - BandWidth 解题报告(暴力)
- Java冒泡排序算法
- C语言编译四则运算
- 加密数
- JDBC数据库通用DAO
- PackageManager使用
- hdoj.2003 求绝对值 20140721
- 手机内存卡格式化文件怎么恢复