這是一個簡單的問題,但我不知道它是否有非昂貴的解決方案。如何旋轉圖庫視圖中的圖像
我有一個畫廊視圖加載從內存,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);
你建議創建一個新的位圖或繪製它從predefiened位圖導出的畫布上?我知道該怎麼做,但是因爲在每一個項目上都會調用getView,所以在這種情況下,我會懷疑循環是否會起作用,並且所有這些都可能會減慢並破壞畫廊滾動的平滑度和速度,這實際上是其非常重要的特性。 – Lumis 2011-05-18 21:47:20
謝謝,看來使用矩陣和createBitmap是做到這一點的最短途徑,我現在可以弄清楚。 – Lumis 2011-05-19 19:14:47