android图片加载上面两个角有弧度,下面的没有,或者四个角的弧度不同的自定义控件

此自定义控件是百度找到的,现在找不到原创了,此篇博客仅供记录:


/**
 * Created by guoyan on 2018/1/4 0004.
 * 设置图片四个角的弧度,可以单独设置
 */
 
public class RoundedImageView extends ImageView {
 
    /*圆角的半径,依次为左上角xy半径,右上角,右下角,左下角*/
    private float[] rids = {15.0f,15.0f,15.0f,15.0f,0.0f,0.0f,0.0f,0.0f,};
 
    public RoundedImageView(Context context) {
        super(context);
    }
 
    public RoundedImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public RoundedImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
 
    /**
     * 画图
     * by Hankkin at:2015-08-30 21:15:53
     * @param canvas
     */
    protected void onDraw(Canvas canvas) {
        Path path = new Path();
        int w = this.getWidth();
        int h = this.getHeight();
        /*向路径中添加圆角矩形。radii数组定义圆角矩形的四个圆角的x,y半径。radii长度必须为8*/
        path.addRoundRect(new RectF(0,0,w,h),rids,Path.Direction.CW);
        canvas.clipPath(path);
        super.onDraw(canvas);
    }
}

和用imageview一样的用法

你可能感兴趣的:(自定义控件,android)