0

我正在使用此xamarin示例中的 FingerPaintCanvasView 。

我正在使用2 层。第一层是我想要绘制的ImageView 。第二层是要绘制的PaintCanvasView

  <RelativeLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <ImageView
      android:id="@+id/markImageImageView"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"/>
    <fingerpaint.FingerPaintCanvasView
      android:id="@+id/canvasMarkMeta"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
  </RelativeLayout>

画布具有透明背景,并且两个视图的布局参数均以编程方式设置。这样标记工作正常。

在此处输入图像描述

现在的问题是,我如何才能将此图像视图与标记画布合并,以尽可能少地丢失单个文件(文件系统中的位图或图像文件)的质量损失。

让我解释一下为什么我提到了质量损失:例如,背景中的图像从设备相机的尺寸为 1920x1080。显示器只有 1280x800 像素。由于我无法将图像放入显示器中,因此我需要显示一个缩小的版本,并且标记发生在这个缩小的版本上。

编辑:

@Joe LV:

这是您的演示,没有在我的设备上部署任何更改:

联想 Yoga 3,安卓 6.0.1 在此处输入图像描述

华为荣耀8,安卓7.0 在此处输入图像描述

我很快就会尝试 Android 8 模拟器。

像素 2XL,安卓 8.1 在此处输入图像描述

所以这个方法不适用于 API <= 24 :-( (API 25 和 26 未测试)

markImageImageView仅保存我从设备存储加载的图像(可以是任何图像) canvasMarkMeta是来自链接模板的 FingerPaintCanvas,其中保存了绘制的线条。

4

1 回答 1

2

以下是代码,我在其中添加了注释:

   public class MainActivity : Activity
    {
        private Rect mSrcRect, mDestRect;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            //your background picture ---markImageImageView
            Bitmap background = BitmapFactory.DecodeResource(Resources, Resource.Drawable.pause);
            //your foreground picture ---FingerPaintCanvasView
            Bitmap foreground = BitmapFactory.DecodeResource(Resources, Resource.Drawable.play);

            Paint p = new Paint();
            p.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcOver));

            //use background to create a canvas
            Bitmap workingBitmap = Bitmap.CreateBitmap(background);
            Bitmap mutableBitmap = workingBitmap.Copy(Bitmap.Config.Argb8888, true);
            Canvas c = new Canvas(mutableBitmap);

            int mBWith = background.Width;
            int mBHeight = background.Height;

            int mFWith = foreground.Width;
            int mFHeight = foreground.Height;
            mSrcRect = new Rect(0, 0, mBWith, mBHeight);
            mDestRect = new Rect(0, 0, mFWith, mFHeight);

            //draw foreground on the backaground, then they will be single bitmap
            c.DrawBitmap(foreground, mSrcRect, mDestRect, p);
            ImageView imageView = FindViewById<ImageView>(Resource.Id.iv);
            imageView.SetImageBitmap(mutableBitmap);
        }
    }

我已经在 github 上提供了演示。

更新:

更改Bitmap.Config.Argb4444Bitmap.Config.Argb8888

于 2018-05-07T03:26:23.033 回答