2016-06-07 47 views
0

我目前有幾張從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; 
    } 

    } 

回答

0

創建這樣一個bmp最簡單的方法是使用Canvas.drawBitmap()。此方法允許您在畫布上繪製位圖。所以解決方案是:

1)創建一個白色背景所需大小的畫布(詳情請參閱此question)。

2)使用drawBitmap()在畫布中心繪製位圖。

3)使用輸出bmp進行共享。