Java android中的位图canvas.drawBitmap方法
基本上我想用drawBitmap方法替换drawcircle方法。这个想法是用我导入的图像替换圆 这是我的资源方法Java android中的位图canvas.drawBitmap方法,java,android,eclipse,canvas,bitmap,Java,Android,Eclipse,Canvas,Bitmap,基本上我想用drawBitmap方法替换drawcircle方法。这个想法是用我导入的图像替换圆 这是我的资源方法 // Create the bitmap object using BitmapFactory // Access the application resource, and then retrieve the drawable Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
// 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,但到目前为止我所做的每一次尝试都没有成功。
如果有人能帮忙,我将不胜感激 更改以下代码:
// set the color of the ball
paint.setColor(Color.rgb(240, 240, 80));
canvas.drawCircle(ballX, ballY, BALL_SIZE, paint);
致:
在上面的代码中,位图是指您在代码中创建的位图,而desRect是一个决定在何处绘制位图的Rect。可以这样计算:
Rect destRect = new Rect(ballX - BALL_SIZE, ballY - BALL_SIZE, ballX + BALL_SIZE, ballY + BALL_SIZE);
请记住在onDraw方法之外进行计算,以防出现效率问题。我希望将canvas.drawCircle替换为看起来像球的图像,而不是只绘制一个圆。
Rect destRect = new Rect(ballX - BALL_SIZE, ballY - BALL_SIZE, ballX + BALL_SIZE, ballY + BALL_SIZE);