Question

I have made a Simple drawing application in android for the learning purpose..In that i have taken diffrent colorbuttons just like a colorpicker in horizontalscrollview,Now i need is when one of them is clicked that particular color should be chosen and pencolor od drawing pen should be changed..I have tried as below,but its not working..Please help me for the same,Thanx in advance...! 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));
    }



}

Please help me friends..please...

Était-ce utile?

La solution

still a bit unclear but I will try to give you a direction. What happens if you try the below onDraw method? I have a feeling you have not set the colors. The code is a bit messy and not clear to read. Don't worry for now about the performance, creating a new paint every time, just want to make sure it give's you the wanted result.

@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);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top