欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义ImageView实现圆角功能

程序员文章站 2022-08-20 17:14:06
使用自定义imageview,实现圆角功能,供大家参考,具体内容如下 1.自定义属性attrs.xml

使用自定义imageview,实现圆角功能,供大家参考,具体内容如下

1.自定义属性attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="roundcornerimageview">
    <attr name="radius" format="dimension" />
    <attr name="left_top_radius" format="dimension" />
    <attr name="right_top_radius" format="dimension" />
    <attr name="right_bottom_radius" format="dimension" />
    <attr name="left_bottom_radius" format="dimension" />
  </declare-styleable>
</resources>

2.自定义roundcornerimageview,继承appcompatimageview

public class roundcornerimageview extends appcompatimageview {
  private float width, height;
  private int defaultradius = 0;
  private int radius;
  private int lefttopradius;
  private int righttopradius;
  private int rightbottomradius;
  private int leftbottomradius;


  public roundcornerimageview(context context) {
    this(context, null);
    init(context, null);
  }

  public roundcornerimageview(context context, attributeset attrs) {
    this(context, attrs, 0);
    init(context, attrs);
  }

  public roundcornerimageview(context context, attributeset attrs, int defstyleattr) {
    super(context, attrs, defstyleattr);
    init(context, attrs);
  }

  private void init(context context, attributeset attrs) {
    if (build.version.sdk_int < 18) {
      setlayertype(view.layer_type_software, null);
    }
    // 读取配置
    typedarray array = context.obtainstyledattributes(attrs, r.styleable.roundcornerimageview);
    radius = array.getdimensionpixeloffset(r.styleable.roundcornerimageview_radius, defaultradius);
    lefttopradius = array.getdimensionpixeloffset(r.styleable.roundcornerimageview_left_top_radius, defaultradius);
    righttopradius = array.getdimensionpixeloffset(r.styleable.roundcornerimageview_right_top_radius, defaultradius);
    rightbottomradius = array.getdimensionpixeloffset(r.styleable.roundcornerimageview_right_bottom_radius, defaultradius);
    leftbottomradius = array.getdimensionpixeloffset(r.styleable.roundcornerimageview_left_bottom_radius, defaultradius);


    //如果四个角的值没有设置,那么就使用通用的radius的值。
    if (defaultradius == lefttopradius) {
      lefttopradius = radius;
    }
    if (defaultradius == righttopradius) {
      righttopradius = radius;
    }
    if (defaultradius == rightbottomradius) {
      rightbottomradius = radius;
    }
    if (defaultradius == leftbottomradius) {
      leftbottomradius = radius;
    }
    array.recycle();
  }


  @override
  protected void onlayout(boolean changed, int left, int top, int right, int bottom) {
    super.onlayout(changed, left, top, right, bottom);
    width = getwidth();
    height = getheight();
  }

  @override
  protected void ondraw(canvas canvas) {
    //这里做下判断,只有图片的宽高大于设置的圆角距离的时候才进行裁剪
    int maxleft = math.max(lefttopradius, leftbottomradius);
    int maxright = math.max(righttopradius, rightbottomradius);
    int minwidth = maxleft + maxright;
    int maxtop = math.max(lefttopradius, righttopradius);
    int maxbottom = math.max(leftbottomradius, rightbottomradius);
    int minheight = maxtop + maxbottom;
    if (width >= minwidth && height > minheight) {
      path path = new path();
      //四个角:右上,右下,左下,左上
      path.moveto(lefttopradius, 0);
      path.lineto(width - righttopradius, 0);
      path.quadto(width, 0, width, righttopradius);

      path.lineto(width, height - rightbottomradius);
      path.quadto(width, height, width - rightbottomradius, height);

      path.lineto(leftbottomradius, height);
      path.quadto(0, height, 0, height - leftbottomradius);

      path.lineto(0, lefttopradius);
      path.quadto(0, 0, lefttopradius, 0);

      canvas.clippath(path);
    }
    super.ondraw(canvas);
  }

}

3.布局文件中使用

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="voicedemo.iflytek.com.roundimage.mainactivity">

  <voicedemo.iflytek.com.roundimage.roundcornerimageview
    android:id="@+id/iv_avatar"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginbottom="10dp"
    android:layout_margintop="50dp"
    android:scaletype="centercrop"
    app:left_top_radius="20dp"
    app:right_top_radius="20dp"
    />
</linearlayout>

4.调用

public class mainactivity extends appcompatactivity {

  string avatarurl = "19e9d4c0a8f1cd033ecac3692_th.jpg";

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);

    imageview ivavatar = findviewbyid(r.id.iv_avatar);

    glide.with(this).load(avatarurl).into(ivavatar);

  }
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。