Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓在屏幕上用手指触摸删除线路_Java_Android - Fatal编程技术网

Java 安卓在屏幕上用手指触摸删除线路

Java 安卓在屏幕上用手指触摸删除线路,java,android,Java,Android,我有一个自定义视图,它的功能类似于画布。我可以在画布上画直线。我通过路径画线。我想做的是通过触摸线来删除随机线。任何人都可以帮助我吗?就像当我们长时间单击手机屏幕时,所有应用程序都会被震撼,我可以选择一个来删除它。 另一个问题是这个自定义视图中的对话框显示,我需要单击四次,然后对话框可以关闭。有人知道为什么吗?对不起,我的英语很差,如果你不能理解我的问题,任何问题请告诉我。谢谢 package com.icst.symmetry.View; import android.app.AlertDi

我有一个自定义视图,它的功能类似于画布。我可以在画布上画直线。我通过路径画线。我想做的是通过触摸线来删除随机线。任何人都可以帮助我吗?就像当我们长时间单击手机屏幕时,所有应用程序都会被震撼,我可以选择一个来删除它。 另一个问题是这个自定义视图中的对话框显示,我需要单击四次,然后对话框可以关闭。有人知道为什么吗?对不起,我的英语很差,如果你不能理解我的问题,任何问题请告诉我。谢谢

package com.icst.symmetry.View;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.icst.symmetry.Bean.Axis;
import com.icst.symmetry.Bean.Image;
import com.icst.symmetry.Bean.Line;
import com.icst.symmetry.R;
import com.icst.symmetry.Tools.Util;

import java.util.ArrayList;

/**
 * Created by hugo on 16/8/31.
 */
public class PaintView extends View {
    private static final String TAG = PaintView.class.getSimpleName();


    private Paint machinePaint;
    private Paint userPaint;
    private Line mLine;
    private Axis mAxis;
    private Path mPath;

    private ArrayList<Line> machineLines;
    private ArrayList<Line> saveLines;
    private ArrayList<Line> deleteLines;
    private boolean isEraseModel;
    AlertDialog mDialog;

    public PaintView(Context context) {
        super(context);
        init(context);
    }

    public PaintView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        saveLines = new ArrayList<>();
        deleteLines = new ArrayList<>();
        machineLines = new ArrayList<>();
        machinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        userPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLine = new Line();
        mAxis = new Axis();
        mPath = new Path();
        machinePaint.setStyle(Paint.Style.STROKE);
        machinePaint.setStrokeWidth(4);
        machinePaint.setColor(Color.RED);
        userPaint.setStyle(Paint.Style.STROKE);
        userPaint.setStrokeWidth(4);
        userPaint.setColor(Color.BLACK);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(machineLines.size()!=0){
            for(Line line :machineLines)
                canvas.drawPath(line.getPath(), machinePaint);
        }
        for (Line p : saveLines) {
            canvas.drawPath(p.getPath(), userPaint);
        }
        canvas.drawPath(mPath, userPaint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        if (!isEraseModel) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    break;
                case MotionEvent.ACTION_MOVE:
                    touch_move(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    touch_up(x, y);
                    invalidate();
                    break;
            }
        } else {
            for (Line line : saveLines){
                if(Util.isTouched(line,x,y)){
                    showDialog(line);
                    break;
                }
            }

        }

