基本上我想用drawBitmap方法替换drawcircle方法。我的想法是用我导入的图像替换圆圈。
这是我的资源方法
// Create the bitmap object using BitmapFactory
// Access the application resource, and then retrieve the drawable
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.ball);
我想更改drawcircle方法,而是用drawbitmap替换它。
// Create a new class extended from the View class
class GameView extends View
{
Paint paint = new Paint();
// Constructor
public GameView(Context context)
{
super(context);
setFocusable(true);
}
// Override the onDraw method of the View class to configure
public void onDraw(Canvas canvas)
{
// Configure the paint which will be used to draw the view
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
// If the game is over
if (isLose)
{
paint.setColor(Color.RED);
paint.setTextSize(40);
canvas.drawText("Game Over", 50, 200, paint);
}
// Otherwise
else
{
// set the color of the ball
paint.setColor(Color.rgb(240, 240, 80));
canvas.drawCircle(ballX, ballY, BALL_SIZE, paint);
// set the color of the racket
paint.setColor(Color.rgb(80, 80, 200));
canvas.drawRect(racketX, racketY, racketX + RACKET_WIDTH,
racketY + RACKET_HEIGHT, paint);
}
}
我知道我必须以某种方式取代canvas.drawCircle,但到目前为止我所做的每一次尝试都没有效果。 如果有人可以提供帮助,那将非常感激。
答案 0 :(得分:0)
更改以下代码:
// set the color of the ball
paint.setColor(Color.rgb(240, 240, 80));
canvas.drawCircle(ballX, ballY, BALL_SIZE, paint);
为:
canvas.drawBitmap(bitmap, null, destRect, null);
在上面的代码中,位图是指您在代码中创建的位图,而desRect是一个确定在何处绘制该位图的Rect。可以这样计算:
Rect destRect = new Rect(ballX - BALL_SIZE, ballY - BALL_SIZE, ballX + BALL_SIZE, ballY + BALL_SIZE);
请记住在onDraw方法之外计算它,以防出现效率问题。