Drawing using Canvas in Android


In This tutorials We learn that how you can draw Images using canvas technology.
canvas used for drawing geometrical shapes. Painting the shape using paint object . and it also used for drawing bitmap images.
Fist of all we create inner class and extend it from View class.
Now we override the onDraw() method of View class. all the drawing are implemented in the onDrea(Canvas c) method. In setContentView() write the new class name to set customize View instead of layout view
Now let the fun Start



public class CanvasTesting extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(new Panel(this));
}
//inner class extending View
class Panel extends View {
private Bitmap myBm;
private Canvas cc;
private Handler hand=new Handler();
private int x=0;
public Panel(Context context) {
super(context);
myBm = BitmapFactory.decodeResource(getResources(), R.drawable.myImage);
}

@Override
public void onDraw(Canvas canvas) {
cc=canvas;
// paint used for fill shape with different color and styles
Paint p=new Paint();
p.setColor(Color.GREEN);
cc.drawBitmap(bm5, 0, 0, p);
;

}

}

}

Post a Comment

0 Comments