2012-03-15 45 views
0

我使用的圖像來自4x6。所以長寬比應該是大約.66。當我計算我的代碼中的縱橫比時,我會在.66左右。但是,顯示的圖像的高度看起來很糟糕。如果我手動將縱橫比設置爲大約.85,圖像看起來很正確。縮放並繪製位圖時寬高比關閉

由於手動設置寬高比並不完美,我怎樣才能使我的圖像高度看起來不會被壓扁。

public class SpotGameActivity extends Activity { 

private Bitmap mBitmap; 

/** Called when the activity is first created. */ 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dpi2b); 
    setContentView(new BitMapView(this, mBitmap)); 

} 

    class BitMapView extends View { 
     Bitmap mBitmap = null; 

     public BitMapView(Context context, Bitmap bm) { 
      super(context); 
      mBitmap = bm; 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      // called when view is drawn 
      Paint paint = new Paint(); 
      paint.setFilterBitmap(true); 
      // The image will be scaled so it will fill the width, and the 
      // height will preserve the image’s aspect ration 
      float aspectRatio = ((float) mBitmap.getWidth())/mBitmap.getHeight(); 
      Rect dest = new Rect(0, 0, this.getWidth(),(int) (this.getHeight() * aspectRatio)); 

      String AR = Double.toString(aspectRatio); 

      //Rect dest = new Rect(0, 0, getWidth(), getHeight()); 

      canvas.drawBitmap(mBitmap, null, dest, paint); 
      Toast.makeText(SpotGameActivity.this, AR, Toast.LENGTH_SHORT).show(); 
     } 
    } 

}

回答

3

縱橫=寬度/高度

newHeight = newWidth /縱橫

執行此,

Rect dest = new Rect(0, 0, this.getWidth(),(int) (this.getWidth()/aspectRatio)); 
+0

謝謝,Shubhayu。 – user465001 2012-03-15 09:36:04

相關問題