2012-04-06 45 views
2

我能夠從手機中檢索圖片並將它們存儲在數組中。之後,我將它們顯示在屏幕上。但它們都有不同的形狀和大小。我想以相同的尺寸和形狀顯示它們。任何想法?在Android中以編程方式放大圖片

photoPaths = new ArrayList<String>(); 
    getAllPhotos(Environment.getExternalStorageDirectory(), photoPaths); 
    images = new Bitmap[photoPaths.size()]; 


     apa = (AnimationPhotoView)findViewById(R.id.animation_view); 
     for(int i=0;i<photoPaths.size();i++) 
     { 
      File imgFile = new File(photoPaths.get(0)); 

      if(imgFile.exists()) 
      { 

       images[0] = decodeFile(imgFile);} 

回答

7
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.tedd); 

    int width = bitmapOrg.getWidth(); 

    int height = bitmapOrg.getHeight(); 


    int newWidth = 200; 

    int newHeight = 200; 

    // calculate the scale - in this case = 0.4f 

    float scaleWidth = ((float) newWidth)/width; 

    float scaleHeight = ((float) newHeight)/height; 

    Matrix matrix = new Matrix(); 

    matrix.postScale(scaleWidth, scaleHeight); 
    matrix.postRotate(x); 
    // this will create image with new size 
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,width, height, matrix, true); 

    iv.setScaleType(ScaleType.CENTER); 
    iv.setImageBitmap(resizedBitmap); 
+0

工作完美!我需要等待才能接受。謝謝Mac – user182192 2012-04-06 13:21:47

+1

實際上,當使用這個時出現異常:'IllegalArgumentException:位圖大小超過32位',請檢查我的答案。 – 2013-05-07 10:42:42

+0

使用[this](http://stackoverflow.com/a/16248911/1289716)所以你不會得到'位圖大小超過' – MAC 2013-05-07 17:06:23

1

我使用:

Bitmap bitmap = //Your source 

int newWidth = //compute new width 
int newHeight = //compute new height 

bitmap = Bitmap.createScaledBitmap(bitmap, scaleWidth, scaleHeight, true); 

最後booleanfilter,這使得圖像更加平滑。

MAC提供的解決方案給了我一個IllegalArgumentException: bitmap size exceeds 32bits

這隻能縮放位圖,不會旋轉它。

0

我想你也可以試試這個。

private Bitmap decodeFile(File f) 
{ 
    try 
    { 
     //decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 
     //Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE=200; 
     int width_tmp=o.outWidth, height_tmp=o.outHeight; 
     int scale=1; 
     while(true) 
     { 
      if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
       break; 
      width_tmp/=2; 
      height_tmp/=2; 
      scale*=2; 
     } 
     //decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) {} 
    return null; 
} 
相關問題