2011-05-18 15 views
2

這是一個簡單的問題,但我不知道它是否有非昂貴的解決方案。如何旋轉圖庫視圖中的圖像

我有一個畫廊視圖加載從內存,SD卡或資源的圖像。它們的尺寸是480x800,因此代碼將它們縮小並在「圖庫視圖」中顯示爲縮略圖。所有的作品都很好,有點...

一些圖像是一些肖像的風景。畫廊的imageview佈局將它們全部顯示爲風景,因此所有人像圖像看起來都非常緊張!

由於這些圖像是抽象的圖紙,我想旋轉肖像的圖片,使它們都很好地均勻地適合畫廊。我可以通過比較它們的寬度和高度來檢測它們的方向,然後使用矩陣和畫布旋轉畫像的方向,但這可能太昂貴並且很慢,無法在BaseAdapter的getView()中運行。

任何人都可以想出一個更便宜的方法來做到這一點嗎?

public View getView(int position, View convertView, ViewGroup parent){ 
     ImageView i = new ImageView(mContext); 
     Bitmap bm = null; 

     if (mUrls[position] != null){ 
      bm = BitmapFactory.decodeFile(mUrls[position].toString(),options); 
      if (bm != null){ 
       //here could use a bitmap rotation code 
       //.... 
       i.setImageBitmap(bm); 
      } 
     }else{ 
      bm = BitmapFactory.decodeResource(getResources(), R.drawable.deleted); 
      i.setImageBitmap(bm); 
     } 
     i.setScaleType(ImageView.ScaleType.FIT_XY); 
     i.setLayoutParams(new Gallery.LayoutParams(200, 130)); 
     i.setBackgroundResource(mGalleryItemBackground); 
     return i; 
    } 

編輯

經過一些測試我使用下面的代碼轉動這不是超快速的圖像,但它可以在瞬間做。

int w = bm.getWidth(); 
int h = bm.getHeight(); 
if (w < h){ 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(-90); 
    bm = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, false);  
} 
i.setImageBitmap(bm); 

回答

0

輸入矩陣!

Resize and Rotate Image - Example

對我來說是非常有用的,希望它有助於

if (bm != null){ 
     Matrix matrix = new Matrix(); 
     matrix.postScale(scaleWidth, scaleHeight); 
     matrix.postRotate(45); 
     Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true); 
     BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); 
     i.setImageBitmap(bmd); 
     } 

回收的作品,我建議使用的AsyncTask加載圖片這避免了UI塊。

這裏有一個例子::

http://android-apps-blog.blogspot.com/2011/04/how-to-use-asynctask-to-load-images-on.html

另一個例子::

http://open-pim.com/tmp/LazyList.zip

+0

你建議創建一個新的位圖或繪製它從predefiened位圖導出的畫布上?我知道該怎麼做,但是因爲在每一個項目上都會調用getView,所以在這種情況下,我會懷疑循環是否會起作用,並且所有這些都可能會減慢並破壞畫廊滾動的平滑度和速度,這實際上是其非常重要的特性。 – Lumis 2011-05-18 21:47:20

+0

謝謝,看來使用矩陣和createBitmap是做到這一點的最短途徑,我現在可以弄清楚。 – Lumis 2011-05-19 19:14:47