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

标签 java android bitmap bufferedimage

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

有没有办法在安卓上做到这一点?我考虑过将两个 map 都转换为 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>

关于java - 我有两张 Bitmap 格式的图片,我想将一张透明度为 50% 的图片叠加在另一张上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57829385/

相关文章:

c# - xamarin.android.support.compat 28.0.0.1 与 netstandard2.0 不兼容

wpf - 位图性能优化模式

c# - 从 8 位索引像素数组转换为位图对象

java - 如何为 Maven 安装的 jar 编写 POM 文件?

android - 带有比例标签的动画列表在 Marshmallow 中不起作用

java - 如何模拟 Google API AndroidPublisher 请求

android - Facebook 原生广告中的展示位置

c# - 高效处理超宽但不是那么高的位图?

java - 调用另一个类中的函数

java - 在 for 循环中返回,不评估 Iterator 中的所有条目