2013-07-26 25 views
0

我已經在android中做了一個簡單的繪圖應用程序的學習目的..因爲我已經採取了不同的顏色按鈕就像horizo​​ntalscrollview中的colorpicker,現在我需要的是當他們之一被點擊那個特定應該選擇顏色和pencolor od繪圖筆應該改變..我已經嘗試了下面,但它不工作..請幫助我一樣,Thanx提前...! main.java 公共無效的onClick(視圖v){ 開關(v.getId()){定製colorpicker不工作在android

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)); 
    } 



} 

請幫助我的朋友..請...

+0

嗨,也許發佈一些更相關的代碼。如onDraw方法。問題是有點不清楚考慮改善問題太有用的資源 – QVDev

+0

@ QVDev-當然兄弟...等我發佈我的整個代碼視圖和活動...好吧,..以便您可以更好地瞭解我的問題..我編輯了我的代碼審查它。 – jigar

回答

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); 
    } 
}