超简洁代码实现CircleImageView
发布时间:2020-12-15 03:16:40 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 public class CircleView extends ImageView { private Paint mPaint = new Paint(); public CircleView(Context context,AttributeSet attrs) { supe
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 public class CircleView extends ImageView { private Paint mPaint = new Paint(); public CircleView(Context context,AttributeSet attrs) { super(context,attrs); } @Override protected void onDraw(Canvas canvas) { Drawable mDrawable = getDrawable();//获取xml文件设置的图片 if (null == mDrawable) super.onDraw(canvas);//如果为空,交给父类处理 Bitmap mBitmap = ((BitmapDrawable) mDrawable).getBitmap();//将图片转化成bitmap int len = getWidth() < getHeight() ? getWidth() : getHeight();//获取xml的长宽属性值,选择较小的 Bitmap tempBitmap = Bitmap.createBitmap(len,len,Bitmap.Config.ARGB_8888);//新建一个bitmap对象,作为缓存 Canvas mCanvas = new Canvas(tempBitmap);//新建画布缓存bitmap对象 mPaint.setAntiAlias(true);//抗锯齿 mCanvas.drawCircle(len / 2,len / 2,mPaint);//画一个圆 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//设置画笔的覆盖类型 Matrix matrix = new Matrix(); // 初始化Matrix对象 matrix.setScale((float) len / mBitmap.getWidth(),(float) len / mBitmap.getHeight()); //设置缩放比例 mCanvas.drawBitmap(mBitmap,matrix,mPaint);//画出缩放后的图片 mPaint.reset();//重置画笔 canvas.drawBitmap(tempBitmap,mPaint);//绘制缓存图片 } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |