user1349407
user1349407

Reputation: 622

Android - lost previous drawing on making a paint App

Below is my new painter app for android.

However, it does not reflect previously drawn object. (when i touch up the screen, it lost the shape)

To, reflect previously drawn object, I tried to use 'Bitmap.createBitmap' method but it does not work.

please help me.


public class CreativePainterActivity extends Activity {

//
 //private MyView vw;
Paint mPaint;

//--Variables to store the current figure info
private float _currentStartX;       //where mouse first pressed
private float _currentStartY;
private float _currentEndX;         //where dragged to or released
private float _currentEndY;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
setContentView(new MyView(this));
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFFFF00);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(8);
}
//view class
public class MyView extends View{

private Canvas mCanvas;
private Bitmap mBitmap;
private Paint mBitmapPaint;
Bitmap bm;

//private Paint mBitmapPaint;

public MyView(Context context){
super(context);


//ADDED
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

mBitmap = Bitmap.createBitmap(metrics.widthPixels, metrics.heightPixels, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mCanvas.drawColor(0xFFFFFFFF);
bm = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
}


public void onDraw(Canvas canvas){
//canvas.drawColor(Color.LTGRAY);
canvas.drawBitmap(bm, 0, 0, mBitmapPaint);
canvas.drawLine(_currentStartX, _currentStartY, _currentEndX, _currentEndY, mPaint);
}

//Methods for touch events
public boolean onTouchEvent(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN){
_currentStartX=event.getX();
_currentStartY=event.getY();
return true;
}

if(event.getAction() == MotionEvent.ACTION_MOVE){
_currentEndX=event.getX();
_currentEndY=event.getY();
invalidate();
return true;
}
return true;
}

}//end of the class MyView

}//end of the class CreativePainterActivity

Upvotes: 0

Views: 501

Answers (1)

Kumar Bibek
Kumar Bibek

Reputation: 9117

You will need to know a bit more about how views are drawn. Your views won't preserve whatever is there on, after you draw again. So, you should somehow save whatever was there before, and redraw the old stuff, along with the new changes.

A simpler solution, would be to save the previous drawings to a Bitmap, and then draw that Bitmap again on the canvas first, and add new stuff.

The flow

onDraw(){
    drawBitmap(bmp);
    drawOtherStuff();
    bmp = saveOnScreenBitmap();
}

So, each time you need to save the last drawn bitmap, and re-draw it. Hope it's more clear now.

Some sample tutorials: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/TouchPaint.html

https://web.archive.org/web/20201026064852/http://www.tutorialforandroid.com/2009/06/drawing-with-canvas-in-android.html

Upvotes: 3

Related Questions