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

Touchイベントが終了したときにpathMapを削除しますか(つまり、touchEndedメソッド)?

これにより、ビットマップではなく、線とビットマップの両方を描画しているCanvasに描画している(そして、CanvasはすべてのonDrawイベントで再描画されている)ため、説明した動作が発生します。つまり、各onDrawで、前の描画の上に描画します。

于 2013-02-16T20:39:28.417 に答える
0

次のコードを使用して問題を解決しました。編集のためにビットマップをコピーする必要がありました。

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