3

I am new to android graphic programming. I want to put a bitmap in the centre of my canvas. Hence, i use :

public void onDraw(Canvas canvas) {
    float canvasx = (float) canvas.getWidth();
    float canvasy = (float) canvas.getHeight();

Then i call the bitmap i want to use,

Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),
        R.drawable.myBitmap);

Then i find the coordinate location for my bitmap by using these,

float bitmapx = (float) myBitmap.getWidth();
float bitmapy = (float) myBitmap.getHeight();

float boardPosX = (canvasx - bitmapx) / 2;
float boardPosY = (canvasy - bitmapy) / 2;

Finally, i draw the bitmap using,

canvas.drawBitmap(myBitmap, boardPosX, boardPosY, null);

But, the bitmap is not in the center of the canvas. It's a little bit below the position which i reckon should be the center of the canvas.

Is it correct to get the canvas height and width inside the onDraw() method ? Any idea what's wrong ? Thanks in advance.

*Edit :

Finally, i make it work by changing

public void onDraw(Canvas canvas) {
    float canvasx = (float) canvas.getWidth();
    float canvasy = (float) canvas.getHeight();

to

public void onDraw(Canvas canvas) {
    float canvasx = (float) getWidth();
    float canvasy = (float) getHeight();

However, i dont know why the change fixes my problem.

JrL
  • 221
  • 1
  • 7
  • 19

2 Answers2

2

Use this:

float boardPosX = ((canvasx/2) - (bitmapx / 2));
float boardPosY = ((canvasy/2) - (bitmapy / 2));
Todd Davies
  • 5,484
  • 8
  • 47
  • 71
  • thanks, but it doesn't work either. Still puts the bitmap below the center of the canvas. – JrL Jul 26 '12 at 12:45
  • 1
    Is there any padding around the edge of the image. No transparent pixels on one side or anything? – Todd Davies Jul 26 '12 at 12:49
  • There are transparent pixels of maybe 3-5 pixels wide. But the difference between the canvas' center and the bitmap position is more than 15 pixels. The thing is that i printed-out the result from the formula above and lets say it gives me x=10 y=20. >> canvas.drawBitmap(myBitmap, 10, 20, null); this one puts the bitmap correctly at the center. But, >> canvas.drawBitmap(myBitmap, boardPosX, boardPosY, null); this one puts the bitmap a bit below the center. – JrL Jul 26 '12 at 12:58
  • You could try using int's instead of float's but I don't think that's the problem. I'm not sure what else I can do without the source code :( – Todd Davies Jul 26 '12 at 13:00
  • I tried to convert it to int and float. It frustrates me that none of it works. Thanks though. – JrL Jul 26 '12 at 13:06
  • by the way, is it correct to get the canvas height and width inside onDraw method ? – JrL Jul 26 '12 at 13:12
  • I'm not sure, ask a question, somebody else'll know! – Todd Davies Jul 26 '12 at 13:13
1
private int mWidth;
private int mHeight;
private float mAngle;

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    mWidth = View.MeasureSpec.getSize(widthMeasureSpec);
    mHeight = View.MeasureSpec.getSize(heightMeasureSpec);

    setMeasuredDimension(mWidth, mHeight);
}

@Override protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.compass);

    // Here's the magic. Whatever way you do it, the logic is:
    // space available - bitmap size and divide the result by two.
    // There must be an equal amount of pixels on both sides of the image.
    // Therefore whatever space is left after displaying the image, half goes to
    // left/up and half to right/down. The available space you get by subtracting the
    // image's width/height from the screen dimensions. Good luck.

    int cx = (mWidth - myBitmap.getWidth()) >> 1; // same as (...) / 2
    int cy = (mHeight - myBitmap.getHeight()) >> 1;

    if (mAngle > 0) {
        canvas.rotate(mAngle, mWidth >> 1, mHeight >> 1);
    }

    canvas.drawBitmap(myBitmap, cx, cy, null);
}
Android
  • 8,995
  • 9
  • 67
  • 108