如何在画布中添加.png?

时间:2014-03-31 16:31:15

标签: android canvas

我有一个球被反弹的游戏..我想用png格式的足球替换drawen球。

我尝试过很多东西,但没有任何效果。

这是我绘制球的代码:

    public ball() {
    position = new Vector(0.0f, 0.0f);
    paused_velocity = velocity = new Vector(0.0f, 0.0f);
    paused_acceleration = acceleration = new Vector(0.0f, gravity);

    radius = 0.0f;
    delta = 0;

    ball_paint = PaintList.getRandPaint();

    start_time = SystemClock.uptimeMillis();
}

public void draw(Canvas canvas) {

    canvas.drawCircle(position.X(), position.Y(), radius, ball_paint);
}

非常感谢

1 个答案:

答案 0 :(得分:0)

如果你的png在drawable目录中出现了一个ball.png ..

public Bitmap getBallBitmap()
{
    return BitmapFactory.decodeResource(getResources(),
                        R.drawable.ball);
}

@Override
protected void draw(Canvas canvas)
{
    //ideally you should cache the value returned by getBallBitmap()
    canvas.drawBitmap(getBallBitmap(),position.X(), position.Y(), null);
}

请注意,您应尽可能使用onDraw()方法而不是draw()方法。