2014-07-07 71 views
0

我有一個帶有背景圖像的ImageView。 ImageView的大小以屏幕的百分比爲單位進行編程。該視圖位於RelativeLayout中,位於它的中心。我想要的是使其可繪製。我希望能夠用手指畫出它(就像我們小時候在油漆中畫廢話一樣)。另外,當我移動手指時,我想要立即繪製路徑。你能給我一個如何去做的例子嗎?Android ImageView如何繪製路徑

回答

0

您將不得不重寫ImageView的onTouchEventonDraw調用。獲取用戶在onTouchEvent中的觸摸位置,並畫到ImageViewcanvas。 看到這裏的示例 - paint canvas android

1

只需拿GestureOverlayView並將您的圖像設置爲此OverlayView的背景。

<android.gesture.GestureOverlayView 
      android:id="@+id/signaturePad" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="5" 
      android:background="@color/white" 
      android:eventsInterceptionEnabled="true" 
      android:fadeEnabled="false" 
      android:gestureColor="@color/black" 
      android:gestureStrokeLengthThreshold="0.1" 
      android:gestureStrokeType="multiple" 
      android:orientation="vertical" > 
     </android.gesture.GestureOverlayView> 

以及用於從GestureOverlayView保存圖像的功能。

try { 
      GestureOverlayView gestureView = (GestureOverlayView) findViewById(R.id.signaturePad); 
      gestureView.setDrawingCacheEnabled(true); 
      Bitmap bm = Bitmap.createBitmap(gestureView.getDrawingCache()); 
      File f = new File(Environment.getExternalStorageDirectory() 
        + File.separator + "signature.png"); 
      f.createNewFile(); 
      FileOutputStream os = new FileOutputStream(f); 
      os = new FileOutputStream(f); 
      //compress to specified format (PNG), quality - which is ignored for PNG, and out stream 
      bm.compress(Bitmap.CompressFormat.PNG, 100, os); 
      os.close(); 
     } catch (Exception e) { 
      Log.v("Gestures", e.getMessage()); 
      e.printStackTrace(); 
     } 

關注該Example