【问题标题】:Android ImageView How to draw pathAndroid ImageView 如何绘制路径
【发布时间】:2014-07-07 18:28:15
【问题描述】:

我有一个带有背景图像的 ImageView。 ImageView 以编程方式设置为屏幕的百分比。该视图位于 RelativeLayout 中,并且位于其中心。我想要的是让它可绘制。我希望能够用我的手指画出来(就像我们小时候在画里画废话一样)。此外,当我移动手指时,我希望立即绘制路径。可以举个例子吗?

【问题讨论】:

    标签: android bitmap android-canvas draw


    【解决方案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,但我相信您可以适应切换视图类型

    【讨论】:

      【解决方案2】:

      只需使用 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

      【讨论】:

        【解决方案3】:

        您必须覆盖 ImageView 的 onTouchEventonDraw 调用。在onTouchEvent 中获取用户的触摸位置并绘制到ImageViewcanvas。 请参阅此处的示例 - paint canvas android

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-09-13
          • 2019-03-31
          • 1970-01-01
          • 2012-05-21
          • 1970-01-01
          • 1970-01-01
          • 2022-01-09
          • 1970-01-01
          相关资源
          最近更新 更多