u010825838
2017-07-24 09:28
采纳率: 33.3%
浏览 1.3k

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条回答 默认 最新

  • 昭杏之念 2017-07-24 11:13
    已采纳

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

    评论
    解决 无用
    打赏 举报
查看更多回答(3条)

相关推荐 更多相似问题