1.attrs添加

    <declare-styleable name="RoundImageView"><attr name="circle" format="boolean" /><attr name="radius" format="dimension" /></declare-styleable>

2.新增class

public class RoundImageView  extends ImageView {private Paint paint;private Paint paintBorder;private Bitmap mSrcBitmap;/*** 圆角的弧度*/private float mRadius;private boolean mIsCircle;public RoundImageView(final Context context) {this(context, null);}public RoundImageView(Context context, AttributeSet attrs) {super(context, attrs);TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);mRadius = ta.getDimension(R.styleable.RoundImageView_radius, 0);mIsCircle = ta.getBoolean(R.styleable.RoundImageView_circle, false);int srcResource = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "src", 0);if (srcResource != 0)mSrcBitmap = BitmapFactory.decodeResource(getResources(),srcResource);ta.recycle();paint = new Paint();paint.setAntiAlias(true);paintBorder = new Paint();paintBorder.setAntiAlias(true);}@Overridepublic void onDraw(Canvas canvas) {int width = canvas.getWidth() - getPaddingLeft() - getPaddingRight();int height = canvas.getHeight() - getPaddingTop() - getPaddingBottom();Bitmap image = drawableToBitmap(getDrawable());if (mIsCircle) {Bitmap reSizeImage = reSizeImageC(image, width, height);canvas.drawBitmap(createCircleImage(reSizeImage, width, height),getPaddingLeft(), getPaddingTop(), null);} else {Bitmap reSizeImage = reSizeImage(image, width, height);canvas.drawBitmap(createRoundImage(reSizeImage, width, height),getPaddingLeft(), getPaddingTop(), null);}}/*** 画圆角** @param source* @param width* @param height* @return*/private Bitmap createRoundImage(Bitmap source, int width, int height) {Paint paint = new Paint();paint.setAntiAlias(true);Bitmap target = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(target);RectF rect = new RectF(0, 0, width, height);canvas.drawRoundRect(rect, mRadius, mRadius, paint);// 核心代码取两个图片的交集部分paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));canvas.drawBitmap(source, 0, 0, paint);return target;}/*** 画圆** @param source* @param width* @param height* @return*/private Bitmap createCircleImage(Bitmap source, int width, int height) {Paint paint = new Paint();paint.setAntiAlias(true);Bitmap target = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(target);canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2,paint);// 核心代码取两个图片的交集部分paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));canvas.drawBitmap(source, (width - source.getWidth()) / 2,(height - source.getHeight()) / 2, paint);return target;}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int width = MeasureSpec.getSize(widthMeasureSpec);int height = MeasureSpec.getSize(heightMeasureSpec);setMeasuredDimension(width, height);}/*** drawable转bitmap** @param drawable* @return*/private Bitmap drawableToBitmap(Drawable drawable) {if (drawable == null) {if (mSrcBitmap != null) {return mSrcBitmap;} else {return null;}} else if (drawable instanceof BitmapDrawable) {return ((BitmapDrawable) drawable).getBitmap();}Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());drawable.draw(canvas);return bitmap;}/*** 重设Bitmap的宽高** @param bitmap* @param newWidth* @param newHeight* @return*/private Bitmap reSizeImage(Bitmap bitmap, int newWidth, int newHeight) {int width = bitmap.getWidth();int height = bitmap.getHeight();// 计算出缩放比float scaleWidth = ((float) newWidth) / width;float scaleHeight = ((float) newHeight) / height;// 矩阵缩放bitmapMatrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);}/*** 重设Bitmap的宽高** @param bitmap* @param newWidth* @param newHeight* @return*/private Bitmap reSizeImageC(Bitmap bitmap, int newWidth, int newHeight) {int width = bitmap.getWidth();int height = bitmap.getHeight();int x = (newWidth - width) / 2;int y = (newHeight - height) / 2;if (x > 0 && y > 0) {return Bitmap.createBitmap(bitmap, 0, 0, width, height, null, true);}float scale = 1;if (width > height) {// 按照宽度进行等比缩放scale = ((float) newWidth) / width;} else {// 按照高度进行等比缩放// 计算出缩放比scale = ((float) newHeight) / height;}Matrix matrix = new Matrix();matrix.postScale(scale, scale);return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);}}

3.使用:

 <RoundImageViewandroid:layout_width="40dp"android:layout_height="40dp"android:scaleType="centerCrop"android:id="@+id/headImg"android:src="@mipmap/menudefault"app:radius="20dp"/>

转载于:https://www.cnblogs.com/hualuoshuijia/p/9238375.html

android ImageView加圆角相关推荐

  1. android imageview 加边框,RCImageView 自定义圆角ImageView,带边框效果

