如何在android中点击按钮点击笔的颜色

时间:2013-07-24 10:02:20

标签: android android-canvas

我为singletouch绘制了一个简单的android程序,其中我有4种不同大小的笔,我想要的是基于该图像我想改变笔的笔画。我试过如下,请帮帮我:

My image screen shot

main.java

public class MainActivity extends Activity implements OnClickListener {
    ImageView pen, color;
    SingleTouchView mDrawView;
    RelativeLayout layout, layout1;
    ImageView remove;
    ImageView eraser;
    LinearLayout pens;
    HorizontalScrollView myplate;
    private Paint mPaint, mBitmapPaint;
    private Button red, green, blue, cyan, yellow, orange, aqua, black, cofee,
            fuchiya, gray, indigo, khaki, lavendar, magenta, mango, maroon,
            pista, pink, purple;

    private Path mPath;

    public Canvas mCanvas;
    LinearLayout pen1, pen2, pen3, pen4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDrawView = (SingleTouchView) findViewById(R.id.myview);
        layout1 = (RelativeLayout) findViewById(R.id.layout1);
        layout = (RelativeLayout) findViewById(R.id.layout);
        pen = (ImageView) findViewById(R.id.pen);
        pens = (LinearLayout) findViewById(R.id.linear);
        pens.setVisibility(View.GONE);
        pen1 = (LinearLayout) findViewById(R.id.pen1);
        pen2 = (LinearLayout) findViewById(R.id.pen2);
        pen3 = (LinearLayout) findViewById(R.id.pen3);
        pen4 = (LinearLayout) findViewById(R.id.pen4);
        color = (ImageView) findViewById(R.id.color);
        myplate = (HorizontalScrollView) findViewById(R.id.myplate);

        // colors.........!!!!
        red = (Button) findViewById(R.id.red);
        green = (Button) findViewById(R.id.green);
        blue = (Button) findViewById(R.id.blue);
        orange = (Button) findViewById(R.id.orange);
        yellow = (Button) findViewById(R.id.yellow);
        cyan = (Button) findViewById(R.id.cyan);
        black = (Button) findViewById(R.id.black);
        cofee = (Button) findViewById(R.id.cofee);
        fuchiya = (Button) findViewById(R.id.fuchiya);
        gray = (Button) findViewById(R.id.gray);
        indigo = (Button) findViewById(R.id.indigo);
        khaki = (Button) findViewById(R.id.khaki);
        lavendar = (Button) findViewById(R.id.lavendar);
        magenta = (Button) findViewById(R.id.magenta);
        mango = (Button) findViewById(R.id.mango);
        maroon = (Button) findViewById(R.id.maroon);
        pista = (Button) findViewById(R.id.pista);
        pink = (Button) findViewById(R.id.pink);
        purple = (Button) findViewById(R.id.purple);

        red.setOnClickListener(this);
        green.setOnClickListener(this);
        blue.setOnClickListener(this);
        orange.setOnClickListener(this);
        yellow.setOnClickListener(this);
        cyan.setOnClickListener(this);
        black.setOnClickListener(this);
        cofee.setOnClickListener(this);
        fuchiya.setOnClickListener(this);
        gray.setOnClickListener(this);
        indigo.setOnClickListener(this);
        khaki.setOnClickListener(this);
        lavendar.setOnClickListener(this);
        magenta.setOnClickListener(this);
        mango.setOnClickListener(this);
        maroon.setOnClickListener(this);
        pista.setOnClickListener(this);
        pink.setOnClickListener(this);
        purple.setOnClickListener(this);

        eraser = (ImageView) findViewById(R.id.eraser);
        remove = (ImageView) findViewById(R.id.remove);

