2013-08-27 113 views
1

我使用下面給出的代碼繪製了基於用戶手指移動的多邊形路徑。更改已繪製的多邊形路徑的顏色

paint = new Paint(); 
    strokePaint = new Paint(); 
    //paint.setColor(Color.RED); 
    paint.setARGB(125, 255, 0, 0); 
    paint.setStyle(Style.FILL); 
    paint.setPathEffect(new DashPathEffect(new float[] {10,20}, 0)); 
    paint.setStrokeWidth(5); 
    paint.setAntiAlias(true); 
    wallpath = new Path(); 

和onDraw有我使用這個代碼來繪製圖

wallpath.lineTo(endPoint.x, endPoint.y); 
     canvas.drawPath(wallpath, paint); 

上面的代碼工作正常。但雙擊我想改變填充顏色。爲此,我正在使用此代碼

paint.setARGB(125, 225, 0, 0); 
     paint.setStyle(Style.FILL); 
     paint.setPathEffect(new DashPathEffect(new float[] {10,20}, 0)); 
     paint.setStrokeWidth(5); 
     paint.setAntiAlias(true); 
     invalidate(); 

但它似乎並沒有工作。我怎樣才能正確地做到這一點?參考

public class HotspotView extends View 
{ 

    private Paint paint,strokePaint; 
    private PointF startPoint, endPoint; 
    private boolean isDrawing,isFinished,isAnimating,isRecording,isRedrawing; 
    private Path wallpath; 
    private ArrayList<PointF> points; 
    private RectF rectF; 
    private CurlView curlView; 
    public int imageWidth; 
    private String fileName; 
    private AudioRecorder audioRecorder; 
    private GestureDetector gestureDetector; 
    public static int LONG_PRESS_TIME = 500; // Time in miliseconds 
    private AudioPlayer player; 

    final Handler _handler = new Handler(); 
    Runnable _longPressed = new Runnable() { 
     public void run() { 
      Log.i("info","LongPress"); 
      isRecording = true; 
      isDrawing = false; 
      isRedrawing = true; 
      ///////////////////******************/////////////////////// 
      //paint = new Paint(); 
      //strokePaint = new Paint(); 
      //paint.setColor(Color.RED); 
      paint.setARGB(125, 225, 0, 0); 
      paint.setStyle(Style.FILL); 
      paint.setPathEffect(new DashPathEffect(new float[] {10,20}, 0)); 
      paint.setStrokeWidth(5); 
      paint.setAntiAlias(true); 
      invalidate(); 
      //////////////////*****************//////////////////////// 
      audioRecorder = new AudioRecorder(fileName); 
      setFileName(); 
      audioRecorder.startRecording(fileName); 
     } 
    }; 

    public HotspotView(Context context) 
    { 
     super(context); 
     init(); 
     gestureDetector = new GestureDetector(context, new GestureListener()); 
    } 

