Android自定义view贝塞尔曲线重绘问题

Android自定义view,怎么不会销毁之前的然后重绘啊?下面贴上代码,请大神帮忙看下

图片说明

源码如下:

 import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class BezierView extends View {
    private Path mPath;
    private Point startPoint;
    private Point endPoint;
    // 辅助点
    private Point assistPoint;
    private Paint mPaint;


    public BezierView(Context context) {
        super(context);
        init(context);
    }

    public BezierView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public BezierView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public BezierView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    /**
     * 初始化画笔 路径
     */
    private void init(Context context){
        mPaint=new Paint();
        mPaint.setColor(Color.BLACK);
        //抗锯齿
        mPaint.setAntiAlias(true);
        //防抖动
        mPaint.setDither(true);
        //描边
        mPaint.setStyle(Paint.Style.STROKE);
        //笔宽
        mPaint.setStrokeWidth(5);


        mPath=new Path();

        startPoint=new Point(200,200);
        endPoint=new Point(400,200);
        assistPoint=new Point(600,900);


    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //重置路径
//        mPaint.reset();
        //起点
        mPath.moveTo(startPoint.x,startPoint.y);
        //
        mPath.quadTo(assistPoint.x,assistPoint.y,endPoint.x,endPoint.y);
        //画路径
        canvas.drawPath(mPath,mPaint);
        //画辅助点
        canvas.drawPoint(assistPoint.x,assistPoint.y,mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE :
                assistPoint.x= (int) event.getX();
                assistPoint.y= (int) event.getY();
                break;
        }
        invalidate();
        return true;

    }
}

4个回答

你应该是写错了,重置应该是mPath.reset();

u010825838
u010825838 兄弟你是对的,是我太粗心了。谢谢
大约 3 年之前 回复

销毁之前的吗,你要重置Path啊

u010825838
u010825838 谢谢,我试了销毁之前和在销毁之后重新调用onDraw()方法,在onDraw中重置效果是一样的。
大约 3 年之前 回复

path没有重置,你为什么要把mPaint.reset();注释调,打开就行拉

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//重置路径
// mPaint.reset();
if (mState == MotionEvent.ACTION_DOWN) {
return;
}
//起点
mPath.moveTo(startPoint.x, startPoint.y);
//
mPath.quadTo(assistPoint.x, assistPoint.y, endPoint.x, endPoint.y);
//画路径
canvas.drawPath(mPath, mPaint);
//画辅助点
canvas.drawPoint(assistPoint.x, assistPoint.y, mPaint);
}

private int mState = -1;

@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mPath.reset();
            mState = MotionEvent.ACTION_DOWN;
            break;
        case MotionEvent.ACTION_MOVE:
            assistPoint.x = (int) event.getX();
            assistPoint.y = (int) event.getY();
            mState = MotionEvent.ACTION_MOVE;
            break;
    }
    invalidate();
    return true;

}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问