0

在 onTouch 下 else if (event.getAction() == MotionEvent.ACTION_MOVE) 条件无效不起作用。实际上,一旦用户在屏幕上移动手指,我就想显示线条。

public class Custom_View extends ImageView implements View.OnTouchListener {

    ArrayList<ArrayList<Point>> mPathPoints = null;
    Path mPath = null;
    Paint mPaint = null;
    GestureDetector mDetector = null;
    private Context mContext = null;

    public Custom_View(Context context) {
        super(context);
        inIt(context);
    }

    public Custom_View(Context context, AttributeSet attrs) {
        super(context, attrs);
        inIt(context);
    }

    public Custom_View(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inIt(context);
    }

    private void inIt(Context mContext) {
        this.mContext = mContext;
        mPathPoints = new ArrayList<ArrayList<Point>>();
        mPath = new Path();
        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(3);
        mPaint.setColor(Color.RED);
        setOnTouchListener(this);

    }

    Point mPrePoint = null;
    Point mCurPoint = null;

    private ArrayList<Point> mDemage = null;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.YELLOW);
        Log.i("Size ", mPathPoints.size() + "");

        for (ArrayList<Point> mDemage : mPathPoints) {
            for (Point point : mDemage) {
                if (mPrePoint == null) {
                    canvas.drawPoint(point.x, point.y, mPaint);
                    mPrePoint = point;
                } else {
                    canvas.drawLine(mPrePoint.x, mPrePoint.y, point.x, point.y,
                            mPaint);
                    mPrePoint = point;
                }
            }
            mPrePoint = null;
        }
    }

    private Point lastPoint = null;

    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mDemage = new ArrayList<Point>();
            lastPoint = new Point((int) event.getX(), (int) event.getY());
            mDemage.add(lastPoint);
            Custom_View.this.invalidate();
            Toast.makeText(mContext, "ACTION_DOWN", Toast.LENGTH_SHORT).show();

        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                lastPoint = new Point((int) event.getX(), (int) event.getY());
                mDemage.add(lastPoint);
            this.invalidate();
            Toast.makeText(mContext, "ACTION_MOVE", Toast.LENGTH_SHORT).show();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            mDemage.add(new Point((int) event.getX(), (int) event.getY()));
            mPathPoints.add(mDemage);
            mDemage = null;
            lastPoint = null;
            Custom_View.this.invalidate();
            Toast.makeText(mContext, "ACTION_UP", Toast.LENGTH_SHORT).show();

        }

        return true;
    }

}
4

0 回答 0