        pen.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // layout.addView(mDrawView);
                pens.setVisibility(View.VISIBLE);
            }
        });

        pens.setVisibility(View.GONE);

        pen1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawView.setPen(SingleTouchView.DrawingPens.PEN_1);
                pens.setVisibility(View.GONE);
            }
        });

        pen2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawView.setPen(SingleTouchView.DrawingPens.PEN_2);
                pens.setVisibility(View.GONE);
            }
        });

        pen3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                pens.setVisibility(View.GONE);
                mDrawView.setPen(SingleTouchView.DrawingPens.PEN_3);
            }
        });

        pen4.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                pens.setVisibility(View.GONE);
                mDrawView.setPen(SingleTouchView.DrawingPens.PEN_4);
            }
        });

        remove.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                layout.removeView(mDrawView);
                mDrawView = new SingleTouchView(MainActivity.this);
                layout.addView(mDrawView);
            }
        });

        eraser.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mBitmapPaint.setColor(Color.TRANSPARENT);
                mPath.reset();
                mDrawView.invalidate();
                }
        });

        color.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                myplate.setVisibility(View.VISIBLE);
            }
        });
    }

    // /colorpicker................!
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.black:
            myplate.setVisibility(View.GONE);
            mDrawView.setColor(SingleTouchView.DrawingColors.Black);
            System.out.println("==============="+SingleTouchView.DrawingColors.Black+"=========================");
            break;
        case R.id.blue:
            myplate.setVisibility(View.GONE);
            //mDrawView.setColor(SingleTouchView.DrawingColors.Blue);
            break;
        case R.id.cofee:
            myplate.setVisibility(View.GONE);
            break;
        case R.id.cyan:
            myplate.setVisibility(View.GONE);
            break;
        case R.id.fuchiya:
            myplate.setVisibility(View.GONE);
            break;
        case R.id.gray:
            myplate.setVisibility(View.GONE);
            break;
        case R.id.green:
            myplate.setVisibility(View.GONE);

            break;
        case R.id.indigo:
            myplate.setVisibility(View.GONE);
            break;
        case R.id.khaki:
            myplate.setVisibility(View.GONE);
            break;
        case R.id.lavendar:
            myplate.setVisibility(View.GONE);
            break;
        case R.id.magenta:
            myplate.setVisibility(View.GONE);
            break;
        case R.id.mango:
            myplate.setVisibility(View.GONE);
            break;
        case R.id.maroon:
            myplate.setVisibility(View.GONE);
            break;
        case R.id.orange:
            myplate.setVisibility(View.GONE);
            break;
        case R.id.pink:
            myplate.setVisibility(View.GONE);
            break;
        case R.id.pista:
            myplate.setVisibility(View.GONE);
            break;
        case R.id.purple:
            myplate.setVisibility(View.GONE);
            break;
        case R.id.red:
            myplate.setVisibility(View.GONE);

            break;
        case R.id.yellow:
            myplate.setVisibility(View.GONE);
            break;
        }
    }
}

Singleotuch.java

public class SingleTouchView extends View {
    public static int width;
    public int height;
    public Bitmap mBitmap;
    public Canvas mCanvas;
    public Path mPath;
    public Paint mBitmapPaint;
    Context context;
    public Paint mPaint;
    public Paint circlePaint;
    public Path circlePath;

    public enum DrawingPens {
        PEN_1(6), PEN_2(4), PEN_3(2), PEN_4(1);

        public Paint mPaint;

        private DrawingPens(final int width) {
            mPaint = new Paint();

            mPaint.setAntiAlias(true);
            mPaint.setStrokeWidth(width);

            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
        }

        Paint getPaint() {
            return mPaint;
        }
    }

    public enum DrawingColors{
        Black(Color.parseColor("#000000")),Blue(Color.parseColor("#0000FF")),Cofee(Color.parseColor("#D2691E")),Cyan(Color.parseColor("#00FFFF"))
        ,Fuchiya(Color.parseColor("#FF00FF")),Gray(Color.parseColor("#808080")),Green(Color.parseColor("#00FF00")),Indigo(Color.parseColor("#4B0082")),
        Khaki(Color.parseColor("#F0E68C")),Lavendar(Color.parseColor("#E6E6FA")),Magenta(Color.parseColor("#FF00FF")),Mango(Color.parseColor("#FF8C00"))
        ,Maroon(Color.parseColor("#800000")),Orange(Color.parseColor("#FFA500")),Pink(Color.parseColor("#FFC0CB")),Pista(Color.parseColor("#9ACD32")),
        Purple(Color.parseColor("#800080")),Red(Color.parseColor("#FF0000")),Tan(Color.parseColor("#0000A0")),Yellow(Color.parseColor("#FFD801"));

