Load the bitmap into the canvas and draw it

I like to make an application, something like a little paint, I need to get a bitmap, draw it on the canvas, and then draw it (using figer) ... So, I have this code:

import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import android.content.Context; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.os.Environment; import android.util.Log; import android.graphics.Bitmap; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; public class DrawView extends View implements OnTouchListener { private Canvas mCanvas; private Path mPath; public Paint mPaint; private ArrayList<Path> paths = new ArrayList<Path>(); private boolean start = true; public DrawView(Context context) { super(context); setFocusable(true); setFocusableInTouchMode(true); this.setOnTouchListener((OnTouchListener) this); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(4); mPaint.setDither(true); mPaint.setFilterBitmap(true); mCanvas = new Canvas(); mPath = new Path(); paths.add(mPath); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onDraw(Canvas canvas) { for (Path p : paths) { canvas.drawPath(p, mPaint); } if (start) { Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.v01); canvas.drawBitmap(bmp, 0, 0, mPaint); start = false; } } 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 = new Path(); paths.add(mPath); } public boolean onTouch(View arg0, 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; } } 

With this code, I can draw on top of the canvas and get the bitmap in the first place ... But, if I touch, the bitmap disappears, which causes every onTouch event that I draw again based on the path ... How can I do things that I like? I mean, get a bitmap and draw it ...

+1
java android bitmap android-canvas draw
Jun 07 '13 at 17:24
source share
4 answers

Solved, I used this code to upload an image and draw it ...

 DrawView.java package com.example.com.dibuja; import java.util.ArrayList; 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.graphics.Path; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; public class DrawView extends View implements OnTouchListener { private Canvas mCanvas; private Path mPath; public Paint mPaint; private ArrayList<Path> paths = new ArrayList<Path>(); Bitmap bmp; public DrawView(Context context) { super(context); setFocusable(true); setFocusableInTouchMode(true); this.setOnTouchListener(this); bmp = BitmapFactory.decodeResource(getResources(), R.drawable.v01); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setColor(Color.BLUE); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(6); mCanvas = new Canvas(); mPath = new Path(); paths.add(mPath); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onDraw(Canvas canvas) { canvas.drawBitmap(bmp, 0, 0, mPaint); for (Path p : paths) { canvas.drawPath(p, mPaint); } } private float mX, mY; private static final float TOUCH_TOLERANCE = 0; Draw dw = new Draw(); 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 = new Path(); paths.add(mPath); } @Override public boolean onTouch(View arg0, 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; } } 
0
Jul 23 '13 at 17:22
source share

Should work if you move canvas.drawBitmap () outside the if statement. Save the decoding, but move the actual drawing.

0
Jun 07 '13 at 17:37
source share

I am not sure what the problem is. you need to redraw the bitmap every time you run onDraw , or nothing will be there. Did you really notice any performance loss there?

What you do not need to do is decode the resource on each invalidate .

Declare your bmp as a global variable

 Bitmap bmp; 

Save the bitmap on your constructor:

 DrawView(Context context){ ... bmp = BitmapFactory.decodeResource(getResources(), R.drawable.v01); } 

and remove the if method when drawing. You do not want to be there. You MUST redraw the bitmap every time you invoke an invalid one. But this should not cause performance problems.

 canvas.drawBitmap(bmp, 0, 0, mPaint); 
0
Jun 11 '13 at 16:19
source share

This will make your presentation very slow, but if you delete the if(start) , your bitmap will not disappear.

A slightly better way to do this is to take canvas.drawBitmap() outside the if(start) method, but it will still be slow because the bitmap will be drawn every time you touch the screen.




In fact, this is because every time you call onDraw() , you pass an empty bitmap. This means that you need to redraw everything every time when onDraw is called, but since you only set the bitmap the first time it is called again, the bitmap is not added again and therefore disappears.

0
Jun 11 '13 at 19:26
source share



All Articles