Friday, May 14, 2010

Draw something on a Canvas

Simple draw something (drawRect) on a Canvas over a bitmap on a View.



Modify from the previous article "Draw a bitmap on View". In order to draw something, a object of Paint class have to been create, and set some parameter; such as color, style, Strok.

Modify the code:
package com.exercise.AndroidPaint;

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

public class AndroidPaint extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setContentView(new myView(this));
}

private class myView extends View{

public myView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

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

Paint myPaint = new Paint();
myPaint.setColor(Color.GREEN);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(3);
canvas.drawRect(10, 10, 100, 100, myPaint);
}
}
}


Download the files.

Next: Android FaceDetector
Next: Custom View with User Interaction



3 comments:

Unknown said...
This comment has been removed by the author.
Anonymous said...

Hello Can you help me to draw a nXn metric over the image. My my image zoomable and Panning with the finger. How can i Draw a Grid over the image from left top corner of the image but not from left top corner of the view.

Unknown said...

can u pls help me how to draw ruler (scale)using canvas and apply user interaction with it so that user can select any number by touching ruler