2012-03-15 12 views
0

私が使用しているイメージは4x6からです。したがってアスペクト比は約0.66にすべきです。私のコードでアスペクト比を計算すると、約0.66になります。しかし、表示される画像の高さは潰れて見えます。アスペクト比を約0.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

関連する問題