        public Paint mPaint;

        private DrawingColors(final int color) {
            mPaint = new Paint();

            mPaint.setAntiAlias(true);
            mPaint.setStrokeWidth(width);
            mPaint.setColor(color);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
        }

        Paint getPaint() {
            return mPaint;
        }
    }

    public SingleTouchView(final Context context) {
        super(context);

        init(context);
    }

    public SingleTouchView(final Context context, final AttributeSet attrs) {
        super(context, attrs);

        init(context);
        mBitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);
    }

    public SingleTouchView(final Context context, final AttributeSet attrs,
            final int defStyle) {
        super(context, attrs, defStyle);

        init(context);
    }

    private ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>> mPaths = new ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>>();
    private ConcurrentLinkedQueue<Map.Entry<Path, DrawingColors>> mPaths1 = new ConcurrentLinkedQueue<Map.Entry<Path, DrawingColors>>();

    private Path mCurrentPath;


    private void init(final Context context) {
        setPen(DrawingPens.PEN_1);
    }

    @Override
    public void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        for (Map.Entry<Path, DrawingPens> entry : mPaths) {
            canvas.drawPath(entry.getKey(), entry.getValue().getPaint());
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent me) {
        float eventX = me.getX();
        float eventY = me.getY();

        switch (me.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mCurrentPath.moveTo(eventX, eventY);
            return true;
        case MotionEvent.ACTION_MOVE:
            mCurrentPath.lineTo(eventX, eventY);
            break;
        case MotionEvent.ACTION_UP:
            break;
        }

        invalidate();

        return true;
    }

    public void setPen(final DrawingPens pen) {

        mCurrentPath = new Path();
        mPaths.add(new AbstractMap.SimpleImmutableEntry<Path, DrawingPens>(
                mCurrentPath, pen));
    }
    public void eraser() {
        mPaint = new Paint();
    }
    public void setColor(final DrawingColors color ) {

        mCurrentPath = new Path();
        mPaths1.add(new AbstractMap.SimpleImmutableEntry<Path, DrawingColors>(
                mCurrentPath, color));
    }
}

1 个答案:

答案 0 :(得分:1)

似乎无法仅使用一个Path绘制Paint的一部分,而使用另一个绘制另一部分。所以,我看到唯一的解决方案 - 使用多个Paths并将路径指向Paint。另外,我建议在视图中封装所有与绘图相关的信息,并让Activity以高级操作进行操作。

