For Andriod I have this code:
public Tank(int color) {
bounds = new RectF();
paint = new Paint();
paint.setColor(color);
}
public void draw(Canvas canvas) {
bounds.set(x - radius, y - radius, x + radius, y + radius);
canvas.drawRect(bounds, paint);
}
where I am drawing a Rect, but now I want to draw a Bitmap instead of a Rect, but
bitTank = BitmapFactory.decodeRescource(getRescource(),R.drawable.ic_launcher);
or
bitTank = BitmapFactory.decodeFile("C:\Users\...\res\drawable-hdpi\ic_launcher.png");
(both) in combination with
canvas.drawBitmap(bitTank, matrix, null);
is not working.
The first does not know getRescource() and with the second one it doesn’t work anymore. How can I realize that? (The Code is in the class Tank and another class calls the draw function).
The second version simply cannot work, because you are trying to access a file on your PC from the inside Android app. Android doesn’t know anything about your local PC.
With the first code, you need an instance of
Contextto access resources. You can pass a context into your constructor and then use it:While this is not the only way to achieve what you’re after, it should get you started.