2013-02-05 29 views
3

我有非常大的位圖圖像。我的源如何將位圖的大小調整爲最大可用大小?

BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(new FileInputStream(f), null, o); 

      // The new size we want to scale to 
      final int REQUIRED_WIDTH = 1000; 
      final int REQUIRED_HIGHT = 500; 
      // Find the correct scale value. It should be the power of 2. 
      int scale = 1; 
      while (o.outWidth/scale/2 >= REQUIRED_WIDTH 
        && o.outHeight/scale/2 >= REQUIRED_HIGHT) 
       scale *= 2; 

      // Decode with inSampleSize 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize = scale; 
      return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 

我想正確地調整圖片大小,我需要調整圖像最大可用大小

例如

我下載的圖像尺寸4000x4000像素,我的手機支持2000x1500像素大小 我需要安排尺寸如何支持我的手機? 然後我調整圖像以2000x1500(例如)

+0

問題被關閉,HTTP:// stackoverflow.com/a/7523221/1568164 - answer –

回答

0

在這裏,你有很好的調整位圖來最大avaliabe大小:

public void onClick() //for example 
{ 
/* 
Getting screen diemesions 
*/ 
WindowManager w = getWindowManager(); 
     Display d = w.getDefaultDisplay(); 
     int width = d.getWidth(); 
     int height = d.getHeight(); 
// "bigging" bitmap 
Bitmap nowa = getResizedBitmap(yourbitmap, width, height); 
} 

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 

int width = bm.getWidth(); 

int height = bm.getHeight(); 

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

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

// create a matrix for the manipulation 

Matrix matrix = new Matrix(); 

// resize the bit map 

matrix.postScale(scaleWidth, scaleHeight); 

// recreate the new Bitmap 

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 

return resizedBitmap; 

} 

我希望我幫助

+0

metrics.widthPixels - 這是顯示尺寸...顯示尺寸遠遠小於我想要的 –

+0

@MaxUsanin抱歉,我改變了這一點。看它。 – TN888

+0

這個答案仍然基於最大屏幕大小,而不是最大的位圖大小來顯示。需要找到的是最大的位圖大小,在大多數手機上都是2048x2048,但這需要以編程方式找到。 –

相關問題