Note that there are some explanatory texts on larger screens.

plurals
  1. POUndo and redo button not working
    text
    copied!<p>I am working on Paint App and The app is working perfectly.So now suppose I want to draw something like making painting of someone else.So I draw wrong in this painting so now I want to go back means previous(past) step then I want undo button which is not working perfectly .this is my code tell me where I am wrong.please help. package com.example.paint;</p> <pre><code>import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import android.app.Activity; import android.content.Context; import android.content.Intent; 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.os.Bundle; import android.os.Environment; import android.util.DisplayMetrics; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.Button; import android.widget.FrameLayout; import android.widget.Toast; import com.example.paint.ColorPickerDialog.OnColorChangedListener; public class Main extends Activity { public static ArrayList&lt;Path&gt; mMaindialog; private FrameLayout relativelayout; static String sdpath; // Instance variables private Paint mPaint, mBitmapPaint; private MyView mView; private Bitmap mBitmap; private Canvas mCanvas; private Path mPath; private Button ClearPaint, Colorpaint; Ghostdatabase gost; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); relativelayout = (FrameLayout) findViewById(R.id.frameLayout); //relativelayout.addView(mView); DisplayMetrics metrics = getBaseContext().getResources() .getDisplayMetrics(); int w = metrics.widthPixels; int h = metrics.heightPixels; System.out.println(" width " + w); System.out.println(" height " + h); mView = new MyView(this, w, h); mView.setDrawingCacheEnabled(true); 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(5); ClearPaint = (Button) findViewById(R.id.ne); ClearPaint.setOnClickListener(new OnClickListener() { public void onClick(View v) { // mBitmap.eraseColor(Color.TRANSPARENT); // mPath.reset(); // mView.invalidate(); mView.onClickUndo(); } }); Button save22 = (Button) findViewById(R.id.save); save22.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub File cacheDir; Toast.makeText(Main.this, "Photo", 500) .show(); Bitmap icon; relativelayout.setDrawingCacheEnabled(true); icon = Bitmap.createBitmap(relativelayout.getDrawingCache()); Bitmap bitmap = icon; relativelayout.setDrawingCacheEnabled(false); //File mFile1 = Environment.getExternalStorageDirectory(); Date d=new Date(); String fileName = d.getTime()+"mg1.jpg"; File storagePath = (Environment .getExternalStorageDirectory()); File dest = new File(storagePath + "/CityAppImages"); if (!dest.exists()) { dest.mkdirs(); } File mFile2 = new File(dest, fileName); sdpath= mFile2.getAbsolutePath(); Log.d("qqqqqqqqqqqqqqqqqqqqqqq", "zzzzzzzz" + sdpath); try { FileOutputStream outStream; outStream = new FileOutputStream(mFile2); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream); outStream.flush(); outStream.close(); Toast.makeText(Main.this, "Photo Saved Sucessfully", 500) .show(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(Main.this, "Photo Not Saved Sucessfully", 500).show(); } gost = new Ghostdatabase(Main.this); gost.open(); gost.insertTitle(sdpath); } }); Button view = (Button) findViewById(R.id.listtt); view.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent ii = new Intent(Main.this,ListView5.class); startActivity(ii); } }); Button Colorpaint = (Button) findViewById(R.id.Color); Colorpaint.setOnClickListener(new OnClickListener() { public void onClick(View v) { //int _color = R.color.red; new ColorPickerDialog(v.getContext(), new OnColorChangedListener() { public void colorChanged(int color) { mPaint.setColor(color); Log.i("TAG", "mpaint one" + mPaint); } }, mPaint.getColor()).show(); Log.i("TAG", "mpaint two" + mPaint); } }); relativelayout.addView(mView); } /* * /// color changed function, getting value from ColorPickerDialog /// * public void colorChanged(int color) { mPaint.setColor(color); } */ // //////////******************* Pinting view // *******************/////////////////// // public class MyView extends View { // private ArrayList&lt;Path&gt; mMaindialog = new ArrayList&lt;Path&gt;(); // private ArrayList&lt;Path&gt; undonePaths = new ArrayList&lt;Path&gt;(); // // public MyView(Context c, int w, int h) { // super(c); // //image_name=mMaindialog.get(position).imagename; // mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.base); // i // // Bitmap mBitmap = // // Bitmap.createScaledBitmap(originalBitmap,200,200,true); // mCanvas = new Canvas(mBitmap); // mPath = new Path(); // //mBitmapPaint = new Paint(Paint.DITHER_FLAG); // //mBitmapPaint // //.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); // } // // @Override // protected void onSizeChanged(int w, int h, int oldw, int oldh) { // super.onSizeChanged(w, h, oldw, oldh); // // mBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888); // // mCanvas = new Canvas(mBitmap); // } // // @Override // protected void onDraw(Canvas canvas) { // canvas.drawColor(Color.TRANSPARENT); // canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); // canvas.drawPath(mPath, mPaint); // for (Path p : mMaindialog){ // canvas.drawPath(p, mPaint); // } // } // // // //////************touching evants for painting**************/////// // private float mX, mY; // private static final float TOUCH_TOLERANCE = 5; // // 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 &gt;= TOUCH_TOLERANCE || dy &gt;= 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(); // mMaindialog.add(mPath); // } // // @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; // } // end of touch events for image // // public void onClickUndo () { // if (mMaindialog.size()&gt;0) // { // undonePaths.add(mMaindialog.remove(mMaindialog.size()-1)); // invalidate(); // } // else // { // // } // //toast the user // } // // public void onClickRedo (){ // if (undonePaths.size()&gt;0) // { // mMaindialog.add(undonePaths.remove(undonePaths.size()-1)); // invalidate(); // } // else // { // // } // //toast the user // } // }// end MyView public class MyView extends View implements OnTouchListener { private ArrayList&lt;Path&gt; mMaindialog = new ArrayList&lt;Path&gt;(); private ArrayList&lt;Path&gt; undonePaths = new ArrayList&lt;Path&gt;(); private Bitmap im; public MyView(Context context, int w, int h) { super(context); setFocusable(true); setFocusableInTouchMode(true); this.setOnTouchListener(this); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setColor(0xFFFFFFFF); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(4); mCanvas = new Canvas(); mPath = new Path(); // paths.add(mPath); im=BitmapFactory.decodeResource(context.getResources(),R.drawable.base); im = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(im); mPath = new Path(); } @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.drawColor(Color.TRANSPARENT); canvas.drawBitmap(im, 0, 0, mBitmapPaint); canvas.drawPath(mPath, mPaint); for (Path p : mMaindialog){ canvas.drawPath(p, 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 &gt;= TOUCH_TOLERANCE || dy &gt;= 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(); mMaindialog.add(mPath); } public void onClickUndo () { if (mMaindialog.size()&gt;0) { undonePaths.add(mMaindialog.remove(mMaindialog.size()-1)); invalidate(); Log.d("","undonePaths"+ undonePaths); } else { } //toast the user } public void onClickRedo (){ if (undonePaths.size()&gt;0) { mMaindialog.add(undonePaths.remove(undonePaths.size()-1)); invalidate(); } else { } //toast the user } @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; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload