Android自定义ImageView实现圆角功能

 更新时间:2018年12月20日 11:48:35   作者:676598624  
这篇文章主要为大家详细介绍了Android自定义ImageView实现圆角功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

使用自定义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);

  }
} 

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

相关文章

  • Android TabLayout 自定义样式及使用详解

    Android TabLayout 自定义样式及使用详解

    这篇文章主要为大家介绍了Android TabLayout 自定义样式及使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Android内容提供者ContentProvider用法实例分析

    Android内容提供者ContentProvider用法实例分析

    这篇文章主要介绍了Android内容提供者ContentProvider用法,结合实例形式较为详细的分析了内容提供者ContentProvider获取及解析数据的相关技巧,需要的朋友可以参考下
    2016-03-03
  • Android实现启动页倒计时效果

    Android实现启动页倒计时效果

    这篇文章主要介绍了Android实现启动页倒计时效果的示例代码,帮助大家更好的理解和学习使用Android进行开发,感兴趣的朋友可以了解下
    2021-04-04
  • Android自定义密码输入EditTextLayout

    Android自定义密码输入EditTextLayout

    这篇文章主要为大家详细介绍了Android自定义密码输入EditTextLayout,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • Android保持屏幕常亮唤醒状态的方法

    Android保持屏幕常亮唤醒状态的方法

    这篇文章主要介绍了Android保持屏幕常亮唤醒状态的方法,实例分析了Android权限控制及屏幕状态操作的相关实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10
  • 详解Android.activity销毁流程的工作原理

    详解Android.activity销毁流程的工作原理

    这篇文章主要介绍了详解Activity销毁流程的工作原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • 基于Android的英文词典的实现方法

    基于Android的英文词典的实现方法

    这篇文章主要为大家详细介绍了基于Android的英文词典的实现方法
    2016-05-05
  • android检测SD卡读写权限方法

    android检测SD卡读写权限方法

    今天小编就为大家分享一篇android检测SD卡读写权限方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • Android context源码详解及深入分析

    Android context源码详解及深入分析

    这篇文章主要介绍了Android context源码详解及深入分析的相关资料,这里对Android Context 如何使用进行了详细介绍,需要的朋友可以参考下
    2017-01-01
  • Android实现自定义轮播图片控件示例

    Android实现自定义轮播图片控件示例

    我们都知道我们做软件的时候,有些应用是有广告的轮番图的,我们实现这个功能的时候大多数是采用:ViewPager +LinearLayout来实现的,今天分享一下我自己自定义的广告轮番图的控件!
    2016-11-11

最新评论