因此应该应用以下更改:

  1. Path使用一些容器并在其中保留Path - Paint关系;
  2. 从自定义View提供一些方法,让Activity更改当前的Pen;
  3. 在自定义视图中保留绘图详细信息部分,因为Activity可能不需要这些详细信息;
  4. 如下所示:

    public class MyActivity extends Activity implements View.OnClickListener {
    
        private static final String TAG = "MyActivity";
    
        ImageView pen, color;
        SingleTouchDrawingView mDrawView;
        // Drawing view container
        ImageView mRemove;
        ImageView mEraser;
        LinearLayout pens;
        HorizontalScrollView myplate;
        private Paint mPaint;
        private Paint mBitmapPaint;
        private View aqua;
        private ViewGroup mRootView;
    
        private Path mPath;
        public Canvas mCanvas;
        View pen1;
        View pen2;
        View pen3;
        View pen4;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mRootView = (ViewGroup) findViewById(R.id.root);
            mDrawView = (SingleTouchDrawingView) findViewById(R.id.myview);
            pen = (ImageView) findViewById(R.id.pen);
            pens = (LinearLayout) findViewById(R.id.linear);
            pens.setVisibility(View.GONE);
            pen1 = findViewById(R.id.pen1);
            pen2 = findViewById(R.id.pen2);
            pen3 = findViewById(R.id.pen3);
            pen4 = findViewById(R.id.pen4);
            color = (ImageView) findViewById(R.id.color);
            myplate = (HorizontalScrollView) findViewById(R.id.myplate);
    
            // colors.........!!!!
            final View red = findViewById(R.id.red);
            final View green = findViewById(R.id.green);
            final View blue = findViewById(R.id.blue);
            final View orange = findViewById(R.id.orange);
            final View yellow = findViewById(R.id.yellow);
            final View cyan = findViewById(R.id.cyan);
            final View black = findViewById(R.id.black);
            final View cofee = findViewById(R.id.coffee);
            final View fuchiya = findViewById(R.id.fuchiya);
            final View gray = findViewById(R.id.gray);
            final View indigo = findViewById(R.id.indigo);
            final View khaki = findViewById(R.id.khaki);
            final View lavendar = findViewById(R.id.lavendar);
            final View magenta = findViewById(R.id.magenta);
            final View mango = findViewById(R.id.mango);
            final View maroon = findViewById(R.id.maroon);
            final View pista = findViewById(R.id.pista);
            final View pink = findViewById(R.id.pink);
            final View purple = findViewById(R.id.purple);
    
            red.setOnClickListener(this);
            green.setOnClickListener(this);
            blue.setOnClickListener(this);
            orange.setOnClickListener(this);
            yellow.setOnClickListener(this);
            cyan.setOnClickListener(this);
            black.setOnClickListener(this);
            cofee.setOnClickListener(this);
            fuchiya.setOnClickListener(this);
            gray.setOnClickListener(this);
            indigo.setOnClickListener(this);
            khaki.setOnClickListener(this);
            lavendar.setOnClickListener(this);
            magenta.setOnClickListener(this);
            mango.setOnClickListener(this);
            maroon.setOnClickListener(this);
            pista.setOnClickListener(this);
            pink.setOnClickListener(this);
            purple.setOnClickListener(this);
    
            mEraser = (ImageView) findViewById(R.id.eraser);
            mRemove = (ImageView) findViewById(R.id.remove);
    
            pen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (pens.getVisibility() == View.VISIBLE) {
                        pens.setVisibility(View.GONE);
                    } else {
                        pens.setVisibility(View.VISIBLE);
                        myplate.setVisibility(View.GONE);
                    }
                }
            });
    
            pens.setVisibility(View.GONE);
    
            pen1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDrawView.setPen(SingleTouchDrawingView.DrawingPens.PEN_1);
                    pens.setVisibility(View.GONE);
                }
            });
    
            pen2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDrawView.setPen(SingleTouchDrawingView.DrawingPens.PEN_2);
                    pens.setVisibility(View.GONE);
                }
            });
    
            pen3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pens.setVisibility(View.GONE);
                    mDrawView.setPen(SingleTouchDrawingView.DrawingPens.PEN_3);
                }
            });
    
            pen4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pens.setVisibility(View.GONE);
                    mDrawView.setPen(SingleTouchDrawingView.DrawingPens.PEN_4);
                }
            });
    
            mRemove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mRootView.removeView(mDrawView);
                    mDrawView = new SingleTouchDrawingView(MyActivity.this);
                    mDrawView.setBackgroundResource(R.drawable.up_image);
                    mRootView.addView(mDrawView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                    mRootView.bringChildToFront(findViewById(R.id.tools_container));
                    mRootView.bringChildToFront(findViewById(R.id.linear));
                    mRootView.bringChildToFront(findViewById(R.id.myplate));
                    pens.setVisibility(View.GONE);
                    myplate.setVisibility(View.GONE);
                }
            });
    
            mEraser.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mBitmapPaint.setColor(Color.TRANSPARENT);
                    mPath.reset();
                    mDrawView.invalidate();
                    pens.setVisibility(View.GONE);
                    myplate.setVisibility(View.GONE);
                }
            });
    
            color.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (myplate.getVisibility() == View.VISIBLE) {
                        myplate.setVisibility(View.GONE);
                    } else {
                        myplate.setVisibility(View.VISIBLE);
                        pens.setVisibility(View.GONE);
                    }
                }
            });
        }
    
        // /colorpicker................!
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.black:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.BLACK);
                    break;
                case R.id.blue:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.BLUE);
                    break;
                case R.id.coffee:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.COFEE);
                    break;
                case R.id.cyan:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.CYAN);
                    break;
                case R.id.fuchiya:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.FUCHIYA);
                    break;
                case R.id.gray:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.GRAY);
                    break;
                case R.id.green:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.GREEN);
                    break;
                case R.id.indigo:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.INDIGO);
                    break;
                case R.id.khaki:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.KHAKI);
                    break;
                case R.id.lavendar:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.LAVENDAR);
                    break;
                case R.id.magenta:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.MAGENTA);
                    break;
                case R.id.mango:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.MANGO);
                    break;
                case R.id.maroon:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.MAROON);
                    break;
                case R.id.orange:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.ORANGE);
                    break;
                case R.id.pink:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.PINK);
                    break;
                case R.id.pista:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.PISTA);
                    break;
                case R.id.purple:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.PURPLE);
                    break;
                case R.id.red:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.RED);
                    break;
                case R.id.yellow:
                    myplate.setVisibility(View.GONE);
                    mDrawView.setColor(SingleTouchDrawingView.DrawingColors.YELLOW);
                    break;
            }
        }
    }
    

    并观察自己:

    public class SingleTouchDrawingView extends View {
    
        public int height;
        public Bitmap mBitmap;
        public Canvas mCanvas;
        public Path mPath;
        public Paint mBitmapPaint;
        Context context;
    
        public enum DrawingPens {
            PEN_1(6), PEN_2(4), PEN_3(2), PEN_4(1);
    
            public int mWidth;
    
            private DrawingPens(final int width) {
                mWidth = width;
            }
    
            int getPenWidth() {
                return mWidth;
            }
        }
    
        public enum DrawingColors{
            BLACK(Color.parseColor("#000000")),   BLUE(Color.parseColor("#0000FF")),     COFEE(Color.parseColor("#D2691E")),   CYAN(Color.parseColor("#00FFFF")),
            FUCHIYA(Color.parseColor("#FF00FF")), GRAY(Color.parseColor("#808080")),     GREEN(Color.parseColor("#00FF00")),   INDIGO(Color.parseColor("#4B0082")),
            KHAKI(Color.parseColor("#F0E68C")),   LAVENDAR(Color.parseColor("#E6E6FA")), MAGENTA(Color.parseColor("#FF00FF")), MANGO(Color.parseColor("#FF8C00")),
            MAROON(Color.parseColor("#800000")),  ORANGE(Color.parseColor("#FFA500")),   PINK(Color.parseColor("#FFC0CB")),    PISTA(Color.parseColor("#9ACD32")),
            PURPLE(Color.parseColor("#800080")),  RED(Color.parseColor("#FF0000")),      TAN(Color.parseColor("#0000A0")),     YELLOW(Color.parseColor("#FFD801"));
    
            private int mColorValue;
    
            private DrawingColors(final int color) {
                mColorValue = color;
            }
    
            int getColorValue() {
                return mColorValue;
            }
        }
    
        public SingleTouchDrawingView(final Context context) {
            super(context);
    
            init(context);
        }
    
        public SingleTouchDrawingView(final Context context, final AttributeSet attrs) {
            super(context, attrs);
    
            init(context);
            mBitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        }
    
        public SingleTouchDrawingView(final Context context,
                                      final AttributeSet attrs,
                                      final int defStyle) {
            super(context, attrs, defStyle);
    
            init(context);
        }
    
        private ConcurrentLinkedQueue<Map.Entry<Path, Map.Entry<DrawingPens, DrawingColors>>> mPaths = new ConcurrentLinkedQueue<Map.Entry<Path, Map.Entry<DrawingPens, DrawingColors>>>();
        /** Stores created {@link Paint} objects */
        private HashMap<Map.Entry<DrawingPens, DrawingColors>, Paint> mPaints = new HashMap<Map.Entry<DrawingPens, DrawingColors>, Paint>();
        /** Current paint parameters - PEN_1 width and BLACK color */
        private AbstractMap.SimpleImmutableEntry<DrawingPens, DrawingColors> mCurrentPaintParameters = new AbstractMap.SimpleImmutableEntry<DrawingPens, DrawingColors>(DrawingPens.PEN_1, DrawingColors.BLACK);
    
        private Path mCurrentPath = null;
    
    
        private void init(final Context context) {
            // Initialize default paint
            getPaintWithParameters(mCurrentPaintParameters);
            mCurrentPath = new Path();
            mPaths.add(new AbstractMap.SimpleImmutableEntry<Path, Map.Entry<DrawingPens, DrawingColors>>(mCurrentPath, mCurrentPaintParameters));
        }
    
        /**
         * Helper to create Paint from parameters
         *
         * @param paintParameters
         * @return
         */
        private Paint getPaintWithParameters(final Map.Entry<DrawingPens, DrawingColors> paintParameters) {
            Paint paint = mPaints.get(paintParameters);
    
            if (null == paint) {
                paint = new Paint();
                paint.setAntiAlias(true);
                paint.setDither(true);
                paint.setStrokeWidth(paintParameters.getKey().getPenWidth());
                paint.setColor(paintParameters.getValue().getColorValue());
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeJoin(Paint.Join.ROUND);
                paint.setStrokeCap(Paint.Cap.ROUND);
    
                mPaints.put(paintParameters, paint);
            }
    
            return paint;
        }
    
        @Override
        public void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            for (Map.Entry<Path, Map.Entry<DrawingPens, DrawingColors>> entry : mPaths) {
                canvas.drawPath(entry.getKey(), getPaintWithParameters((entry.getValue())));
            }
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent me) {
            float eventX = me.getX();
            float eventY = me.getY();
    
            switch (me.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mCurrentPath.moveTo(eventX, eventY);
                    return true;
                case MotionEvent.ACTION_MOVE:
                    mCurrentPath.lineTo(eventX, eventY);
                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }
    
            invalidate();
    
            return true;
        }
    
        public void setPen(final DrawingPens pen) {
            mCurrentPath = new Path();
    
            mCurrentPaintParameters = new AbstractMap.SimpleImmutableEntry<DrawingPens, DrawingColors>(pen, mCurrentPaintParameters.getValue());
    
            mPaths.add(new AbstractMap.SimpleImmutableEntry<Path, Map.Entry<DrawingPens, DrawingColors>>(mCurrentPath, mCurrentPaintParameters));
        }
    
        public void eraser() {
            // Get default paint parameters
            mCurrentPaintParameters = new AbstractMap.SimpleImmutableEntry<DrawingPens, DrawingColors>(DrawingPens.PEN_1, DrawingColors.BLACK);
        }
    
        public void setColor(final DrawingColors color ) {
            mCurrentPath = new Path();
    
            mCurrentPaintParameters = new AbstractMap.SimpleImmutableEntry<DrawingPens, DrawingColors>(mCurrentPaintParameters.getKey(), color);
    
            mPaths.add(new AbstractMap.SimpleImmutableEntry<Path, Map.Entry<DrawingPens, DrawingColors>>(mCurrentPath, mCurrentPaintParameters));
        }
    }
    

    更新(有自定义颜色/宽度):

    您的代码问题在于代码不一致以及对Java Collections如何工作的错误理解。您不需要mPaths1额外的集合,但应该对所有绘图使用相同的集合并维护其他结构(在我们的例子中为Map.Entry<DrawingPens, DrawingColors>),这可以识别确切的Paint并使用与路径集合。有关集合的更多详细信息,我建议从Oracle Trials开始。