Code:
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener {
DrawView drawView;
private GestureDetectorCompat g1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.TRANSPARENT);
setContentView(drawView);
g1 = new GestureDetectorCompat(this,this);
g1.setOnDoubleTapListener(this);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
g1.onTouchEvent(event);
return super.onTouchEvent(event);
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
DrawView.x_touch = e.getX();
DrawView.y_touch = e.getY();
Log.v("id2","message2");
return false;
}
Code for drawing:
public class DrawView extends View {
Paint paint = new Paint();
static float x_touch = -1;
static float y_touch = -1;
public DrawView(Context context) {
super(context);
super.setWillNotDraw(false);
paint.setColor(Color.BLACK);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Display mdisp = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point mdispSize = new Point();
mdisp.getSize(mdispSize);
int maxX = mdispSize.x;
int maxY = mdispSize.y;
canvas.drawLine(maxX/3, 0, maxX/3, maxY, paint);
canvas.drawLine(maxX*2/3, 0, maxX*2/3, maxY, paint);
canvas.drawLine(0, maxY/3, maxX, maxY/3, paint);
canvas.drawLine(0, maxY*2/3, maxX, maxY*2/3, paint);
Log.v("id1","message1");
if (x_touch>0 && y_touch > 0)
canvas.drawText("X",x_touch,y_touch,paint);
}
}
On doing some research I found that one need to call setWillNotDraw(false) while overriding onDraw() or use dispatchDraw().
I have tried both but Draw function is called only once.
I am new to android development.
Change your code as follow :
public class DrawView extends View {
Paint paint = new Paint();
float x_touch = -1;
float y_touch = -1;
public DrawView(Context context) {
super(context);
super.setWillNotDraw(false);
paint.setColor(Color.BLACK);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Display mdisp = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point mdispSize = new Point();
mdisp.getSize(mdispSize);
int maxX = mdispSize.x;
int maxY = mdispSize.y;
canvas.drawLine(maxX/3, 0, maxX/3, maxY, paint);
canvas.drawLine(maxX*2/3, 0, maxX*2/3, maxY, paint);
canvas.drawLine(0, maxY/3, maxX, maxY/3, paint);
canvas.drawLine(0, maxY*2/3, maxX, maxY*2/3, paint);
Log.v("id1","message1");
if (x_touch>0 && y_touch > 0)
canvas.drawText("X",x_touch,y_touch,paint);
}
public void setXandY(float x, float y) {
x_touch = x;
y_touch = y;
this.invalidate();
}
}
And :
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
drawView.setXandY(e.getX(), e.getY());
return (true);
}
Related
I'm trying to draw a Bitmap multiple time on canvas my code gives me this :
Code :
public DrawView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
setOnTouchListener(this);
this.paint.setColor(Color.TRANSPARENT);
this.paint.setAntiAlias(true);
this.mContext = context;
}
public void onDraw(Canvas canvas) {
this.ch = getHeight();
this.cw = getWidth();
for (Point point : this.points) {
canvas.drawBitmap(point.bt, point.x, point.y, null);
}
}
public void drawSingleEmoji(MotionEvent motionEvent, Bitmap singleBitmap) {
this.bt1 = singleBitmap;
this.bt2 = Bitmap.createScaledBitmap(this.bt1, w, h, true);
Point point = new Point();
point.x = motionEvent.getX() - ((float) (this.bt2.getWidth() / 2));
point.y = motionEvent.getY() - ((float) (this.bt2.getHeight() / 2));
point.bt = this.bt2;
this.points.add(point);
invalidate();
}
public boolean onTouch(View view, MotionEvent motionEvent) {
drawSingleEmoji(motionEvent,bitSingle);
return true;
}
public void setEmoji(Bitmap textAsBitmap) {
bitSingle = textAsBitmap;
}
}
How I can adjust the space between each emoji drown on canvas like this :
Please I need the help regarding Undo method , I tried to implement it like the code below but it is not remove the last color,
Kindly help me to resolve it.
awaiting your kind response.
Thanks in Advance.
MainActivity
public class MainActivity extends AppCompatActivity implements
View.OnTouchListener {
Button red, blue, yellow, undo;
Paint paint;
private RelativeLayout drawingLayout;
private MyView myView;
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
/**
* Called when the activity is first created.
*/
/*
*
* private ImageView imageView; private Canvas cv; private Bitmap mask,
* original, colored; private int r,g,b; private int sG, sR, sB;
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myView = new MyView(this);
drawingLayout = (RelativeLayout) findViewById(R.id.relative_layout);
drawingLayout.addView(myView);
red = (Button) findViewById(R.id.btn_red);
blue = (Button) findViewById(R.id.btn_blue);
yellow = (Button) findViewById(R.id.btn_yellow);
undo = (Button) findViewById(R.id.undo);
red.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
paint.setColor(Color.RED);
}
});
yellow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
paint.setColor(Color.YELLOW);
}
});
blue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
paint.setColor(Color.BLUE);
}
});
undo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myView.onClickUndo();
}
});
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
// flood fill
public class MyView extends View {
final Point p1 = new Point();
Bitmap mBitmap;
ProgressDialog pd;
Canvas canvas;
private Path path;
// Bitmap mutableBitmap ;
public MyView(Context context) {
super(context);
paint = new Paint();
paint.setAntiAlias(true);
pd = new ProgressDialog(context);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(5f);
mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.gp1_1).copy(Bitmap.Config.ARGB_8888, true);
this.path = new Path();
}
public void onClickUndo() {
if (paths.size() > 0) {
undonePaths.add(paths.remove(paths.size() - 1));
invalidate();
} else {
Toast.makeText(getContext(), getString(R.string.nomore), Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onDraw(Canvas canvas) {
this.canvas = canvas;
paint.setColor(Color.GREEN);
// int width = drawingLayout.getWidth();
// int height = drawingLayout.getHeight();
// float centerX = (width - mBitmap.getWidth()) * 0.5f;
//float centerY = (height - mBitmap.getHeight()) * 0.5f;
canvas.drawBitmap(mBitmap, 0, 0, paint);
///////////////////////////////
for (Path p : paths) {
canvas.drawPath(p, paint);
//////////////////////////
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
p1.x = (int) x;
p1.y = (int) y;
final int sourceColor = mBitmap.getPixel((int) x, (int) y);
final int targetColor = paint.getColor();
new TheTask(mBitmap, p1, sourceColor, targetColor).execute();
paths.add(path);
invalidate();
}
return true;
}
public void clear() {
path.reset();
invalidate();
}
public int getCurrentPaintColor() {
return paint.getColor();
}
class TheTask extends AsyncTask<Void, Integer, Void> {
Bitmap bmp;
Point pt;
int replacementColor, targetColor;
public TheTask(Bitmap bm, Point p, int sc, int tc) {
this.bmp = bm;
this.pt = p;
this.replacementColor = tc;
this.targetColor = sc;
pd.setMessage(getString(R.string.wait));
pd.show();
}
#Override
protected void onPreExecute() {
pd.show();
}
#Override
protected void onProgressUpdate(Integer... values) {
}
#Override
protected Void doInBackground(Void... params) {
FloodFill f = new FloodFill();
f.floodFill(bmp, pt, targetColor, replacementColor);
return null;
}
#Override
protected void onPostExecute(Void result) {
pd.dismiss();
invalidate();
}
}
}
public class FloodFill {
public void floodFill(Bitmap image, Point node, int targetColor,
int replacementColor) {
int width = image.getWidth();
int height = image.getHeight();
int target = targetColor;
int replacement = replacementColor;
if (target != replacement) {
Queue<Point> queue = new LinkedList<Point>();
do {
int x = node.x;
int y = node.y;
while (x > 0 && image.getPixel(x - 1, y) == target) {
x--;
}
boolean spanUp = false;
boolean spanDown = false;
while (x < width && image.getPixel(x, y) == target) {
image.setPixel(x, y, replacement);
if (!spanUp && y > 0
&& image.getPixel(x, y - 1) == target) {
queue.add(new Point(x, y - 1));
spanUp = true;
} else if (spanUp && y > 0
&& image.getPixel(x, y - 1) != target) {
spanUp = false;
}
if (!spanDown && y < height - 1
&& image.getPixel(x, y + 1) == target) {
queue.add(new Point(x, y + 1));
spanDown = true;
} else if (spanDown && y < height - 1
&& image.getPixel(x, y + 1) != target) {
spanDown = false;
}
x++;
}
} while ((node = queue.poll()) != null);
}
}
}
}
I guess you mean the undo method doesn't work. The possible reason is the previous drawing on the canvas is not removed. Try clearing the canvas before drawing in the onDraw() method.
#Override protected void onDraw(Canvas canvas) {
this.canvas = canvas;
canvas. drawColor(0,PorterDuff.Mode.CLEAR); //This clears the canvas.
paint.setColor(Color.GREEN);
//rest of the code
}
Seems it is impossible to return previous color it is required an array for each point to store the used color in every point.
MainActivity class Code which uses GestureDetector to get coordinate:
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener {
DrawView drawView;
private GestureDetectorCompat g1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.TRANSPARENT);
setContentView(drawView);
g1 = new GestureDetectorCompat(this,this);
g1.setOnDoubleTapListener(this);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
g1.onTouchEvent(event);
return super.onTouchEvent(event);
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
drawView.setXandY(e.getX(), e.getY());
Log.v("id2","message2");
return false;
}
Code which uses this coordinate:
public class DrawView extends View {
Paint paint = new Paint();
static float x_touch = -1;
static float y_touch = -1;
static int [][] arr = new int[][]{{-1,-1,-1},{-1,-1,-1},{-1,-1,-1}};
static int maxX = -1, maxY = -1;
public DrawView(Context context) {
super(context);
super.setWillNotDraw(false);
paint.setColor(Color.BLACK);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Display mdisp = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point mdispSize = new Point();
mdisp.getSize(mdispSize);
maxX = mdispSize.x;
maxY = mdispSize.y;
canvas.drawLine(maxX/3, 0, maxX/3, maxY, paint);
canvas.drawLine(maxX*2/3, 0, maxX*2/3, maxY, paint);
canvas.drawLine(0, maxY / 3, maxX, maxY / 3, paint);//This is first Horizontal line
canvas.drawLine(0, maxY * 2 / 3, maxX, maxY * 2 / 3, paint); //This is second horizontal line
Log.v("id1", "message1");
paint.setTextSize(150f);
if (x_touch>0 && y_touch>0) {
if (x_touch < maxX/3 && y_touch < maxY/3)
canvas.drawText("X", maxX/6, maxY/6, paint);
else if (x_touch>maxX/3 && x_touch<2*maxX/3 && y_touch<maxY/3)
canvas.drawText("X", maxX/2, maxY/6, paint);
else if (x_touch>maxX*2/3 && y_touch<maxY/3)
canvas.drawText("X", 5*maxX/6, maxY/6, paint);
else if (x_touch<maxX/3 && y_touch>maxY/3 && y_touch<2*maxY/3)
canvas.drawText("X", maxX/6, maxY/2, paint);
else if (x_touch>maxX/3 && x_touch<2*maxX/3 && y_touch>maxY/3 && y_touch<2*maxY/3)
canvas.drawText("X", maxX/2, maxY/2, paint);
else if (x_touch>2*maxX/3 && y_touch>maxY/3 && y_touch<2*maxY/3)
canvas.drawText("X", 5*maxX/6, maxY/2, paint);
else if (x_touch<maxX/3 && y_touch>2*maxY/3)
canvas.drawText("X", maxX/6, 5*maxY/6, paint);
else if (x_touch>maxX/3 && x_touch<2*maxX/3 && y_touch>2*maxY/3)
canvas.drawText("X", maxX/2, 5*maxY/6, paint);
else if (x_touch>2*maxX/3 && y_touch>2*maxY/3)
canvas.drawText("X", 5*maxX/6, 5*maxY/6, paint);
Log.v("id3", "maxX="+Float.toString(maxX)+" maxY="+Float.toString(maxY)+
" x_touch="+Float.toString(x_touch)+" y_touch="+Float.toString(y_touch));
}
}
public void setXandY(float x, float y) {
x_touch = x;
y_touch = y;
this.invalidate();
}
}
Clicking just above first horizontal line prints "X" below the first horizontal line and Clicking just above second horizontal line prints "X" below the second horizontal line.
Logcat shows that clicking just above first horizontal line does give a value greater than maxY/3.
EDIT: I tried removing titlebar from the app and its accuracy improved to some extent but still not very accurate.
The problem is that you are using the MotionEvent received by the onTouchEvent of your Activity (documentation) so the coordinates you are receiving are Activity based instead of View based.
To solve your problem you can move the gesture detection to your DrawView:
public class DrawView extends View implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
private GestureDetectorCompat g1;
public DrawView(final Context context) {
super(context);
g1 = new GestureDetectorCompat(getContext(), this);
g1.setOnDoubleTapListener(this);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
g1.onTouchEvent(event);
return super.onTouchEvent(event);
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
setXandY(e.getX(), e.getY());
Log.v("id2","message2");
return false;
}
}
Also, to receive touch events on your DrawView don't forget to make it clickable:
drawView.setClickable(true);
Edit based on the comments:
In this example the black X represents the touch.
When you receive the MotionEvent on the onTouchEvent of the View, the coordinates you receive are View related, so you will receive (124, 187).
When you receive the MotionEvent on the onTouchEvent of the Activity, the coordinates you receive are Activity related, so you will receive (324, 487).
When you draw on the View the coordinates are View related, so if you draw the coordinates received Activity related, you will draw on the red point.
I am trying to animate my object. For drawing an object I need to use a canvas. I can send canvas to the object constructor only from onDraw function. But doing that, a new object is creating on every step of animation.
public class DrawView extends View {
public static Paint paint;
public DrawView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.BLACK);
}
#Override
public void onDraw(Canvas canvas) {
Line s = new Line(10, 10, canvas);
super.onDraw(canvas);
s.move();
invalidate();
}
}
class Line{
private float x, y;
private Canvas canvas;
public Line(float x, float y, Canvas canvas) {
this.x = x;
this.y = y;
this.canvas = canvas;
}
public void move(){
draw();
x++;
y++;
}
public void draw(){
canvas.drawLine(x, y, x + 5, y + 5, DrawView.paint);
}
}
You mean this???
public void move(Canvas canvas){
draw(Canvas canvas);
x++;
y++;
}
public void draw(Canvas canvas){
canvas.drawLine(x, y, x + 5, y + 5, DrawView.paint);
}
I have already set up the canvas for the drawing. My problem now is how to clear the drawing.
Tried doing this to no avail.
public void clear()
{
circlePath.reset();
mPath.reset();
// Calls the onDraw() method
//invalidate();
}
Please take a look at the whole code here:
https://gist.github.com/akosijiji/a29cca90bead2e5e35ad
Any help is truly appreciated.
On button click
b1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dv.clear(); // call clear method using in your custom view
}
});
Define a clear method in your custom view
public void clear()
{
mBitmap = Bitmap.createBitmap(width,height ,
Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
//Added later..
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.GREEN);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
invalidate();
}
Reset all drawing tools and call invalidate to refresh the view.
Complete working code
public class MainActivity extends Activity implements ColorPickerDialog.OnColorChangedListener {
DrawingView dv ;
RelativeLayout rl;
private Paint mPaint;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dv = new DrawingView(this);
setContentView(R.layout.activity_main);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.GREEN);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
rl = (RelativeLayout) findViewById(R.id.rl);
rl.addView(dv);
Button b = (Button) findViewById(R.id.button1);
//b.setText(R.string.France);
Button b1 = (Button) findViewById(R.id.button2);
rl.setDrawingCacheEnabled(true);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// dv.clear();
new ColorPickerDialog(MainActivity.this, MainActivity.this, mPaint.getColor()).show();
}
});
b1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dv.clear();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class DrawingView extends View {
private int width;
private int height;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
Context context;
private Paint circlePaint;
private Path circlePath;
public DrawingView(Context c) {
super(c);
context=c;
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
circlePaint = new Paint();
circlePath = new Path();
circlePaint.setAntiAlias(true);
circlePaint.setColor(Color.BLUE);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeJoin(Paint.Join.MITER);
circlePaint.setStrokeWidth(4f);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width =w;
height =h;
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
public void clear()
{
mBitmap = Bitmap.createBitmap(width,height ,
Bitmap.Config.ARGB_8888);
//Log.d("BITMAP","Restoring...");
//mBitmap=BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
//Added later..
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.GREEN);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
invalidate();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
canvas.drawPath(circlePath, circlePaint);
}
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;
circlePath.reset();
circlePath.addCircle(mX, mY, 30, Path.Direction.CW);
// invalidate();
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
circlePath.reset();
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
// mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));//.Mode.SCREEN));
mPaint.setMaskFilter(null);
mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.SCREEN);
}
#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;
}
}
#Override
public void colorChanged(int color) {
// TODO Auto-generated method stub
mPaint.setColor(color);
}
}
The above works but it is not good design coz everytime you need to clear you will be creating objects like paint object.
You can create singleton class and have your tools reset and then call invalidate
public class DrawingManager {
private static DrawingManager mInstance=null;
public DrawingTools mDrawingUtilities=null;
public int mThemeIndex;
public Canvas mCanvas;
public Path mPath;
public Paint mBitmapPaint;
public Bitmap mBitmap;
public Paint mPaint;
private DrawingManager()
{
resetDrawingTools();
}
public static DrawingManager getInstance()
{
if(mInstance==null)
{
mInstance=new DrawingManager();
}
return mInstance;
}
public void resetDrawingTools()
{
mBitmap = Bitmap.createBitmap(screenwidth,screenheight ,
Bitmap.Config.ARGB_8888)
mCanvas = new Canvas(mBitmap);
Path = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
//Added later..
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.Green);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
}
/**
* Clears the drawing board.
*/
public static void clearDrawingBoard()
{
mInstance=null;
}
public void resetBitmapCanvasAndPath() {
// TODO Auto-generated method stub
mBitmap = Bitmap.createBitmap(screenwidth,screenheight ,
Bitmap.Config.ARGB_8888);
//Log.d("BITMAP","Restoring...");
//mBitmap=BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
mCanvas = new Canvas(.mBitmap);
mPath = new Path();
}
}
In your painting class
private DrawingManager mDrawingManager=null;
mDrawingManager=DrawingManager.getInstance();// initialize drawing tools once
Then on button click to clear draw
mDrawingManager.resetBitmapCanvasAndPath();
invalidate();
you should use yourpaintlayout.removeallviews();
you would have added one layout to your drawing canvas class. You will have option to remove all views from your drawing layout. hope this will help you.