2015-10-18 44 views
0

我需要從畫廊得到4張圖片...如何防止滯後於Android的加載多張圖片工作室

當我摸着我的活動在4 ImageView它調用下面的代碼片段之一時:

public void imagePicker(View view) 
{ 
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
    photoPickerIntent.setType("image/*"); 
    startActivityForResult(photoPickerIntent, view.getId()); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    ImageView image1=(ImageView)findViewById(R.id.image1); 
    ImageView image2=(ImageView)findViewById(R.id.image2); 
    ImageView image3=(ImageView)findViewById(R.id.image3); 
    ImageView image4=(ImageView)findViewById(R.id.image4); 

    if ((requestCode == R.id.image1 || requestCode == R.id.image2 || requestCode == R.id.image3 || requestCode == R.id.image4) && resultCode == RESULT_OK && data != null) 
    { 
     Uri pickedImage = data.getData(); 
     String[] filePath = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null); 
     cursor.moveToFirst(); 
     String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0])); 

     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options); 
     switch(requestCode) 
     { 
      case R.id.image1: 
       image1.setImageBitmap(bitmap); 
       break; 
      case R.id.image2: 
       image2.setImageBitmap(bitmap); 
       break; 
      case R.id.image3: 
       image3.setImageBitmap(bitmap); 
       break; 
      case R.id.image4: 
       image4.setImageBitmap(bitmap); 
       break; 
     } 
     cursor.close(); 
    } 

但是當它加載第二個圖像時,活動開始滯後。 那麼,我該如何避免這種情況?我想我可以減少ImageView的分辨率並將原始圖像存儲在其他地方。如果這樣做,我該怎麼做?

回答

1

試試縮放你的bitmap以降低其實際分辨率,我用下面的代碼來減少bitmap的大小。

int nh = (int) (bitmap.getHeight() * (512.0/bitmap.getWidth())); 
Bitmap scaled = Bitmap.createScaledBitmap(bitmap, 512, nh, true); 
+0

謝謝你,完美的工作。 –

+0

@RafaelMarinho標記爲答案,如果有幫助,謝謝 –