本文介绍了如何implemement在绘画应用程序在Android中撤消功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做的事情撤消我的应用程序的功能。对于这个我搜索我网发现,拍X的ArrayList中,我做了codeI上午下面无法为撤销图纸,θY点。
公共drawView函数(上下文的背景下,浮法X,浮动Y,INT读){
超(上下文);
mBitmap = Bitmap.createBitmap(800,1200,Bitmap.Config.ARGB_8888);
// mainbitmap.add(mBitmap);
//System.out.println("**mBitmapPaint***"+mBitmapPaint);
mCanvas =新的Canvas(mBitmap);
的mpath =新路径();
mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
}
@覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
super.onSizeChanged(W,H,oldw,oldh);
}
@覆盖
保护无效的OnDraw(帆布油画){
的System.out.println(*** Drawinglines ****+ Drawinglines);
canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
canvas.drawPath(的mpath,mPaint);
}
私人浮动MX,我的;
私有静态最终浮动TOUCH_TOLERANCE = 4;
私人无效touch_start(浮X,浮动Y){
mPath.reset();
mPath.moveTo(X,Y);
MX = X;
我= Y;
}
私人无效TOUCH_MOVE(浮X,浮动Y){
浮DX = Math.abs(X - MX);
浮DY = Math.abs(Y - 我的);
如果(DX> = TOUCH_TOLERANCE || DY> = TOUCH_TOLERANCE){
mPath.quadTo(MX,MY,(X + MX)/ 2,(Y +我)/ 2);
MX = X;
我= Y;
}
}
私人无效touch_up(){
mPath.lineTo(MX,MY);
mCanvas.drawPath(的mpath,mPaint);
/// 路径。
//graphics.add(mPath);
mPath.reset();
}
//事件触摸屏幕时,
公共布尔的onTouchEvent(MotionEvent事件){
event.getAction();
INT eventaction = event.getAction();
INT X =(int)的event.getX();
INT Y =(INT)event.getY();
开关(eventaction){
案例MotionEvent.ACTION_DOWN:
{
touch_start(X,Y);
}
无效();
打破;
案例MotionEvent.ACTION_MOVE:
的System.out.println(*** ACTION_MOVE **************+ X +************+ Y);
如果((Drawinglines.equals(笔))||(Drawinglines.equals(擦除)))
{
TOUCH_MOVE(X,Y);
temp_point.add(新点(X,Y));
}
无效();
打破;
案例MotionEvent.ACTION_UP:
main_Points.add(temp_point);
temp_point = NULL;
润色();
无效();
打破;
}
返回true;
}
公共无效undopath()
{
main_Points.remove(main_Points.size() - 1);
无效();
}
}
@覆盖
公共无效的onClick(视图v){
如果(V ==撤消)
{
drawView函数ABC =新drawView函数();
abc.undopath();
}
但撤消撤消功能无法正常工作。
解决方案
私人板岩mSlate;
私人TiledBitmapCanvas mTiledCanvas;
公共无效clickUndo(查看未使用的){
mSlate.undo();
}
公共无效撤消(){
如果(mTiledCanvas == NULL){
Log.v(TAG,撤销之前mTiledCanvas inited);
}
mTiledCanvas.step(-1);
无效();
}
i want to do undo features in in my application.. for this i searched i net found that take arraylist of x,y points which i have done below code i am unable for undo the drawings ??
public Drawview(Context context, float x, float y, int r) {
super(context);
mBitmap = Bitmap.createBitmap(800, 1200, Bitmap.Config.ARGB_8888);
// mainbitmap.add(mBitmap);
//System.out.println("**mBitmapPaint***"+mBitmapPaint);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
System.out.println("***Drawinglines****"+Drawinglines);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, 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);
mCanvas.drawPath(mPath, mPaint);
/// path.
//graphics.add(mPath);
mPath.reset();
}
// events when touching the screen
public boolean onTouchEvent(MotionEvent event) {
event.getAction();
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction ) {
case MotionEvent.ACTION_DOWN:
{
touch_start(X, Y);
}
invalidate();
break;
case MotionEvent.ACTION_MOVE:
System.out.println("***ACTION_MOVE**************"+X+"************"+Y);
if((Drawinglines.equals("Pen"))||(Drawinglines.equals("Erase")))
{
touch_move(X, Y);
temp_point.add(new Point(X,Y));
}
invalidate();
break;
case MotionEvent.ACTION_UP:
main_Points.add(temp_point);
temp_point = null;
touch_up();
invalidate();
break;
}
return true;
}
public void undopath()
{
main_Points.remove(main_Points.size()-1);
invalidate();
}
}
@Override
public void onClick(View v) {
if(v == Undo)
{
Drawview abc=new Drawview();
abc.undopath();
}
But undo undo functionality is not working.
解决方案
private Slate mSlate;
private TiledBitmapCanvas mTiledCanvas;
public void clickUndo(View unused) {
mSlate.undo();
}
public void undo() {
if (mTiledCanvas == null) {
Log.v(TAG, "undo before mTiledCanvas inited");
}
mTiledCanvas.step(-1);
invalidate();
}
这篇关于如何implemement在绘画应用程序在Android中撤消功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!