I am new to the Android Graphics class. I want to draw an image(actually a signature kind) using the touch events and want it to be saved on SDcard when I want to save it. I
the drawing thing
Scribbler.java:
package org.yourpackage.scribble;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
public class Scribbler extends Activity {
DrawView drawView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
drawView.requestFocus();
}
}
DrawView.java:
package org.yourpackage.scribble;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class DrawView extends View implements OnTouchListener {
List points = new ArrayList();
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
paint.setColor(Color.BLACK);
}
@Override
public void onDraw(Canvas canvas) {
for (Point point : points) {
canvas.drawCircle(point.x, point.y, 2, paint);
}
}
public boolean onTouch(View view, MotionEvent event) {
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
points.add(point);
invalidate();
return true;
}
}
class Point {
float x, y;
}
AndroidManifest.xml:
..will give you something like that:

You may want to draw lines instead of pixels
the saving thing
To implement saving you can pass a Bitmap into the Canvas constructor, then use the compress
method of Bitmap to create an OutputStream which can be written to the SD card
EDIT to answer your comment:
Sure you can use XML to define your layout since DrawView extends View you can use it in your layout xml file.
An example for the main.xml layout file
that gives you something like that:

You'll just need an additional contstructor public DrawView(Context context, AttributeSet attrSet)