    public HotspotView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     init(); 
     gestureDetector = new GestureDetector(context, new GestureListener()); 
    } 
    public HotspotView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
     gestureDetector = new GestureDetector(context, new GestureListener()); 
    } 

    private void init() 
    { 
     isRecording = false; 
     isRedrawing = false; 
     paint = new Paint(); 
     strokePaint = new Paint(); 
     //paint.setColor(Color.RED); 
     paint.setARGB(125, 255, 0, 0); 
     paint.setStyle(Style.FILL); 
     paint.setPathEffect(new DashPathEffect(new float[] {10,20}, 0)); 
     paint.setStrokeWidth(5); 
     paint.setAntiAlias(true); 
     wallpath = new Path(); 
     points = new ArrayList<PointF>(); 
     rectF = new RectF(); 
     rectF.set(-1.7883435f, 1.0f, 1.7883435f, -1.0f); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) 
    { 

     if(isAnimating) 
     { 
      PointF point = this.translate(points.get(0)); 
      if(wallpath == null) 
      { 
       wallpath = new Path(); 
      } 
      wallpath.moveTo(point.x, point.y); 
      isDrawing = false; 
      isFinished = false; 
      for(int i=1;i<points.size();i++) 
      { 
       if(isRedrawing) 
       { 
        point = points.get(i); 
       } 
       else 
       { 
        point = this.translate(points.get(i)); 
       } 

       wallpath.lineTo(point.x, point.y); 
       //Log.d("Points", "X = "+point.x); 
       //Log.d("Points", "Y = "+point.y); 
       canvas.drawPath(wallpath, paint); 
      } 
      if(isRedrawing) 
      { 
       point = points.get(0); 
      } 
      else 
      { 
       point = this.translate(points.get(0)); 
      } 

      wallpath.lineTo(point.x, point.y); 
      canvas.drawPath(wallpath, paint); 
      isFinished = true; 
     } 
     else if(isDrawing) 
     { 
      //canvas.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, paint); 

      wallpath.lineTo(endPoint.x, endPoint.y); 
      canvas.drawPath(wallpath, paint); 
     } 

     if(isFinished) 
     { 
      //wallpath.lineTo(endPoint.x, endPoint.y); 
      //canvas.drawPath(wallpath, strokePaint); 
      wallpath.close(); 
     } 
    } 


    @Override 
    public boolean onTouchEvent(MotionEvent event) 
    { 
     boolean result = gestureDetector.onTouchEvent(event);//return the double tap events 

     if(!isAnimating) 
     { 

      switch (event.getAction()) 
      { 
       case MotionEvent.ACTION_DOWN: 
        isDrawing = true; 
        //wallpath.reset(); 
        _handler.postDelayed(_longPressed, LONG_PRESS_TIME); 
        startPoint = new PointF(event.getX(), event.getY()); 
        endPoint = new PointF(); 

        endPoint.x = event.getX(); 
        endPoint.y = event.getY(); 
        wallpath.moveTo(endPoint.x,endPoint.y); 
        points.add(startPoint); 
        //invalidate(); 
        break; 
       case MotionEvent.ACTION_MOVE: 
        PointF point = new PointF(event.getX(),event.getY()); 
        endPoint.x = event.getX(); 
        endPoint.y = event.getY(); 
        double distance = Math.sqrt(Math.pow((endPoint.x - startPoint.x), 2)+ Math.pow(endPoint.y - startPoint.y,2)); 
        if(distance >2) 
        { 
         _handler.removeCallbacks(_longPressed); 
         if(!isRecording) 
         { 

          if(isDrawing) 
          { 

           Log.d("Point", "X = "+(event.getX() - this.getLeft())); 
           Log.d("Point", "Y = "+(event.getY() - this.getTop())); 
           points.add(point); 
           invalidate(); 
          } 

         } 
        } 
        break; 
       case MotionEvent.ACTION_UP: 
        _handler.removeCallbacks(_longPressed); 
        if(isRecording) 
        { 
         audioRecorder.stopRecording(); 
         isRecording = false; 
        } 

        if(isDrawing) 
        { 
         endPoint.x = startPoint.x;//event.getX(); 
         endPoint.y = startPoint.y;//event.getY(); 
         strokePaint.setARGB(255, 255, 0, 0); 
         strokePaint.setStyle(Style.STROKE); 
         strokePaint.setPathEffect(new DashPathEffect(new float[] {5,10}, 0)); 
         strokePaint.setStrokeWidth(5); 
         strokePaint.setAntiAlias(true); 
         isFinished = true; 
         invalidate(); 
         //isDrawing = false; 
        } 
        break; 
       default: 
        break; 
      } 
     } 

     return result; 
    } 

回答

1

如果你可以從你的onDraw方法中發佈更多的代碼會很有幫助,因爲很難說什麼時候調用什麼。

我的猜測是invalidate工作正常,但每次調用onDraw時,都會重置您的繪圖設置(paint = new Paint()),因此不會使用不同顏色的繪製。

編輯:

我不能告訴你它的確切行,你有一個bug,但是在我看來,它是不相關的路徑或油漆設置。在你的代碼中有太多的狀態標誌(isDrawing,isFinished,isAnimating,isRecording,isRedrawing),你無法控制它。

的OnDraw方法應該簡單地畫點:

@Override 
protected void onDraw(Canvas canvas) { 
    if (points.size() > 0) { 
     PointF point = points.get(0); 
     wallpath.rewind(); 
     wallpath.moveTo(point.x, point.y); 
     for (int i = 1; i < points.size(); i++) { 
      point = points.get(i); 
      wallpath.lineTo(point.x, point.y); 
     } 
     canvas.drawPath(wallpath, paint); 
    } 
} 

簡化代碼。只是爲了測試:用我的建議來替換你的onDraw方法,你會發現這個路徑在長按時會改變顏色。

+0

我已添加完整的代碼 – Zach

+0

謝謝,這是與其中一個標誌的問題。我有新問題,你可以請檢查這個http://stackoverflow.com/questions/18512269/change-the-color-of-a-particular-closed-path-in-a-android-canvas – Zach

1

完整的代碼來填充你需要先關閉一個Path。致電Path.close()關閉當前輪廓。

如果要更改行的顏色,則需要使用paint.setStyle(Paint.Style.STROKE)並使用paint.setColor()

...當然,您需要使您的視圖無效,即onDraw() - 繪製路徑的地方 - 再次調用。