Andrdoid自定义View之canvas.clipPath(path);

根据path裁剪画布

canvas.clipPath(path);

Andrdoid自定义View之canvas.clipPath(path);_第1张图片
Paste_Image.png

1.1如实现一张图片上面是圆角下面是直角,可裁剪画布。

public class RoundedImageView extends ImageView {  
  
    /*圆角的半径,依次为左上角xy半径,右上角,右下角,左下角*/  
    private float[] rids = {10.0f,10.0f,10.0f,10.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);  
    }  
}  

![Uploading Paste_Image_483488.png . . .]

2.1如自定义View四个角都是圆角,

Paste_Image.png
private float[] rids = {15.0f, 15.0f, 15.0f, 15.0f, 15.0f, 15.0f, 15.0f, 15.0f,};//四个角都圆角

...

 @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();//save、restore 图层的保存和回滚相关的方法 详见 http://blog.csdn.net/tianjian4592/article/details/45234419
        canvas.translate(0, getHeight() / 2);//移动图层到垂直居中位置


        float radio = getProgress() * 1.0f / getMax();
        float realWidth = getWidth() - getPaddingLeft() - getPaddingRight();//实际宽度减去文字宽度
        float progressX = radio * realWidth;

        Path path = new Path();
         /*向路径中添加圆角矩形。radii数组定义圆角矩形的四个圆角的x,y半径。radii长度必须为8*/
        path.addRoundRect(new RectF(getPaddingLeft(),getPaddingTop()-HorizontalProgresReachHeight/2,realWidth,HorizontalProgresReachHeight/2), rids, Path.Direction.CW);
        canvas.clipPath(path);

        //绘制走完的进度线
        mPaint.setColor(HorizontalProgresReachColor);
        mPaint.setStrokeWidth(HorizontalProgresReachHeight);
        //canvas.drawLine(getPaddingLeft(), getPaddingTop(), progressX, getPaddingTop(), mPaint);//直角 垂直在同一高度 float startY, float stopY 一样
        RectF mRectF = new RectF(getPaddingLeft(),getPaddingTop()-HorizontalProgresReachHeight/2,(int)progressX,HorizontalProgresReachHeight/2);//圆角 int left, int top, int right, int bottom
        canvas.drawRoundRect(mRectF,0,0,mPaint);//圆角矩形

        //绘制未做走完的进度
        if (progressX < getWidth() - getPaddingLeft() - getPaddingRight()) {//进度走完了,不再画未走完的
            mPaint.setColor(HorizontalProgresUnReachColor);
            mPaint.setStrokeWidth(HorizontalProgresUnReachHeight);
            //canvas.drawLine(progressX, getPaddingTop(), getWidth() - getPaddingLeft() - getPaddingRight(), getPaddingTop(), mPaint);//垂直在同一高度 float startY, float stopY 一样
            RectF mRectF2 = new RectF(progressX - 15 ,getPaddingTop()-HorizontalProgresUnReachHeight/2,realWidth,HorizontalProgresUnReachHeight/2);//圆角 int left, int top, int right, int bottom
            canvas.drawRoundRect(mRectF2,0,0,mPaint);//圆角矩形
        }
        canvas.restore();


    }

下一篇:Android自定义圆形进度条

代码已上传至Github

如喜欢此文,欢迎star和关注,不正之出,欢迎留言交流!
我的GitHub
我的CSDN
我的
开发笔记

你可能感兴趣的:(Andrdoid自定义View之canvas.clipPath(path);)