资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在Android中通过自定义ImageView实现一个圆角功能-创新互联

本篇文章给大家分享的是有关怎么在Android中通过自定义ImageView实现一个圆角功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联公司一直在为企业提供服务,多年的磨炼,使我们在创意设计,全网整合营销推广到技术研发拥有了开发经验。我们擅长倾听企业需求,挖掘用户对产品需求服务价值,为企业制作有用的创意设计体验。核心团队拥有超过10多年以上行业经验,涵盖创意,策化,开发等专业领域,公司涉及领域有基础互联网服务移动服务器托管APP应用开发、手机移动建站、网页设计、网络整合营销。Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

1.自定义属性attrs.xml



  
    
    
    
    
    
  

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.布局文件中使用




  

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);

  }
}

以上就是怎么在Android中通过自定义ImageView实现一个圆角功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


网站标题:怎么在Android中通过自定义ImageView实现一个圆角功能-创新互联
文章源于:http://cdkjz.cn/article/dpgeop.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220