title: How to rotate an image in a Gallery view
body: <p>This is a simple problem, but I wonder if it has a non-expensive solution.</p>
  
  <p>I have a gallery view which loads images from memory, either SD card or resources. Their size is 480x800, so the code samples them down and show as thumbnails in a Gallery View. All works fine, kind of...</p>
  
  <p>Some images are landscape while some portrait. The gallery’s imageview layout shows them all as landscape thus all portrait images look badly stretched!</p>
  
  <p>Since these images are abstract drawings I would like to rotate the portrait ones so that they all nicely and evenly fit in the gallery. I could detect their orientation by comparing their width and height and then rotate the portrait ones using Matrix and canvas, but that may be too expensive and slow to run in getView() of a BaseAdapter.  </p>
  
  <p><strong>Can anybody think of a less expensive way to do it?</strong> </p>
  
  <pre><code>    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;   
      }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>After some testing I am using the code below to rotate the image which is not super-fast but it can do at the moment.</p>
  
  <pre><code>int w = bm.getWidth();
  int h = bm.getHeight();
  if (w &lt; h){
    Matrix matrix = new Matrix(); 
    matrix.postRotate(-90);
    bm = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, false);    
  }
  i.setImageBitmap(bm);
  </code></pre>