android drawing app: line cannot be drawn on a bitmap loaded from gallery

爱⌒轻易说出口 提交于 2020-01-06 07:02:45

问题


I am working on a drawing app, but i do not know why when the picture is loaded from gallery, when further draw on it, the line just drawn will appear on Touch but will disappear when the finger is off the screen, i.e. the line drawn cannot be fixed onto the Bitmap.

Would there be anyone that know how to modify it? Many thanks!!!

coding:

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

回答1:


Do you delete the pathMap when the Touch event ends (i.e., the touchEnded method)?

That would result in the behaviour that you describe, as you are not drawing on the Bitmap, but rather on the Canvas that you are drawing both the line and the bitmap unto (and that Canvas is being redrawn on with every onDraw event); i.e., with each onDraw, you draw over your previous drawing.




回答2:


I have solved the problem using the following code. The bitmap needed to be copy for editing purposes.

bitmap = (BitmapFactory.decodeFile(picturePath));
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);    
bitmapCanvas = new Canvas(bitmap);
invalidate();


来源:https://stackoverflow.com/questions/14427665/android-drawing-app-line-cannot-be-drawn-on-a-bitmap-loaded-from-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!