圆角图片控件

功能

可以实现圆角的图片控件

代码

RoundedImageView

package com.example.myapplication.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;

import com.example.myapplication.R;


/**
 * <pre>
 * Created by zhuguohui
 * Date: 2023/1/11
 * Time: 10:39
 * Desc:圆角图片
 * 使用radius属性定义四个角的圆角
 * 使用radius_top_left 定义左上角
 * 使用radius_top_right 定义右上角
 * 使用radius_bottom_left 定义左下角
 * 使用radius_bottom_right 定义右下角
 * 如果radius是宽高的一半。那么就是圆形图片了
 * </pre>
 */
public class RoundedImageView extends androidx.appcompat.widget.AppCompatImageView {

    private final float tlRadius;
    private final float trRadius;
    private final float brRadius;
    private final float blRadius;
    private Path path;

    public RoundedImageView(Context context) {
        this(context, null);
    }

    public RoundedImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RoundedImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RoundedImageView);

        float radius = array.getDimension(R.styleable.RoundedImageView_radius, 0);
        tlRadius = array.getDimension(R.styleable.RoundedImageView_radius_top_left, radius);
        trRadius = array.getDimension(R.styleable.RoundedImageView_radius_top_right, radius);
        brRadius = array.getDimension(R.styleable.RoundedImageView_radius_bottom_right, radius);
        blRadius = array.getDimension(R.styleable.RoundedImageView_radius_bottom_left, radius);
        setBackground(new ColorDrawable());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int save = canvas.save();
        canvas.clipPath(path, Region.Op.INTERSECT);
        super.onDraw(canvas);
        canvas.restoreToCount(save);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        path = new Path();
        RectF rectF = getRectF();
        float[] readius = {tlRadius, tlRadius, trRadius, trRadius, brRadius, brRadius, blRadius, blRadius};
        path.addRoundRect(rectF, readius, Path.Direction.CW);
    }

    private RectF getRectF() {
        Rect rect = new Rect();
        getDrawingRect(rect);
        RectF rectF = new RectF(rect);
        return rectF;
    }
}

xml属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    
    
    <declare-styleable name="RoundedImageView">
        <!--四个角的大小-->
        <attr name="radius" format="dimension" />
        <!--        左上圆角大小-->
        <attr name="radius_top_left" format="dimension" />
        <!--        右上圆角大小-->
        <attr name="radius_top_right" format="dimension" />
        <!--        右下圆角大小-->
        <attr name="radius_bottom_right" format="dimension" />
        <!--        左下圆角大小-->
        <attr name="radius_bottom_left" format="dimension" />
    </declare-styleable>
</resources>

使用

可以设置不同方向上的圆角
在这里插入图片描述

可以统一设置圆角
在这里插入图片描述
如果圆角是宽高的一半,就是圆形控件了
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值