我正在制作一个屏幕,我需要我的gBall在屏幕中央对齐。 代码如下。
@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
}
但是我将它与屏幕左侧对齐,任何人都可以帮忙吗?
答案 0 :(得分:3)
你得到它在屏幕的左边,因为你的X值设置为0.它应该是
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);
另外,不要在onDraw()方法中初始化你的绘画。
答案 1 :(得分:-1)
使用此代码做对了!
canvas.drawBitmap(gball, (canvas.getWidth()/2-(gball.getWidth()/2)), (float) (canvas.getHeight() * .25), null);