Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Tutorial: [Android] How to draw an image

This is a really basic thing, but it's just took me hours to work out how to do, and Google said nothing useful at all on the matter. So, I'm posting this just so that next time someone googles it, they have a quick response.

How to draw an image in Android

1. Save an image, such as a .png, in the folder myGame/res/drawable-mdpi
2. Eclipse will load this and assign a variable to it that you can use

Then:

import android.graphics.BitmapFactory;


In your draw method of your class:

g.drawBitmap(BitmapFactory.decodeResource(activity.getResources(), R.drawable.background1), 0, 0, null);


Where g is your canvas.

Here, "background1" is the name of your image.

You'll need a Context. Everyone will throw around the word Context without giving you a clue in hell as to what it means.

Well, Activity is a Context. So, you'll need to pass "this" in when initializing your class in your activity.

For example:

Background background = new Background(width, height, this);

// in your class

public Background(int width, int height, Activity a)
{
activity = a;
}


Anyway I hope it's useful.

And I can guarantee that magically as soon as I've posted this somebody will reply and say "that's simple! You actually only need to x, y, z"

Such is the way of bloody java programming.

I'm on a horse.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top