This is a pretty vague question, but I'll give it a shot. This is my first answer on this site and I am by no means an expert but I have found myself doing a lot of tinkering with drawing in Android.
From what I've read and experienced, each view has a bitmap, which is used to draw the View on the screen. Each view also has a canvas. The canvas is what allows the programmer to control what is drawn onto this bitmap.
Every View object has an onDraw(Canvas c)
method which is used to draw it. If you want to draw something yourself, you can create a subclass of the View
class by extending View and you can override the onDraw(Canvas c)
method to draw whatever you want. You draw onto the view using the Canvas
object provided as a parameter to the onDraw()
method.
A drawable is simply an object that can be drawn. This could be an still image (bmp, png, jpg,etc), icon, animated gif, etc. A drawable is usually created from an existing image you want to draw onto the screen. This is done in two steps: including the image into your project, then drawing the image.
To include an image into your project you can simply drag it into one of the res/drawable folders in your project directory in Eclipse.
Once the image file is included into your project, the R.java file will be automatically updated with a unique id for that image file. To load the image file as a drawable in your code, you would do something like Drawable d = getResources().getDrawable(R.id.imagefile);
. To draw it on the canvas, you could set the size and location using the d.setBounds()
method and you could use d.draw(canvas)
in your onDraw()
method to draw it in your view.
The canvas object provided by the onDraw() method has many useful functions for drawing onto the view. Play around with it, that's the best way you can learn about how to use it. Also, don't forget to check out the Android developer site to see a full listing of all methods.
What exactly are you hoping to do with drawing? If you are trying to make something like a game, you should probably look into using the SurfaceView
class.
Here's an example of a custom view class:
public class CustomView extends View{
public void onDraw(Canvas c){
c.drawColor(Color.RED);
}
}
This view, when created, should just draw itself filled with the color red.