-2
我正在研究Paint App和該應用程序工作完美。所以現在假設我想繪製一些東西,如製作其他人的繪畫。所以我現在想畫返回意味着前一個(過去)的步驟,然後我想撤消按鈕,這是不完美的工作。這是我的代碼告訴我,我錯了哪裏。請幫助。 package com.example.paint;撤銷和重做按鈕不起作用
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<Path> 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<Path> mMaindialog = new ArrayList<Path>();
// private ArrayList<Path> undonePaths = new ArrayList<Path>();
//
// 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 >= 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.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()>0)
// {
// undonePaths.add(mMaindialog.remove(mMaindialog.size()-1));
// invalidate();
// }
// else
// {
//
// }
// //toast the user
// }
//
// public void onClickRedo(){
// if (undonePaths.size()>0)
// {
// mMaindialog.add(undonePaths.remove(undonePaths.size()-1));
// invalidate();
// }
// else
// {
//
// }
// //toast the user
// }
// }// end MyView
public class MyView extends View implements OnTouchListener {
private ArrayList<Path> mMaindialog = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
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 >= 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();
mMaindialog.add(mPath);
}
public void onClickUndo() {
if (mMaindialog.size()>0)
{
undonePaths.add(mMaindialog.remove(mMaindialog.size()-1));
invalidate();
Log.d("","undonePaths"+ undonePaths);
}
else
{
}
//toast the user
}
public void onClickRedo(){
if (undonePaths.size()>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;
}
}
}
只給了一塊巨大的代碼,沒有什麼代碼的各個部分做的,是不會幫助任何一個回答你的問題的任何解釋。精確,簡潔並解釋不同代碼片段的意圖。 –
@TheDarkKnight其實問題是撤消按鈕不起作用 –