2015-04-04 57 views
1

我正在處理兩個問題,試圖在Android應用程序中顯示Gallery中的照片。我想要做的很簡單:從畫廊獲取一張照片,並將其放入MainActivity中的ImageView(100dp * 100dp)中。如何正確顯示位圖?

第一個問題是,在某些電話上,如索尼Xperia,照片在ImageView上設置時會旋轉。爲了解決這個問題,我發現這是一段代碼蘇答案:

public Bitmap decodeFile(String path) 
{ 
    int orientation; 

    try { 
     if (path == null) { 
      return null; 
     } 
     // decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     // Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE = 70; 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 0; 

     while (true) { 
      if (width_tmp/2 < REQUIRED_SIZE 
        || height_tmp/2 < REQUIRED_SIZE) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale++; 
     } 
     // decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     Bitmap bm = BitmapFactory.decodeFile(path, o2); 
     Bitmap bitmap = bm; 

     ExifInterface exif = new ExifInterface(path); 

     orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 

     Log.e("ExifInteface .........", "rotation =" + orientation); 

     // exif.setAttribute(ExifInterface.ORIENTATION_ROTATE_90, 90); 

     Log.e("orientation", "" + orientation); 
     Matrix m = new Matrix(); 

     if ((orientation == ExifInterface.ORIENTATION_ROTATE_180)) { 
      m.postRotate(180); 
      // m.postScale((float) bm.getWidth(), (float) bm.getHeight()); 
      // if(m.preRotate(90)){ 
      Log.e("in orientation", "" + orientation); 
      bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), 
        bm.getHeight(), m, true); 
      return bitmap; 
     } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { 
      m.postRotate(90); 
      Log.e("in orientation", "" + orientation); 
      bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), 
        bm.getHeight(), m, true); 
      return bitmap; 
     } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { 
      m.postRotate(270); 
      Log.e("in orientation", "" + orientation); 
      bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), 
        bm.getHeight(), m, true); 
      return bitmap; 
     } 
     return bitmap; 
    } catch (Exception e) { 
     return null; 
    } 
} 

完美的作品,但我也希望圖像是一個正方形,這是不是這樣的了。

爲了做到這一點,我用我的位圖的第一個方法後,我也叫一個:

public static Bitmap cropToSquare(Bitmap bitmap) 
{ 
    int width = bitmap.getWidth(); 
    int height = bitmap.getHeight(); 
    int newWidth = (height > width) ? width : height; 
    int newHeight = (height > width)? height - (height - width) : height; 
    int crop = (width - height)/2; 
    crop = (crop < 0)? 0: crop; 
    Bitmap cropImg = Bitmap.createBitmap(bitmap, crop, 0, newWidth, newHeight); 

    return cropImg; 
} 

它確實開啓了位圖成正方形,但問題是它削減照片,而不是重新調整它。 (基本上一半的圖像丟失)

我很確定我想要做的事很簡單,我該怎麼做?

回答

2

而不是使用,

Bitmap.createBitmap(bitmap, crop, 0, newWidth, newHeight); 

使用此線下方,

Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false); 
+0

它完美,謝謝。 – Kritias 2015-04-04 13:42:14