android - 组合一张图像覆盖另一张图像

标签 android android-imageview android-canvas android-bitmap

我有两张图片,图片 A 是背面的大背景,图片 B 是将合并到图片 A 顶部的小图标。

工作原理

用户从相机拍摄一张照片,这张照片将是图像 A。 用户从布局中选择图标,这将是图像 B。 选择图像 B 的图像后,用户可以在布局中移动图像 B 以调整图像 B 将覆盖在图像 A 之上的位置。

在用户按下保存之后, Canvas 将合并两个图像,B 在 A 之上,与用户想要的位置并将其保存到 SD 卡。

问题

我已经设法让图像 B 在布局中移动但是我不知道如何让它在图像 A 的位置合并。

这就是我让图像 B 在布局中移动的方法。

img_additionalImage = (ImageView) findViewById(R.id.img_additionalImage);
    img_additionalImage.setOnTouchListener(new OnTouchListener()
    {
        @SuppressLint("NewApi")
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    isImageMoving = true;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (isImageMoving)
                    {
                        x = event.getRawX() - img_additionalImage.getWidth() / 2;
                        y = event.getRawY() - img_additionalImage.getHeight() / 2;
                        img_additionalImage.setX(x);
                        img_additionalImage.setY(y);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    isImageMoving = false;
                    break;
            }
            return true;
        }
    });

我不知道如何将两张图片与用户选择的位置合并在一起。

最佳答案

如果您使用 RealtiveLayout 或 LinearLayout 作为这 2 个 ImageView 的父布局,那么您可以通过这种方式捕获该 View ..

view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
b.compress(CompressFormat.JPEG, 95, new FileOutputStream("/some/location/image.jpg"));

其中 view 是您的 View 。 95 是 JPG 压缩的质量。文件输出流就是这样。

setDrawingCacheEnabled 是什么?

Enables or disables the drawing cache. When the drawing cache is enabled, the next call to getDrawingCache() or buildDrawingCache() will draw the view in a bitmap. Calling draw(android.graphics.Canvas) will not draw from the cache when the cache is enabled. To benefit from the cache, you must request the drawing cache by calling getDrawingCache() and draw it on screen if the returned bitmap is not null.

Enabling the drawing cache is similar to setting a layer when hardware acceleration is turned off. When hardware acceleration is turned on, enabling the drawing cache has no effect on rendering because the system uses a different mechanism for acceleration which ignores the flag. If you want to use a Bitmap for the view, even when hardware acceleration is enabled, see setLayerType(int, android.graphics.Paint) for information on how to enable software and hardware layers.

来自 Android Docs

关于android - 组合一张图像覆盖另一张图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20866427/

相关文章:

android - LinearLayout 的内容不可见

java - 路径上的文本从 4.1.2 到 4.0.3 不起作用

java - 安卓填充局部圆弧

java - 使用 Java 传递具有动态属性的 Parceled 对象的值时使用 indexOf 的技术?

Android Parse 推送通知在 Android 中不起作用

android - 如何让ImageView根据固定高度匹配宽度?

android - 如何使用 imageview 和 text android 创建自定义弯曲布局?

android - 如何在android中的 Canvas 中获取屏幕尺寸的大小?

android - 如何使用 For 循环在 android 中获取多个 EditText 值

java - Android - 从 AsyncTask 结果获取对象