Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2D drawing with Android

Tags:

android

Im trying to understand how the Android drawings work.

I hoping that someone can explain how the drawing components relate to each other (View, Drawable, Canvas, Bitmap)

It all seems very confusing and the documentation doesnt do a very good job explaining it.

Does the bitmap inside the Canvas object you get injected in your View's via onDraw() represent the whole display, or just the chunk that the view draws?

What does the drawable do, are they just objects that encapsulate a set of commands to a Canvas object?

Im hoping someone can help me get a basic understanding on how it works, i dont have eny java background only Action script and C# (silverlight).

like image 643
Mads Lee Jensen Avatar asked May 02 '26 15:05

Mads Lee Jensen


1 Answers

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.

like image 190
SSZero Avatar answered May 04 '26 03:05

SSZero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!