0

我有个问题。目标是在画布上绘制笔画并获取它,但是当我让 doubleTap 绘制的笔画采用我选择的颜色时,我不希望第一个笔画的颜色仍然与第二个笔画相同另一种颜色。

这是我的代码:

package com.example.guiao_2_1;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class NewView extends View implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{
    private Paint paint = new Paint();
    private Path path = new Path();
    private DrawingPath newPath;
    private GestureDetectorCompat mDetector;
     private static final String DEBUG_TAG = "Gestures";
     private DrawingPath oldPath;
     private ArrayList<DrawingPath> olds = new ArrayList<DrawingPath>();
     private ArrayList<DrawingPath> news = new ArrayList<DrawingPath>();

    public NewView(Context context, AttributeSet attrs) {
        super(context, attrs);

        paint.setAntiAlias(true);
        paint.setStrokeWidth(6f);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);

        mDetector = new GestureDetectorCompat(context, this);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawPath(path, paint);

        for (DrawingPath p : olds){
            paint.setColor(p.color); 
            paint.setStrokeWidth(p.width);
            canvas.drawPath(p, paint);
        }

        for (DrawingPath j : news){
            paint.setColor(j.color); 
            paint.setStrokeWidth(j.width);
            canvas.drawPath(j, paint);
        }
        //canvas.drawPath(path, paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        this.mDetector.onTouchEvent(event);
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            return true;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(eventX, eventY);
            break;
        case MotionEvent.ACTION_UP:
            // nothing to do
                break;
        default:
            return false;
        }

        // Schedules a repaint.
        invalidate();
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        // TODO Auto-generated method stub  

        float width = paint.getStrokeWidth();
        int color = paint.getColor();

        oldPath = new DrawingPath(width, color);
        olds.add(oldPath);

        newPath = new DrawingPath(8, Color.GREEN);
        news.add(newPath);



        //int numColor = paint.getColor();
        //String color = Integer.toHexString(numColor);
        //Toast.makeText(getContext(), color, Toast.LENGTH_SHORT).show();

        Log.d(DEBUG_TAG, "onDoubleTap: " + e.toString());
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
        return false;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    private class DrawingPath extends Path {

        public float width;
        public int color;

        public DrawingPath(float w, int c){
            width = w;
            color = c;
        }

    }
}

请告诉我我在做什么,请。

4

1 回答 1

0

你永远不会将你的路径分配给你的 ArrayLists。您只需path一遍又一遍地扩展并绘制它,paint双击后会对其进行修改。您的“òldsand新闻”中有零点路径,因此这些路径永远不会被绘制。

您应该制作path类型DrawingPath并将其添加到ArrayLists相应的方法中。

package com.example.guiao_2_1;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class NewView extends View implements GestureDetector.OnGestureListener,
        GestureDetector.OnDoubleTapListener {
    private Paint oldsPaint = new Paint();
    private Paint newsPaint = new Paint();
    private Path path = new Path();
    private Path newPath;
    private GestureDetectorCompat mDetector;
    private static final String DEBUG_TAG = "Gestures";
    private Path oldPath;
    private ArrayList<Path> olds = new ArrayList<Path>();
    private ArrayList<Path> news = new ArrayList<Path>();
    private boolean hasDoubleTapped;

    public NewView(Context context, AttributeSet attrs) {
        super(context, attrs);

        oldsPaint.setAntiAlias(true);
        oldsPaint.setStrokeWidth(6f);
        oldsPaint.setColor(Color.BLACK);
        oldsPaint.setStyle(Paint.Style.STROKE);
        oldsPaint.setStrokeJoin(Paint.Join.ROUND);

        newsPaint.set(oldsPaint);
        newsPaint.setColor(Color.GREEN);
        newsPaint.setStrokeWidth(8f);

        mDetector = new GestureDetectorCompat(context, this);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (hasDoubleTapped) {
            canvas.drawPath(path, oldsPaint);
        } else {
            canvas.drawPath(path, newsPaint);
        }

        for (Path p : olds) {

            canvas.drawPath(p, oldsPaint);
        }

        for (Path j : news) {

            canvas.drawPath(j, newsPaint);
        }
        // canvas.drawPath(path, paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        this.mDetector.onTouchEvent(event);
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            return true;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(eventX, eventY);
            break;
        case MotionEvent.ACTION_UP:
            if (hasDoubleTapped) {
                news.add(path);
            } else {
                olds.add(path);
            }
            break;
        default:
            return false;
        }

        // Schedules a repaint.
        invalidate();
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        // TODO Auto-generated method stub
        hasDoubleTapped = true;

        // int numColor = paint.getColor();
        // String color = Integer.toHexString(numColor);
        // Toast.makeText(getContext(), color, Toast.LENGTH_SHORT).show();

        Log.d(DEBUG_TAG, "onDoubleTap: " + e.toString());
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
        return false;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
}

我现在所做的是:我删除了 Class DrawingPath,因为它没用。然后,我在案例中添加了一些逻辑,MotionEvent.ACTION_UP:以将路径添加到数组中,这是您迄今为止从未做过的事情。我还添加了一个额外的内容Paint以避免需要派生的 DrawingPath。

请看你是否真的理解现在在 Lines 中发生的事情83ff48ff

于 2013-10-17T21:31:47.540 回答