gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:03:18 25 4
gpt4 key购买 nike

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com