Reputation: 5216
I am making a screen where i need my gBall to align at the center of the screen. The code is as follows.
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.RED);
Paint textPaint = new Paint();
textPaint.setColor(Color.BLUE);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTextSize(canvas.getWidth() / 7);
textPaint.setTypeface(font);
canvas.drawText("MY Text", (canvas.getWidth() / 2),
(float) (canvas.getHeight() * 0.75), textPaint);
canvas.drawBitmap(gball, 0, (float) (canvas.getHeight() * .25), null);//This is where i need help
}
But i am getting it aligned to left of the screen, can anyone help?
Upvotes: 0
Views: 4707
Reputation: 589
You get it at the left of the screen because your X value is set to 0. It should be
int startX= (canvas.getWidth()-gBall.getWidth())/2;//for horisontal position
int startY=(canvas.getHeight()-gBall.getHeight())/2;//for vertical position
canvas.drawBitmap(gball, startX, startY, null);
Also, do not initialize your paint in onDraw() method.
Upvotes: 4
Reputation: 5216
Got it right wth this code!
canvas.drawBitmap(gball, (canvas.getWidth()/2-(gball.getWidth()/2)), (float) (canvas.getHeight() * .25), null);
Upvotes: -1