第一条路径没有画

时间:2017-07-12 14:02:48

标签: java android image draw custom-view

我制作了一个自定义视图来绘制位图。问题是当试图画第一次触摸时没有显示任何东西并且颜色也在下次改变了,第二个问题是当我保存图像时没有任何东西从彩绘的颜色中显示

这是我的代码:

public class DrawView extends android.support.v7.widget.AppCompatImageView {
    private ArrayList<ColouredPoint> paths ;
    private ColouredPoint mPath;
    private Paint mPaint;
    private static final float TOUCH_TOLERANCE = 4;
    boolean erase;
    // Current used colour
    private int mCurrColour;
    public void setErase(boolean era){
        erase=era;
    }
    public void setColor(int colour) {
        mCurrColour = colour;
    }

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

    // XML constructor
    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paths = new ArrayList<>();
        mPaint = new Paint();
        mPath = new ColouredPoint(mCurrColour);
        mPaint.setColor(mCurrColour);
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(15);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
    }
    private float mX, mY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Capture a reference to each touch for drawing
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPath.reset();
                mPath.moveTo(x, y);
                mX = x;
                mY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = Math.abs(x - mX);
                float dy = Math.abs(y - mY);
                if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                    mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
                    mX = x;
                    mY = y;}
                break;
            case MotionEvent.ACTION_UP:
                mPath.lineTo(mX, mY);
                mPath = new ColouredPoint(mCurrColour);
                paths.add(mPath);
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas c) {
        // Let the image be drawn first
        super.onDraw(c);
        // Draw your custom points here
        if(!erase){
            for (ColouredPoint i:paths) {
                mPaint.setColor(i.colour);
                c.drawPath(i, mPaint);}}
        else if (paths.size()>0){
            paths.remove(paths.size()-1);

        } else {
            Toast.makeText(getContext(), "Undo unknown", Toast.LENGTH_SHORT)
                    .show();}}
    /**
     * Class to store the coordinate and the colour of the point.
     */
    private class ColouredPoint extends Path{
        int colour;

        public ColouredPoint(int colour) {

            this.colour = colour;
        }
    }
}

如何解决这个问题?

0 个答案:

没有答案