如何改变笔画宽度android?

时间:2013-10-17 10:56:49

标签: android

我有问题。当我用双击绘画时改变笔画宽度时,画笔画宽也已经改变了。我想要改变笔画宽度而不改变最后的画笔画。

这是我的代码:

  package com.example.paint;

    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 SingleTouchEventView extends View implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {

    private Paint paint = new Paint();
    private Path path = new Path();

    private static final String DEBUG_TAG = "Gestures";
    private GestureDetectorCompat mDetector; 

    public SingleTouchEventView(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.BEVEL);

    mDetector = new GestureDetectorCompat(context, this);

    }

    @Override
    protected void onDraw(Canvas canvas) {
     canvas.drawPath(path, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    float eventX = event.getX();
    float eventY = event.getY();

    this.mDetector.onTouchEvent(event);
    // Be sure to call the superclass implementation

    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 event) { 
      Log.d(DEBUG_TAG,"onDown: " + event.toString()); 
      return false;
   }

   @Override
   public boolean onFling(MotionEvent event1, MotionEvent event2, 
          float velocityX, float velocityY) {
      Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());
      return false;
   }

   @Override
   public void onLongPress(MotionEvent event) {
      Log.d(DEBUG_TAG, "onLongPress: " + event.toString()); 
   }

   @Override
   public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
          float distanceY) {
      Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());
      return false;
   }

   @Override
   public void onShowPress(MotionEvent event) {
      Log.d(DEBUG_TAG, "onShowPress: " + event.toString());
   }

   @Override
   public boolean onSingleTapUp(MotionEvent event) {
      Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());
      return false;
   }

   @Override
   public boolean onDoubleTap(MotionEvent event) {

      float width = paint.getStrokeWidth();
      width++;
      paint.setStrokeWidth(width);
      Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
      return true;
   }

   @Override
   public boolean onDoubleTapEvent(MotionEvent event) {
      Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString());
      return false;
   }

   @Override
   public boolean onSingleTapConfirmed(MotionEvent event) {
      Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
      return false;
   }
   } 

这是我的活动

package com.example.paint;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new SingleTouchEventView(this, null));
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
} 

1 个答案:

答案 0 :(得分:0)

目前,您只存储了一个Path

您需要存储多个Path及其关联的宽度,以便您可以在onDraw中为每个不同的路径更改Paint的宽度。