How can I center this rectangle and image in the y axis?
<p>So far, my image starts at 0,0 and scales properly. There is a black bar under it. How can I align it so that it is centered along the y, with a black bar above and below the image?</p>

<p>edit: also, what are "this.getWidth(), this.getHeight()" referring to when I make the object "dest" - the width of mBitmap? The documentation does say they represent the width of the view, I just want to make sure I'm correct in thinking that is it mBitmap.</p>

<pre><code>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) {


        //DisplayMetrics metrics = new DisplayMetrics();
        //getWindowManager().getDefaultDisplay().getMetrics(metrics);

        // 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.getWidth() / aspectRatio));
        String AR = Double.toString(aspectRatio);
        canvas.drawBitmap(mBitmap, null, dest, paint);
        Toast.makeText(SpotGameActivity.this, AR, Toast.LENGTH_SHORT).show();   
    }
</code></pre>

<p>I've now gotten the difference between the view size and image size. I've divided the difference by 2 but now, my image is align to the bottom of the screen rather than the middle of the y-axis. any tips?:</p>

<pre><code>@Override
    protected void onDraw(Canvas canvas) {


        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int scrHeight = this.getHeight();
        String sH = Integer.toString(scrHeight);
        Log.v(TAG, "scrHeight =" + sH );
        int imgHeight= mBitmap.getHeight();
        String iH = Integer.toString(imgHeight);
        Log.v(TAG, "imgHeight =" + iH );

        int diff = scrHeight - imgHeight;
        String dif = Integer.toString(diff);
        Log.v(TAG, "diff =" + dif );


        int dvd= diff/2;

        // 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, dvd, this.getWidth(),(int) (this.getWidth() / aspectRatio)+dvd);
        String AR = Double.toString(dvd);

        canvas.drawBitmap(mBitmap, null, dest, paint);

        Toast.makeText(SpotGameActivity.this, AR, Toast.LENGTH_SHORT).show();   
</code></pre>
