2013-08-27 76 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() - 绘制路径的地方 - 再次调用。