自定义圆角控件

常用工具类记录

java 代码
public class RoundImageView extends ImageView {

    float width, height;
    private int leftTopRadius;
    private int rightTopRadius;
    private int leftBottomRadius;
    private int rightBottomRadius;
    //		默认角度
    private int defaultRadius = 6;
    private int radius;
    private Context mContext;

    public RoundImageView(Context context) {
        this(context, null);
        mContext = context;
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        mContext = context;
        init(context, attrs);
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        if (Build.VERSION.SDK_INT < 18) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    }

    @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();
    }

    private void init(Context context, AttributeSet attrs) {

        //读取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Round_Angle_Image_View);
        radius = typedArray.getDimensionPixelOffset(R.styleable.Round_Angle_Image_View_radius, defaultRadius);
        leftTopRadius = typedArray.getDimensionPixelOffset(R.styleable.Round_Angle_Image_View_left_top_radius, defaultRadius);
        rightTopRadius = typedArray.getDimensionPixelOffset(R.styleable.Round_Angle_Image_View_right_top_radius, defaultRadius);
        leftBottomRadius = typedArray.getDimensionPixelOffset(R.styleable.Round_Angle_Image_View_left_bottom_radius, defaultRadius);
        rightBottomRadius = typedArray.getDimensionPixelOffset(R.styleable.Round_Angle_Image_View_right_bottom_radius, defaultRadius);


        //如果四个角的值没有设置,就用通用的radius
        if (leftTopRadius == defaultRadius) {
            leftTopRadius = radius;
        }
        if (rightTopRadius == defaultRadius) {
            rightTopRadius = radius;
        }
        if (leftBottomRadius == defaultRadius) {
            leftBottomRadius = radius;
        }
        if (rightBottomRadius == defaultRadius) {
            rightBottomRadius = radius;
        }
        typedArray.recycle();

    }

    @Override
    protected void onDraw(Canvas canvas) {

        Path path = new Path();
        /*向路径中添加圆角矩形。radii数组定义圆角矩形的四个圆角的x,y半径。radii长度必须为8*/
        float rids[] = {leftTopRadius, leftTopRadius, rightTopRadius, rightTopRadius, leftBottomRadius, leftBottomRadius, rightBottomRadius, rightBottomRadius};
        path.addRoundRect(new RectF(0, 0, width, height), rids, Path.Direction.CW);
        canvas.clipPath(path);

        super.onDraw(canvas);
    }

}
自定义属性
  <!-- 自定义四角矩形ImageView 的四个圆角属性 -->
 <declare-styleable name="Round_Angle_Image_View">
     <attr name="left_top_radius" format="dimension"/>
     <attr name="right_top_radius" format="dimension"/>
     <attr name="left_bottom_radius" format="dimension"/>
     <attr name="right_bottom_radius" format="dimension"/>
     <attr name="radius" format="dimension"/>
 </declare-styleable>
使用
 <com.xxx.RoundImageView
	 android:scaleType="centerCrop"
	 roundiv:radius="6dp"
	 android:layout_width="match_parent"
	 android:layout_height="match_parent"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值