2013-10-17 150 views
0

我遇到問題。目的是在畫布上畫筆畫並得到它,但是當我製作doubleTap時,我繪製的筆畫採用了我選擇的顏色,並且我並不希望第一筆畫的顏色與第二筆畫的顏色相同另一種顏色。更改畫布顏色Android

這裏我的代碼:

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

    } 
} 

請告訴我,我在做什麼榮,請。

回答

0

您從不將路徑指定給您的ArrayLists。您只需一遍又一遍地延長path,然後用paint對其進行繪製,然後在雙擊後進行修改。你的``新聞``具有零點的路徑,因此它們永遠不會被繪製。

您應該製造DrawingPath類型的path,並將其添加到相應方法中的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的需要。

請看看你真正瞭解現在發生在線路83ff48ff

+0

的目標是用不同的顏色繪製。例如,在開始雙擊之前,你畫一條線,顏色是黑色。當您雙擊時,您將顏色更改爲綠色,但不希望先前變爲綠色,但保留以前的顏色。而在我的代碼中,當我雙擊水龍頭變成綠色時,我繪製了。 –

+0

我現在添加了代碼。我無法測試它,但它應該可以工作。如果沒有,只要提到會發生什麼。 – chuck258