Learn Android for Free,issues for developers, mobengineers website provide complete solutions for developers issues posts.It also provides online training for android development

Saturday, November 24, 2012


Graphics in Android

Android API provides allow to draw custom graphics onto a canvas or to modify existing views to look good.

 We we draw graphics, we can do it in 2 ways.

1. draw the graphics into a view object from layout.
2. draw the graphics directly to the Canvas.

To draw something, we need 4 basic components.

1. A Bitmap to hold the pixels.
2. A canvas to host the draw  calls- writing into the Bitmap.
3. Draw primitive- like draw rectangle, text, path etc.
4. Paint- describes the colors and styles to draw.

Bitmap to hold the pixels:

It's a simple example to draw a bitmap on screen in View. The content view is set to a View, and the drawing is achieved in onDraw method.
To draw a bitmap on screen in View, Content view is set to view and dwaing is get in onDraw().

 A canvas to host the draw  calls- writing into the Bitmap:
Canvas works as interface to actual surface upon which graphics will drawn.
It holds all "draw" calls.With canvas, drawing is actually performed underlying Bitmap, which is placed into Window.

 Bitmap bm=BitmapFactory.decodeResource(getResources(),R.drawable.image);


To draw color

here is the simple example program

package com.srinivas.graphics;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        setContentView(new NewView(this));
    private class NewView extends View

        public NewView(Context context) {
            // TODO Auto-generated constructor stub

        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            Bitmap bm=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
            canvas.drawBitmap(bm, 0,0, null);

download source code here


it is a subclass of View. By using this we can draw surface within the View hierarchy.

The SurfaceView is a special subclass of View that offers a dedicated drawing surface within the View hierarchy. The aim is to offer this drawing surface to an application's secondary thread, so that the application isn't required to wait until the system's View hierarchy is ready to draw. Instead, a secondary thread that has reference to a SurfaceView can draw to its own Canvas at its own pace.
To begin, you need to create a new class that extends SurfaceView. The class should also implement SurfaceHolder.Callback.

0 coment�rios:

Post a Comment

20% off

Online Training

Your Name :
Your Email: (required)
Your Message: (required)

NDTV Gadgets360

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts