android - android 将 1 个位图覆盖到另一个图像,但没有得到正确的输出

标签 android bitmap overlap masking

在我的一个应用程序中,我需要做掩蔽(将一张图像重叠到另一张图像)

在我的应用程序中,我必须将 1 个图像(位图)加载到 imageview,然后必须将一些 fram 应用于该图像我为此使用了另一个 imageview...这完全有效

我的问题是.. 当我要保存位图时... 使用这段代码

public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
        Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
        Canvas canvas = new Canvas(bmOverlay);
        canvas.drawBitmap(bmp1, new Matrix(), null);
        canvas.drawBitmap(bmp2, 0, 0, null);

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();

        bmOverlay.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator
                + "test.jpg");
        try {
            f.createNewFile();
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bmOverlay;
    }

我得到 this

但我需要它,就像

This

最佳答案

目前我发现的最好的是这个:

public Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
        Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), bmp2.getHeight(), bmp1.getConfig());
        float left =(bmp2.getWidth() - (bmp1.getWidth()*((float)bmp2.getHeight()/(float)bmp1.getHeight())))/(float)2.0;
        float bmp1newW = bmp1.getWidth()*((float)bmp2.getHeight()/(float)bmp1.getHeight());
        Bitmap bmp1new = getResizedBitmap(bmp1, bmp2.getHeight(), (int)bmp1newW);
        Canvas canvas = new Canvas(bmOverlay);
        canvas.drawBitmap(bmp1new, left ,0 , null);
        canvas.drawBitmap(bmp2, new Matrix(), null);
        return bmOverlay;
    }

    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        return resizedBitmap;
    }

关于android - android 将 1 个位图覆盖到另一个图像,但没有得到正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24692760/

相关文章:

android - 如何在 react 原生模式上设置borderRadius?

java - 安卓 ListView : how to change background color of specific cells (by index)?

java - 相机拍摄的图像不想显示在 ImageView 中

html - div相对重叠

Android + Groovy + Android 注解

java - 位图创建内存不足

java - PorterduffXfermode : Clear a section of a bitmap

mysql - 检查表的时间重叠?

c# - OverlapCircleAll 没有拾取其他对象

android - 我如何记录这个异常?