圆形图片帮助类

作者: 根艮哏艮根 | 来源:发表于2017-09-22 09:32 被阅读3次

废话部队上,直接上代码

/**
* 圆形ImageView
*
* @author skg
*
*/ public class CircleImageView extends ImageView {

   public CircleImageView(Context context, AttributeSet attrs) {
       super(context, attrs);
       init();
   }

   public CircleImageView(Context context) {
       super(context);
       init();
   }

   private final RectF roundRect = new RectF();
   private float rect_adius = 8;
   private final Paint maskPaint = new Paint();
   private final Paint zonePaint = new Paint();

   private void init() {
       maskPaint.setAntiAlias(true);
       maskPaint.setFilterBitmap(true);
       maskPaint.setDither(true);
       maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
       zonePaint.setAntiAlias(true);
       zonePaint.setFilterBitmap(true);
       zonePaint.setDither(true);
       zonePaint.setColor(Color.WHITE);
                float density = getResources().getDisplayMetrics().density;
       rect_adius = rect_adius * density;
   }

   public void setRectAdius(float adius) {
       rect_adius = adius;
       invalidate();
   }

   @Override
   protected void onLayout(boolean changed, int left, int top, int right,
           int bottom) {
       super.onLayout(changed, left, top, right, bottom);
       int w = getWidth();
       rect_adius = getWidth();
       int h = getHeight();
       roundRect.set(0, 0, w, h);
   }

   @Override
   public void draw(Canvas canvas) {
       canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG);
       canvas.drawRoundRect(roundRect, rect_adius, rect_adius, zonePaint);
       canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG);
       super.draw(canvas);
       canvas.restore();
   }
}

使用方法:
在xml文件中声明即可,在Activity中的使用方法和普通的控件一致

<你的完整的包名.CircleImageView
                    android:id="@+id/my_icon_iv"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:src="@drawable/user_icon">
</你的完整的包名.CircleImageView>


相关文章

  • 圆形图片帮助类

    废话部队上,直接上代码 使用方法:在xml文件中声明即可,在Activity中的使用方法和普通的控件一致

  • 笔记:Bootstrap4 图像形状

    .rounded 类可以让图片显示圆角效果: .rounded-circle 类可以设置椭圆形图片: .img-t...

  • Glide 4解决占位符没有裁剪成圆形的问题

    废话不说,直接上代码,Kotlin版本 1、圆形图片 2、圆角图片 附上GlideRoundTransform 类...

  • Unity3d Shader实现圆形头像

    Unity3d Shader实现圆形头像 设计需要头像显示圆形,但是默认图片类型都是矩形。因为没有美术兄弟帮助所以...

  • 圆形图片

    之前有写过简单的实现圆形图片,不过效果不是很好,今天偶然看到很老的项目中用到自定义的圆形图片,也是基于画布裁剪实现...

  • 圆形图片

    https://github.com/vinc3m1/RoundedImageView

  • 图片圆形

    // 裁剪图片圆形输出 _userimag.layer.cornerRadius=_userimag.frame...

  • 圆形图片

    - (UIImageView *)cicleImageView:(UIImage *)image frame:(C...

  • Android Picasso实现圆形图片和圆角

    Android Picasso实现圆形图片和圆角图片 1.实现圆形图片 1.1代码调用如下 1.2自定义圆形图片处...

  • 图片圆角讨论

    一般有四种方式生成圆形图片: 1 . 图片是圆形的 2 . 方形图片外添加圆形镂空图片(单色背景比较好) 3 .c...

网友评论

    本文标题:圆形图片帮助类

    本文链接:https://www.haomeiwen.com/subject/huvxextx.html