    概况 RCImageView 圆型或者圆角图片(带有边框效果),适配了ImageView的ScaleType属性 RoundImageView 定义圆角图片,四个角圆角弧度可各自定义,也可不定义默认角 ...

  2. android对图片提升清晰度,android – ImageView加载高分辨率图像质量非常差

    我正在使用ListFragment来显示使用 Android Universal Image Loader的Lazy Loaded ImageView对象的ListView.正确的数据来自我的数据源, ...

  3. android xml 圆形图片,Android ImageView实现圆角,圆形图片

    UI设计中,为了有更好的效果,用户的头像很多以圆形方式显示,其实现的步骤 1 在res/values/attrs中添加 2 自定义View,CustomImageView.java package c ...

  4. android 布局加圆角,Android通用圆角布局

    名称 rclayout 语言 Android 平台 GitHub 作者 GcsSloop 该布局是一个可以任意设定布局圆角的大小,宽度,颜色等.可满足基于的开发工作中遇到的圆角布局需求. 效果图 效果 ...

  5. Android imageview设置圆角

    顶部左右圆角 四角都圆角 第一种方法使用glide自带实现圆角方法 RoundedCorners roundedCorners = new RoundedCorners(5);圆角为5RequestO ...

  6. Android Glide加载图片时转换为圆形、圆角、毛玻璃等图片效果

     Android Glide加载图片时转换为圆形.圆角.毛玻璃等图片效果 附录1简单介绍了Android开源的图片加载框架.在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬 ...

  7. android Imageview 随心所欲定制四个圆角

    android Imageview 随心所欲定制四个圆角 实现效果图如下 一个四个圆角的view 包含一个上面两个圆角的Imageview ,这样的设计比较美观,但是这样在安卓端实现让我思考了一段时间 ...

  8. android 图片变量,Android开发实现ImageView加载摄像头拍摄的大图功能

    本文实例讲述了Android开发实现ImageView加载摄像头拍摄的大图功能.分享给大家供大家参考,具体如下: 这个方法是从官方demo中摘录的,在此记录学习. 权限 android:name=&q ...

  9. android imageview 设置网络图片,ImageView加载网络图片

    android网络加载图片框架Android-Universal-Image-Loader功能非常强大,其开源地址https://github.com/nostra13/Android-Univers ...

  10. Android开发 - ImageView加载Base64编码的图片

    在我们开发应用的过程中,并不是所有情况下都请求图片的URL或者加载本地图片,有时我们需要加载Base64编码的图片.这种情况出现在服务端需要动态生成的图片,比如: 二维码 图形验证码 - 这些应用场景 ...

最新文章

  1. CVPR2019:无人驾驶3D目标检测论文点评
  2. 杭电 hdu 2096
  3. java extjs如何使用_ExtJS初探:在项目中使用ExtJS
  4. 重磅直播|大规模点云可视化技术
  5. 《数字孪生体技术白皮书(2019)》(简版)全文
  6. jenkins 下载插件 一直失败_Jenkins 配置国内插件更新源
  7. 通过JS原型定义字符串格式化方法
  8. JAVA--位移运算符详细分析【转载】
  9. LATEX数学式哪种字号比较漂亮
  10. 从面试题看考察知识点(四)
  11. [转]Log4Net五步走
  12. python在工程管理专业的应用_专业篇丨建筑环境与能源应用工程专业和建筑电气与智能化专业...
  13. 003《区块链开发指南》一一1.2 区块和区块链 转
  14. 2014年最新前端开发面试题(题目列表+答案 完整版)
  15. MyEclipse 10.5与ExtJS 4.1.1自动代码提示
  16. WBE15—HttpServletRequest
  17. python云台控制原理_python伺服云台摄像头图像作为背景
  18. 一文了解地理数据和三维地理信息系统
  19. SQL error OCI Error 2003 问题处理
  20. sinx/cosx的泰勒展开式与库函数的比较

热门文章

  1. echarts使用_做数据可视化,为什么我们不再直接使用D3.js、Echarts
  2. PAT之树:一般树、二叉树、完全二叉树、二叉搜索树、二叉平衡树、并查集
  3. c语言中fprintf的作用,C语言中的printf(),sprintf()和fprintf()
  4. 挑战程序设计竞赛_竞赛通知 | 第二届全国高校计算机能力挑战赛——程序设计赛来啦...
  5. react 获取url参数_十分钟上手 React+MirrorX,从此前端大神代码不再难懂
  6. 【UVA10305】Ordering Tasks(拓扑排序)
  7. PAT 乙级A1025 适合当算法入门练习题做
  8. 库ppt演示 python_用Python实现PPT转化图片
  9. 风控中英文术语手册(银行_消费金融信贷业务)_version6
  10. 信用评分-(scorecard)记分卡开发流程,详细介绍分数校准原理calibration