ImageView 圆角

实现自定义ImageView圆角

  • 设置圆角大小
  • 直接设置为圆形

自定义View的代码

public class RoundedImageView extends ImageView {

    private float radius = 0f; // 默认圆角半径
    private boolean isCircle = false; // 是否绘制为圆形

    public RoundedImageView(Context context) {
        super(context);
    }

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

    public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedImageView);
        radius = a.getDimension(R.styleable.RoundedImageView_cornerRadius, 0);//获取圆角值
        isCircle = a.getBoolean(R.styleable.RoundedImageView_isCircle, false); // 获取 isCircle 属性值
        a.recycle();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        Path clipPath = new Path();
        if (isCircle) {
            // 如果ImageView是圆形,那么只需要取宽和高中的最小值作为半径即可
            float radius = Math.min(this.getWidth(), this.getHeight()) / 2.0f;
            clipPath.addCircle(this.getWidth() / 2.0f, this.getHeight() / 2.0f, radius, Path.Direction.CW);
        } else {
            // 否则,绘制圆角矩形
            RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
            clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
        }
        canvas.clipPath(clipPath);
        super.onDraw(canvas);
    }
    /*
     * 设置圆角大小,以dp为单位*/
    public void setCornerRadiusDP(float dp) {
        float density = getResources().getDisplayMetrics().density;
        float px = dp * density;
        this.radius = px;
        invalidate();
    }
}

attrs.xml 的样式

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="RoundedImageView">
        <attr name="cornerRadius" format="dimension" />
        <attr name="isCircle" format="boolean" />
    </declare-styleable>
</resources>

xml调用

  <com.example.test.views.RoundedImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher_background"
        android:scaleType="fitXY"
        app:cornerRadius="15dp"
        />

    <com.example.test.views.RoundedImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher_background"
        android:scaleType="fitXY"
        app:isCircle="true"
        />

    <com.example.test.views.RoundedImageView
        android:id="@+id/rv_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher_background"
        android:scaleType="fitXY"
        />

//代码直接设置圆角大小
RoundedImageView rvImage = (RoundedImageView)  findViewById(R.id.rv_image);
 rvImage.setCornerRadiusDP(30);

实际效果图
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值