import android.content.Context;
import android.graphics.*;
import android.view.MotionEvent;
import android.view.View;
public class MyDrawView extends View {
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;
public MyDrawView(Context c) {
super(c);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFF000000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
public void clear(){
mBitmap.eraseColor(Color.WHITE);
invalidate();
System.gc();
}}
A chamada da função é a seguinte na sua activity
LayoutInflater factory = LayoutInflater.from(SuaActivity.this);
final MyDrawView myDrawView;
final RelativeLayout parent;
final View textEntryView = factory.inflate(R.layout.assinatura, null);
final Button butLimpar = (Button) textEntryView.findViewById(R.id.butLimpar);
parent = (RelativeLayout) textEntryView.findViewById(R.id.signImageParent);
myDrawView = new MyDrawView(this);
parent.setBackgroundColor(Color.WHITE);
parent.addView(myDrawView);
butLimpar.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
myDrawView.clear();
}
});
AlertDialog.Builder dialogs = new AlertDialog.Builder(SuaActivity.this);
dialogs.setTitle("Assinatura");
dialogs.setView(textEntryView);
dialogs.setNegativeButton("Fechar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
dialogs.setPositiveButton("Gravar Assinatura", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
File root = Environment.getExternalStorageDirectory();
arquivo = new File(root,"assinatura".png");
try {
if (arquivo.exists()){
arquivo.delete();
Log.e(tag,"Arquivo "+arquivo.toURL().toString());
}else{
arquivo.createNewFile();
Log.e(tag,"Arquivo Novo");
}
} catch (IOException e) {
Log.e(tag ,e.getMessage());
}
parent.setDrawingCacheEnabled(true);
Bitmap b = parent.getDrawingCache();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(arquivo);
} catch (FileNotFoundException e) {
Log.i(tag,e.toString());
}
b.compress(CompressFormat.PNG,100, fos);
Log.i(tag,"arquivo criado");
imprimir();
}
});
dialogs.create();
dialogs.show();
o XML do dialog é
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/signImageParent"
android:layout_width="1200dp"
android:layout_height="200dp"
android:layout_weight="0.00" >
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/butLimpar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Limpar" />
</LinearLayout>
</LinearLayout>
e no AndroidManifest tem que adicionar a seguinte linha para poder ter acesso ao cartão de memória e poder gravar a imagem
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Esse código é interessante, porque ele se aplica também para desenhar e até escrever