Android ImageView如何绘制路径

时间:2014-07-07 18:28:15

标签: android bitmap android-canvas draw

我有一个带有背景图像的ImageView。 ImageView以编程方式调整为屏幕的百分比。视图位于RelativeLayout中,它位于它的中心。我想要的是让它成为可绘制的。我希望能够用我的手指画出来(就像我们在孩子们画画时一样废话)。当我移动我的手指时,我希望立即绘制路径。你能给我一个如何做的例子吗?

3 个答案:

答案 0 :(得分:1)

有一些非常好的参考绘图应用程序,只需少量谷歌搜索:

简单的应用程序,以获得良好的湿手指/易于实施: http://v4all123.blogspot.com/2013/11/simple-drawing-example-in-android.html

更复杂,但解释非常彻底: http://code.tutsplus.com/tutorials/android-sdk-create-a-drawing-app-touch-interaction--mobile-19202

两者都没有使用ImageView,但我确信您可以适应切换视图类型

答案 1 :(得分: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

答案 2 :(得分:0)

您必须覆盖ImageView的onTouchEventonDraw来电。在onTouchEvent中获取用户的触摸位置,并绘制到ImageView canvas。 看这里的例子 - paint canvas android

相关问题