如何在“库”视图中旋转图像

时间:2011-05-18 20:29:02

标签: android gallery

这是一个简单的问题,但我想知道它是否有非昂贵的解决方案。

我有一个图库视图,可以从内存中加载图像,SD卡或资源。它们的大小为480x800,因此代码会对它们进行采样并在Gallery View中显示为缩略图。一切正常,有点......

有些图像是风景而有些是肖像。画廊的imageview布局将它们全部显示为风景,因此所有肖像图像看起来都非常紧张!

由于这些图像是抽象绘画,我想旋转肖像,以便它们都很好地均匀地适合画廊。我可以通过比较它们的宽度和高度来检测它们的方向,然后使用Matrix和canvas旋转纵向,但这可能太昂贵而且在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);

1 个答案:

答案 0 :(得分:0)

输入Matrix !!!

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