共享前调整图像大小(插入画布)

时间:2016-06-07 20:08:15

标签: java android android-layout

我目前有一些图像(所有尺寸均为100x100,非常小)从MySQL数据库服务器中提取并显示在我的应用程序中。一切都按照我的意愿行事。但是,我注意到当我将图像分享给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;
    }

     }

1 个答案:

答案 0 :(得分:0)

创建此类bmp的最简单方法是使用Canvas.drawBitmap()。此方法允许您在画布上绘制位图。所以解决方案是:

1)使用白色背景创建所需大小的画布(有关详细信息,请参阅此question)。

2)使用drawBitmap()在画布中心绘制位图。

3)使用输出bmp进行共享。

相关问题