我有两张 Bitmap 格式的图片,我想将一张透明度为 50% 的图片叠加在另一张上

I have two images in Bitmap format and want to overlay one with 50% transparency over the other one

正如标题中所述, 给定:两个位图 想要:一张位图,两张图片混合在一起。

有没有办法在 android 上执行此操作?我考虑过将两个地图都转换为 bufferedImages,然后重叠并转换回 Bitmap,但这似乎不适用于 android。我对这一切还很陌生。

基本上是保存在 currentImagePath1currentImagePath2 的位图。这是 java 代码:


package com.example.cameraexample;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    String currentImagePath1 = null;
    String currentImagePath2 = null;
    ImageView imageView;
    int pic = 1;
    private static final int IMAGE_REQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void captureImage1(View view) {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if(cameraIntent.resolveActivity(getPackageManager()) != null){
            File imageFile = null;

            try {
                imageFile = getImageFile1();
            }
            catch (IOException e) {
                e.printStackTrace();
            }

            if(imageFile != null) {
                Uri imageUri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", imageFile);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(cameraIntent, IMAGE_REQUEST);

            }
        }
    }

    public void captureImage2(View view) {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if(cameraIntent.resolveActivity(getPackageManager()) != null){
            File imageFile = null;

            try {
                imageFile = getImageFile2();
            }
            catch (IOException e) {
                e.printStackTrace();
            }

            if(imageFile != null) {
                Uri imageUri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", imageFile);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(cameraIntent, IMAGE_REQUEST);
            }
        }
    }

    /*public void displayImageBackup(View view) {
       Intent intent = new Intent(this, DisplayImage.class);
       intent.putExtra("image_path", currentImagePath);
       startActivity(intent);
    }*/

    public void displayImage1(View view) {
        showImage(currentImagePath1);
    }

    public void displayImage2(View view) {
        showImage(currentImagePath2);
    }

    private File getImageFile1() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
        String imageName = "1_jps_" + timeStamp + "_";
        File storageDir  = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

        File imageFile = File.createTempFile(imageName, ".jpg", storageDir);
        currentImagePath1 = imageFile.getAbsolutePath();
        return imageFile;
    }

    private File getImageFile2() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
        String imageName = "2_jps_" + timeStamp + "_";
        File storageDir  = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

        File imageFile = File.createTempFile(imageName, ".jpg", storageDir);
        currentImagePath2 = imageFile.getAbsolutePath();
        return imageFile;
    }

    public void showImage(String image_path) {
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.mimageView);

        Bitmap bitmap = BitmapFactory.decodeFile(image_path);
        imageView.setImageBitmap(bitmap);
    }

    public void overlay(View view) {
    }

    public void switch_images(View view) {
        if(pic == 1) displayImage2(view);
        if(pic == 2) displayImage1(view);

        if(pic == 1) pic = 2;
        else pic = 1;
    }
}

不幸的是,测试并没有真正显示出对我有用的结果。

如何通过将它们放在 FrameLayout 中来创建两个彼此重叠的 ImageView。然后将 50% alpha 值设置为顶部图像,如下所示:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:src="@drawable/a"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:alpha="0.5"
        android:src="@drawable/b"/>
</FrameLayout>

非常感谢user1506104 指出了解决我问题的方法。抱歉回复晚了,我的生活中有些事情迫不及待了。

我可以将两个 ImageView 叠放在一起。 :)

它的工作方式与以下类似:为了覆盖,我制作了另一种方法,如 showImage,只是它投影位图的 ImageView 具有 0.5 的 alpha。错误是我不得不删除这一行:setContentView(R.layout.activity_main);