2017-01-06 59 views
0

我從網址下載圖片並顯示在我的android應用程序中,但我無法根據我的要求調整圖片大小。Android Image Resizing

private Bitmap decodeFile(File f) { 
    try { 
     //Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     FileInputStream stream1 = new FileInputStream(f); 
     BitmapFactory.decodeStream(stream1, null, o); 
     stream1.close(); 

     //Find the correct scale value. It should be the power of 2. 

     // Set width/height of recreated image 
     final int REQUIRED_SIZE = 285; 

     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 /= 1; 
      height_tmp /= 1; 
      scale *= 2; 
     } 
     //decode with current scale values 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     FileInputStream stream2 = new FileInputStream(f); 
     Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2); 
     stream2.close(); 
     return bitmap; 
    } catch (FileNotFoundException e) { 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 


} 

現在任何一個可以告訴我,我怎麼能調整我的圖像250 * 250像素 也感謝名單提前

+3

「我無法根據我的要求調整圖像大小」 - 請編輯您的問題並詳細解釋**,詳細說明**,您的問題是什麼。 **具體**不適用於您嘗試過的代碼? – CommonsWare

回答

1

嘗試使用畢加索庫 - http://square.github.io/picasso/

這是相當容易使用和集成,並有一個調整大小的方法做到這一點。

Picasso.with(context).load(url).resize(250, 250).centerCrop().into(imageView); 

你基本上把'url'代替了你的url。