Para un proyecto en el que estoy trabajando me han pedido una aplicación para Android que que permita pintar en pantalla y capturar la imagen. Os dejo el código por si a alguno le interesa. Primero creo la vista que capturará los eventos de pantalla y pintará en en canvas.
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.util.*;
public class DrawView extends View implements OnTouchListener {
private static final String TAG = "DrawView";
List points = new ArrayList();
Paint paint = new Paint();
List newLine = new ArrayList();
private Boolean clearCanvas = false;
public DrawView(Context context, AttributeSet attrs){
super(context, attrs);
DrawViewFunction();
}
public DrawView(Context context) {
super(context);
DrawViewFunction();
}
public void DrawViewFunction()
{
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
}
@Override
public void onDraw(Canvas canvas) {
if(clearCanvas)
{ // Choose the colour you want to clear with.
canvas.drawColor(Color.WHITE);
//canvas.drawColor(0, Mode.CLEAR);
clearCanvas = false;
points.clear();
}
for (int i = 0; i<points.size(); i++) {
Point newPoint = new Point();
Point oldPoint = new Point();
if (newLine.contains(i)||i==0){
newPoint = points.get(i);
oldPoint = newPoint;
} else {
newPoint = points.get(i);
oldPoint = points.get(i-1);
}
canvas.drawLine(oldPoint.x, oldPoint.y, newPoint.x, newPoint.y, paint);
}
}
public void clearCanvas()
{
clearCanvas = true;
invalidate();
}
public boolean onTouch(View view, MotionEvent event) {
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
points.add(point);
invalidate();
Log.d(TAG, "point: " + point);
if(event.getAction() == MotionEvent.ACTION_UP){
// return super.onTouchEvent(event);
newLine.add(points.size());
}
return true;
}
public Bitmap getBitmap() {
//Define a bitmap with the same size as the view
View view = this;
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
}
class Point {
float x, y;
@Override
public String toString() {
return x + ", " + y;
}
}
Luego implementaré la actividad que meneja la vista pintable.
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity{
Button btnTakePicture;
Button btnClearDraw;
DrawView vistaPintar;
Bitmap capturaPantalla;
ImageView vistaCaptura;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnTakePicture = (Button)findViewById(R.id.button1);
btnClearDraw = (Button)findViewById(R.id.btnClear);
vistaPintar = (DrawView) findViewById(R.id.drawView1);
vistaCaptura = (ImageView) findViewById(R.id.imageView1);
btnTakePicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
capturaPantalla = vistaPintar.getBitmap();
Drawable drawable = new BitmapDrawable(capturaPantalla);
vistaCaptura.setImageDrawable(drawable);
}
});
btnClearDraw.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
vistaPintar.clearCanvas();
vistaCaptura.setImageDrawable(null);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Espero que os sirva de ayuda