我有两个图像在彼此之上,通过在运行时更改顶部图像的不透明度然后将这两个图像组合(意味着将两个图像作为一个图像)并将其保存在图库中。在android studio中如何实现这个目标?。
答案 0 :(得分:0)
如果图片是全屏的,只需制作自己的屏幕截图即可。如果你想要的话,有很多很棒的库会为你做这件事,或者你可以使用我在这个名为ImageHelper的类中放入的简单代码:
public static Bitmap takeScreenshotOfView(Activity context, Bitmap.CompressFormat compressFormat){
Bitmap screenshot = null;
try {
// create bitmap screen capture
View v1 = context.getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(context.getFilesDir() + File.separator + "A35_temp" + File.separator + "screenshot_temp");
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
screenshot.compress(compressFormat, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
return screenshot;
}
就像使用它一样:
Bitmap screenshot = ImageHelper.takeScreenshotOfView(this, Bitmap.CompressFormat.JPEG);
现在,如果您需要捕获ImageView区域,您可以通过视图获取它或获取两个图像视图的父级以便包含它们。您也可以指定全屏,但从imageView起点和终点开始。以下是我编写的用于管理各个区域的屏幕截图的示例代码。
private void convertCardToBitmap(boolean sendOnComplete){
if(!mIsForStore) {
Toast.makeText(this, getString(R.string.downloading_to_gallery), Toast.LENGTH_LONG).show();
}
CardView cardView = (CardView)findViewById(R.id.my_image_view_id);
cardView.setDrawingCacheEnabled(true);
cardView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//cardView.measure((int)height, (int)width);
cardView.layout(0, 0, cardView.getMeasuredWidth(), cardView.getMeasuredHeight());
//cardView.layout(0, 0, (int)width, (int)height);
cardView.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(cardView.getDrawingCache());
saveBitmapToGallery(bitmap, sendOnComplete);
}
private void saveBitmapToGallery(Bitmap cardToSave, boolean sendFileWhenSaved){
OutputStream output;
// Find the SD Card path
File filepath = Environment.getExternalStorageDirectory();
// Create a new folder in SD Card
File dir = new File(filepath.getAbsolutePath() + "/MyAppPathDir/");
dir.mkdirs();
String imageName = mSelectedPerson.getFirstName() + "_" + mSelectedPerson.getLastName() + "_" + FileNameHelper.getNowTimeStamp();
// Create a name for the saved image
File file = new File(dir, imageName + ".jpg");
try {
int count = 1;
//if file exists add 1 on the end and loop until finding a name that doesn't exist.
while(file.exists()){
file = new File(dir, imageName + count + ".jpg");
count++;
}
output = new FileOutputStream(file);
// Compress into png format image from 0% - 100%
cardToSave.compress(Bitmap.CompressFormat.JPEG, 100, output);
output.flush();
output.close();
addImageToGallery(file.getAbsolutePath());
if(sendFileWhenSaved){
Intent intent = getIntent();
intent.putExtra(Globals.INTENT_KEYS.KEY_FILE_TO_SHARE, file.getAbsolutePath());
intent.putExtra(Globals.INTENT_KEYS.KEY_SELECTED_IMAGE, mSelectedCardModel);
setResult(RESULT_OK, intent);
finish();
} else if (mIsForStore) {
uploadImageToS3(file.getAbsolutePath());
} else{
Toast.makeText(this, getString(R.string.saved_to_gallery), Toast.LENGTH_LONG).show();
finish();
}
} catch (Exception e) {
Toast.makeText(this, getString(R.string.error_failed_save_to_gallery) + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
private void uploadImageToS3(String filePath){
final File newImageFile = new File(filePath);
showProgressDialog(TAG, getString(R.string.loading_please_wait));
//For auth route
BasicAWSCredentials credentials = new BasicAWSCredentials(CognitoManager.getS3ClientID(), CognitoManager.getS3ClientSecret());
AmazonS3Client s3 = new AmazonS3Client(credentials);
TransferUtility transferUtility = new TransferUtility(s3, this);
TransferObserver observer = transferUtility.upload(CognitoManager.getS3BucketName(), newImageFile.getName(), newImageFile);
observer.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
if(state.compareTo(TransferState.COMPLETED) == 0){
String imgURLOfUploadComplete = "https://s3.amazonaws.com/" + CognitoManager.getS3BucketName() + "/" + newImageFile.getName();
hideProgressDialog(TAG);
Intent intent = new Intent();
intent.putExtra(Globals.INTENT_KEYS.KEY_IMAGE_URL, imgURLOfUploadComplete);
setResult(Activity.RESULT_OK, intent);
if(newImageFile.exists()){
newImageFile.delete();
}
finish();
}
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
if(bytesTotal != 0) {
//For viewing progress
int percentage = (int) (bytesCurrent / bytesTotal * 100);
}
}
@Override
public void onError(int id, Exception ex) {
A35Log.e(TAG, getString(R.string.error_uploading_s3_part1) + id + getString(R.string.error_uploading_s3_part2) + ex.getMessage());
hideProgressDialog(TAG);
showDialogMessage(getString(error), getString(R.string.error_failed_create_image_alert_id) + error);
}
});
}
public void addImageToGallery(final String filePath) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, filePath);
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
希望有帮助:)。 我会警告你,虽然当你启动setDrawCacheEnabled它可能在屏幕上有奇怪的行为,并导致你需要重绘,因为它似乎有时删除约束。出于这个原因,我经常只打开一个新的空白屏幕,其中包含我要转换为图像的内容,完成工作然后关闭。
所以,看看它是如何为你工作的,并决定从那里去哪里。我尝试的大多数库也有同样的问题,这就是为什么我只是自己写的。