u010825838
u010825838
采纳率33.3%
2017-07-24 09:28 阅读 1.2k

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

2

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

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

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

    点赞 2 评论 复制链接分享
  • bilionera bilionera 2017-07-24 09:55

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

    点赞 1 评论 复制链接分享
  • zengxianzi 昭杏之念 2017-07-24 11:06

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

    点赞 评论 复制链接分享
  • xw13782513621 江三木洛 2017-07-24 13:00

    @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;
    
    }
    
    点赞 评论 复制链接分享

相关推荐