0

我正在开发一个绘图应用程序,但我不知道为什么当从画廊加载图片时,当进一步绘制它时,刚刚绘制的线条会出现在 Touch 上但当手指离开屏幕时会消失,即线条绘制的不能固定在位图上。

有大佬知道怎么修改吗?非常感谢!!!

编码:

public class DrawView extends View  // the main screen that is painted
{
   private static final float TOUCH_TOLERANCE = 10;

   private Bitmap bitmap; // drawing area for display or saving
   private Canvas bitmapCanvas; // used to draw on bitmap
   private Paint paintScreen; // use to draw bitmap onto screen
   private Paint paintLine; // used to draw lines onto bitmap
   private HashMap<Integer, Path> pathMap; // current Paths being drawn
   private HashMap<Integer, Point> previousPointMap; // current Points   

   public DrawView(Context context, AttributeSet attrs) 
   {
      super(context, attrs);     
      paintScreen = new Paint(); 

      // set the default settings
      paintLine = new Paint();
      paintLine.setColor(Color.BLACK); 
      paintLine.setStyle(Paint.Style.STROKE);
      paintLine.setStrokeWidth(5);
      pathMap = new HashMap<Integer, Path>();
      previousPointMap = new HashMap<Integer, Point>();
   }

   // Method onSizeChanged creates BitMap and Canvas after app displays
   @Override
   public void onSizeChanged(int w, int h, int oldW, int oldH)
   {
      bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
      bitmapCanvas = new Canvas(bitmap);
      bitmap.eraseColor(Color.WHITE); // erase the BitMap with white
   }       

   public void load_pic(String picturePath) // load a picture from gallery
   {
      pathMap.clear(); // remove all paths
      previousPointMap.clear(); // remove all previous points
      bitmap = BitmapFactory.decodeFile(picturePath);
      invalidate(); // refresh the screen
   }   

   @Override
   protected void onDraw(Canvas canvas) 
   {
      canvas.drawBitmap(bitmap, 0, 0, paintScreen);
      for (Integer key : pathMap.keySet()) 
         canvas.drawPath(pathMap.get(key), paintLine); // draw line
   }

   // handle touch event
   @Override
   public boolean onTouchEvent(MotionEvent event) 
   {
      int action = event.getActionMasked();
      int actionIndex = event.getActionIndex(); // pointer (i.e., finger)

      // determine which type of action the given MotionEvent represents, then call the corresponding handling method
      if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN) 
      {
         touchStarted(event.getX(actionIndex), event.getY(actionIndex), event.getPointerId(actionIndex));
      } // end if
      else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) 
      {
         touchEnded(event.getPointerId(actionIndex));
      } // end else if
      else 
      {
         touchMoved(event); 
      } // end else

      invalidate(); // redraw
      return true; // consume the touch event
   } // end method onTouchEvent

   // called when the user touches the screen
   private void touchStarted(float x, float y, int lineID) 
   {
      Path path; // used to store the path for the given touch id
      Point point; // used to store the last point in path

      // if there is already a path for lineID
      if (pathMap.containsKey(lineID)) 
      {
         path = pathMap.get(lineID); // get the Path
         path.reset(); // reset the Path because a new touch has started
         point = previousPointMap.get(lineID); // get Path's last point
      } // end if
      else 
      {
         path = new Path(); // create a new Path
         pathMap.put(lineID, path); // add the Path to Map
         point = new Point(); // create a new Point
         previousPointMap.put(lineID, point); // add the Point to the Map
      } // end else

      // move to the coordinates of the touch
      path.moveTo(x, y);
      point.x = (int) x;
      point.y = (int) y;
   }

...similar for other on Touch event
4

2 回答 2

0

我已经使用以下代码解决了这个问题。需要复制位图以进行编辑。

bitmap = (BitmapFactory.decodeFile(picturePath));
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);    
bitmapCanvas = new Canvas(bitmap);
invalidate();
于 2013-02-17T14:58:24.087 回答
0

Touch事件结束时是否删除pathMap(即touchEnded方法)?

这将导致您描述的行为,因为您不是在位图上绘图,而是在您正在绘制线和位图的画布上(并且每个 onDraw 事件都在重新绘制 Canvas);即,对于每个 onDraw,您都会在之前的绘图上进行绘制。

于 2013-02-16T20:39:28.417 回答