        return true;
    }

    private void showDialog(Line line) {
        final Line mLine=line;
        AlertDialog.Builder builder =new AlertDialog.Builder(getContext());
        builder.setCancelable(true);
        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                saveLines.remove(mLine);
                deleteLines.add(mLine);
                invalidate();
            }
        });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setTitle("是否确认删除这条线?");
        mDialog = builder.create();
        mDialog.show();
    }

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mAxis.setxStart(x);
        mAxis.setyStart(y);
    }

    private void touch_move(float x, float y) {
        mPath.rewind();
        mPath.moveTo(x, y);
        mPath.lineTo(mAxis.getxStart(), mAxis.getyStart());
    }

    private void touch_up(float x, float y) {
        mAxis.setxEnd(x);
        mAxis.setyEnd(y);
        mLine.setAxis(mAxis);
        mLine.setPath(mPath);
        saveLines.add(mLine);
        mPath = new Path();
        mAxis = new Axis();
        mLine = new Line();
    }


    public void onClickEraser() {
        isEraseModel = true;
    }

    public void onClickDraw() {
        isEraseModel = false;
    }

    public void drawMachineLine(float[] data,Image image){
        machineLines.clear();
        float left=data[0];
        float top=data[1];
        float scaleX=data[2];
        float scaleY=data[3];
        for(int i=0;i<image.getMachineAxis().size();i++){
            Axis axis=image.getMachineAxis().get(i);
            axis.setxStart(axis.getxStart());
            Line line =new Line();
            line.setAxis(axis);
            Path path=new Path();
            path.moveTo(axis.getxEnd()*scaleX+left,axis.getyEnd()*scaleY+top);
            path.lineTo(axis.getxStart()*scaleX+left,axis.getyStart()*scaleY+top);
            line.setPath(path);
            machineLines.add(line);
        }
        invalidate();

    }
    public void undo() {
        if (saveLines != null && saveLines.size() > 0) {
            Line line = saveLines.get(saveLines.size() - 1);
            deleteLines.add(line);
            saveLines.remove(saveLines.size() - 1);
            invalidate();
        }
    }

    public void redo() {
        if (deleteLines != null && deleteLines.size() > 0) {
            Line line = deleteLines.get(deleteLines.size() - 1);
            saveLines.add(line);
            deleteLines.remove(deleteLines.size() - 1);
            invalidate();
        }
    }

}
package com.icst.symmetry.View;
导入android.app.AlertDialog;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.graphics.Path;
导入android.util.AttributeSet;
导入android.view.MotionEvent;
导入android.view.view;
导入com.icst.symmetry.Bean.Axis;
导入com.icst.symmetry.Bean.Image;
导入com.icst.symmetry.Bean.Line;
导入com.icst.symmetry.R;
导入com.icst.symmetry.Tools.Util;
导入java.util.ArrayList;
/**
*雨果于31年8月16日创作。
*/
公共类PaintView扩展了视图{
私有静态最终字符串标记=PaintView.class.getSimpleName();
私人油漆机械漆;
私人涂料;
专线;
私有轴最大值;
专用路径mPath;
私人ArrayList机械;
私有ArrayList存储行;
私有数组列表删除行;
私有布尔模型;
警报对话框mDialog;
公共PaintView(上下文){
超级(上下文);
init(上下文);
}
公共画图视图(上下文、属性集属性){
超级(上下文,attrs);
init(上下文);
}
私有void init(上下文){
saveLines=newarraylist();
deleteLines=新的ArrayList();
machineLines=新阵列列表();
machinePaint=新油漆(油漆。防油漆别名标志);
userPaint=new Paint(Paint.ANTI_别名_标志);
mLine=新行();
mAxis=新轴();
mPath=新路径();
机械油漆.设置样式(油漆.样式.笔划);
机械油漆设定行程宽度(4);
机器漆。设置颜色(颜色。红色);
userPaint.setStyle(Paint.Style.STROKE);
userPaint.设置行程宽度(4);
userPaint.setColor(Color.BLACK);
}
@凌驾
受保护的void onDraw(画布){
如果(machineLines.size()!=0){
用于(生产线:机械)
drawPath(line.getPath(),machinePaint);
}
用于(第p行:保存行){
drawPath(p.getPath(),userPaint);
}
canvas.drawPath(mPath,userPaint);
}
@凌驾
公共布尔onTouchEvent(运动事件){
float x=event.getX();
float y=event.getY();
如果(!isEraseModel){
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
触摸启动(x,y);
打破
case MotionEvent.ACTION\u移动:
触摸移动(x,y);
使无效();
打破
case MotionEvent.ACTION\u UP:
补漆(x,y);
使无效();
打破
}
}否则{
用于(行:保存行){
if(Util.isTouched(line,x,y)){
显示对话框(行);
打破
}
}
}
返回true;
}
专用void显示对话框(行){
最终行mLine=行;
AlertDialog.Builder=新建AlertDialog.Builder(getContext());
builder.setCancelable(true);
setPositiveButton(R.string.ok,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
保存行。删除(mLine);
deleteLines.add(mLine);
使无效();
}
});
setNegativeButton(R.string.cancel,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
}
});
builder.setTitle(“是否确认删除这条线?");
mDialog=builder.create();
mDialog.show();
}
专用无效触摸启动(浮动x、浮动y){
mPath.reset();
移动到(x,y)的速度;
mAxis.setxStart(x);
mAxis.setyStart(y);
}
私有无效触摸移动(浮动x、浮动y){
mPath.倒带();
移动到(x,y)的速度;
lineTo(mAxis.getxStart(),mAxis.getyStart());
}
私人空间修补(浮动x、浮动y){
mAxis.setxEnd(x);
mAxis.setyEnd(y);
mLine.setAxis(最大值);
多线设置路径(mPath);
保存行。添加(mLine);
mPath=新路径();
mAxis=新轴();
mLine=新行();
}
公共无效OnClick橡皮擦(){
isEraseModel=true;
}
公共void onClickDraw(){
isEraseModel=false;
}
公共虚空绘图机线(浮动[]数据,图像){
机器线。清除();
左浮动=数据[0];
浮顶=数据[1];
float scaleX=数据[2];
float scaleY=数据[3];
对于(int i=0;i 0){
Line Line=saveLines.get(saveLines.size()-1);
删除行。添加(行);
saveLines.remove(saveLines.size()-1);
使无效();
}
}
公共无效重做(){
if(deleteLines!=null&&deleteLines.size()>0){
Line=deleteLines.get(deleteLines.size()-1);
保存行。添加(行);
deleteLines.remove(deleteLines.size()-1);
使无效();
}
}
}

好。当您触摸屏幕并取下手指时