2014-07-07 69 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