0

私は学習目的でアンドロイドでシンプルな描画アプリケーションを作成しました..水平スクロールビューのカラーピッカーのように異なるカラーボタンを使用したので、そのうちの1つがクリックされたときに特定の色を選択し、ペンカラーを描画ペンにする必要があります変更する必要があります..私は以下のように試しましたが、うまくいきません..同じことを助けてください、事前に感謝します...! main.java public void onClick(View v) { switch (v.getId()) {

    case R.id.black:
        myplate.setVisibility(View.GONE);
        mDrawView.setColor(SingleTouchView.DrawingColors.Black);

        break;
    case R.id.blue:
        myplate.setVisibility(View.GONE);
        mDrawView.setColor(SingleTouchView.DrawingColors.Blue);

        break;
    ...so on...for other colors

MyView.java

   package com.example.singletouch;

import java.util.AbstractMap;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

import android.R.color;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

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() {
        // TODO Auto-generated method stub
          mPaint = new Paint();

           /* Toast.makeText(getContext(), "eraser", Toast.LENGTH_LONG).show();
            mPaint.setXfermode(null);
            mPaint.setAlpha(0x00FFFFFF);
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));*/
       // invalidate();
    }
    public void setColor(final DrawingColors color ) {

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



}

友達を助けてください..お願いします...

4

1 に答える 1

0

まだ少し不明確ですが、私はあなたに方向性を与えようとします. 以下の onDraw メソッドを試すとどうなりますか? 色を設定していない気がします。コードは少し乱雑で、読みにくいです。今のところ、パフォーマンスについて心配する必要はありません。毎回新しいペイントを作成します。希望する結果が得られることを確認したいだけです。

@Override
public void onDraw(Canvas canvas) {

    super.onDraw(canvas);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);

    for (Map.Entry<Path, DrawingPens> entry : mPaths) {
        canvas.drawPath(entry.getKey(), paint);
    }
}
于 2013-07-26T13:31:22.893 に答える