2012-04-22 87 views
0

我使用SD卡中的jpg加載圖像視圖,並對其進行縮放,使其無需裁剪即可最大限度地放大。 然後,我想用一個圓形覆蓋位圖,並以觸摸事件的座標爲中心。將覆蓋圖應用於縮放位圖 - 縮放覆蓋圖的問題

我保存圖像視圖中的當前位圖,創建一個新的疊加位圖,添加畫布,繪製原始位圖,繪製圓,重新應用縮放代碼到新的疊加位圖,然後使用新的疊加位圖重新載入圖像視圖。我第一次觸摸圖像繪製圓形時,該圓形被準確繪製,但圖像縮放不正確。在第二次觸摸時,圖像縮放會被糾正,但是圓圈被繪製在「錯誤」的位置 - 它被繪製在我觸摸屏幕的位置,但現在圖像正確縮放,因此目標已移動。在第三次以及其後的所有事情中,事情都像我希望的那樣從一開始就起作用。

這裏是我的代碼:

private static Matrix curMatrix = new Matrix(); 

public boolean onTouch(View v, MotionEvent event) { 
    int eventType = event.getAction() & MotionEvent.ACTION_MASK; 
    switch (eventType) { 
     case MotionEvent.ACTION_UP: 
      ImageView i = (ImageView) v;   

      //Save the current bitmap 
      i.buildDrawingCache(); 
      Bitmap bm = i.getDrawingCache(); 

      //Create new overlay bitmap 
      Bitmap bmOverlay = Bitmap.createBitmap(i.getWidth(), i.getHeight(), Bitmap.Config.ARGB_8888); 

      //Create new drawing canvas and paint 
      Canvas c = new Canvas(bmOverlay); 
      Paint p = new Paint(); 
      p.setColor(Color.RED); 
      p.setAlpha(50); 

      //Draw the saved bitmap onto the canvas 
      c.drawBitmap(bm, new Matrix(), null); 

      //Draw a circle on the current canvas, centered on event coordinates 
      c.drawCircle(event.getX(), event.getY(), 100F, p); 

      //Autosize canvas to previous imageview settings 
      RectF drawableRect = new RectF(0, 0, (float) c.getWidth(), (float) c.getHeight()); 
      RectF viewRect = new RectF(0, 0, (float) i.getWidth(), (float) i.getHeight()); 
      curMatrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER); 

      //Apply the autosize transformation 
      i.setImageMatrix(curMatrix);   

      //Reload the imageview with the new bitmap 
      i.setImageBitmap(bmOverlay); 
      } 
      return true; 
    } 

這裏有一些圖片,以更好地解釋發生了什麼:

開始 - 我想點擊的魚:
Start

第一點擊 - 點擊是準確的,縮放丟失:
First Click

第二次點擊 - 縮放再次出現,單擊雖然適用於原比例,所以它是關閉的:
Second Click

第三次和隨後的每次點擊 - 工程,我會希望從一開始會:
Third Click

感謝您的幫助!

回答

1

像往常一樣,我過於複雜的事情。對於那些感興趣的人,以下簡單的代碼工作。只需將它設置爲ImageView的TouchListener,當您觸摸圖像時,它將繪製一個以您的觸摸點爲中心的半徑爲100(像素?)的半圓形紅色圓圈:

public class GetCoordinatesTouchListener implements OnTouchListener { 
    public boolean onTouch(View v, MotionEvent event) { 
     int eventType = event.getAction() & MotionEvent.ACTION_MASK; 
     switch (eventType) { 
      case MotionEvent.ACTION_UP: 
       ImageView i = (ImageView) v; 
       //Save the current bitmap from the imageview 
       i.buildDrawingCache(); 
       Bitmap bm = i.getDrawingCache(); 
       //Create new overlay bitmap 
       Bitmap bmOverlay = Bitmap.createBitmap(i.getWidth(), i.getHeight(), Bitmap.Config.ARGB_8888); 
       //Create new drawing canvas and paint 
       Canvas c = new Canvas(bmOverlay); 
       Paint p = new Paint(); 
       p.setColor(Color.RED); 
       p.setAlpha(50); 
       //Draw the saved bitmap onto the canvas 
       c.drawBitmap(bm, new Matrix(), null); 
       //Draw a circle on the current canvas, centered on event coordinates 
       c.drawCircle(event.getX(), event.getY(), 100F, p); 
       //Reload the imageview with the new bitmap with FIT_XY scaling 
       i.setScaleType(ImageView.ScaleType.FIT_XY); 
       i.setImageBitmap(bmOverlay); 
      } 
     return true; 
    } 
}