在共享之前调整图像大小(插入画布)
问题描述:
我目前有几张从MySQL数据库服务器拉出并显示在我的应用中的图像(尺寸全部为100x100,非常小)。一切工作,并按我的意愿行事。但是,我注意到,当我将图像分享给WhatsApp,Messenger或其他社交媒体应用程序(我的应用程序中有按钮共享图像)时,图像被炸毁,好像它们太小而无法共享,所以社交媒体应用程序调整它们的大小 - 这使得图像像素化,因为原始尺寸是100x100。在共享之前调整图像大小(插入画布)
为了避免图像被调整大小,我有一个想法,但不知道它的可能性或如何去做。在共享期间可以将原始图像放置在尺寸为300x300的白色画布上吗?这样图像将是300x300,但有一个白色的背景,并希望分享适当,而不被调整。
CardPreviewActivity
public class CardPreviewActivity extends AppCompatActivity {
BottomSheetLayout bottomSheetLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preview_activity);
final SuperHero superHero = App.self.previewRequestedSuperHeroObject;
// SET IMAGE
ImageLoader imageLoader = CustomVolleyRequest.getInstance(this).getImageLoader();
NetworkImageView heroPreviewImg = (NetworkImageView) findViewById(R.id.imageViewHero);
heroPreviewImg.setImageUrl(superHero.getImageUrl(), imageLoader);
// SET NAME
TextView heroName = (TextView) findViewById(R.id.preview_super_hero_name);
heroName.setText("Name: " + superHero.getName());
// SET DIRECTLINK
TextView heroDirectLink = (TextView) findViewById(R.id.preview_super_hero_directlink);
heroDirectLink.setText("Direct link: " + superHero.getDirectLink());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(superHero.getName()); // set the top title
bottomSheetLayout = (BottomSheetLayout) findViewById(R.id.bottomsheet);
//SET SAVE SHARE BUTTON TEXT
TextView heroName2 = (TextView) findViewById(R.id.btn_custom_view);
heroName2.setText("Share " + superHero.getName());
}
<code>
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_custom_view:
bottomSheetLayout.showWithSheetView(getLayoutInflater().inflate(R.layout.bottomsheet, bottomSheetLayout, false));
break;
// case R.id.btn_save_face:
// break;
case R.id.btn_share_face:
ImageView imageView = (ImageView) findViewById(R.id.imageViewHero);
Drawable mDrawable = imageView.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
mBitmap, "face", null);
Uri uri = Uri.parse(path);
final SuperHero superHero = App.self.previewRequestedSuperHeroObject;
String url = superHero.getImageUrl();
String name = superHero.getName();
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
IntentPickerSheetView intentPickerSheetView = new IntentPickerSheetView(CardPreviewActivity.this, shareIntent, ("Share " + superHero.getName()), new IntentPickerSheetView.OnIntentPickedListener() {
@Override
public void onIntentPicked(IntentPickerSheetView.ActivityInfo activityInfo) {
bottomSheetLayout.dismissSheet();
startActivity(activityInfo.getConcreteIntent(shareIntent));
}
});
bottomSheetLayout.showWithSheetView(intentPickerSheetView);
break;
case R.id.btn_copy_direct_link:
final SuperHero superHero2 = App.self.previewRequestedSuperHeroObject;
ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("face direct link", superHero2.getDirectLink());
clipboard.setPrimaryClip(clip);
// Toast.makeText(getApplicationContext(), ("Copied " + superHero2.getName() + " URL to the clipboard"), Toast.LENGTH_SHORT).show();
bottomSheetLayout.dismissSheet();
Snackbar.make(view, "Copied " + superHero2.getName() + "'s URL to the clipboard", Snackbar.LENGTH_LONG)
.setAction("Action", null)
.show();
break;
case R.id.btn_copy_direct_link_with_tags:
final SuperHero superHero3 = App.self.previewRequestedSuperHeroObject;
ClipboardManager clipboard2 = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
ClipData clip2 = ClipData.newPlainText("face direct link with image tags", ("[img]" + superHero3.getDirectLink() + "[/img]"));
clipboard2.setPrimaryClip(clip2);
// Toast.makeText(getApplicationContext(), ("Copied " + superHero3.getName() + " URL to the clipboard with IMG tags"), Toast.LENGTH_SHORT).show();
bottomSheetLayout.dismissSheet();
Snackbar.make(view, "Copied " + superHero3.getName() + "'s URL to the clipboard with IMG tags", Snackbar.LENGTH_LONG)
.setAction("Action", null)
.show();
break;
}
}
//Add back button to go back
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
}
public boolean onSupportNavigateUp() {
finish();
overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
return true;
}
}
答
创建这样一个bmp最简单的方法是使用Canvas.drawBitmap()。此方法允许您在画布上绘制位图。所以解决方案是:
1)创建一个白色背景所需大小的画布(详情请参阅此question)。
2)使用drawBitmap()在画布中心绘制位图。
3)使用输出bmp进行共享。