[英]Android 2 finger touch triggering 1 finger touch as well as 2 finger touch
[英]android remove line by finger touch on screen
我有一个自定义视图,其功能就像帆布。我可以在画布上绘制直线。我按路径绘制线条。我想要做的是通过触摸线删除随机线。任何人都可以帮助我?这就像当我们长时间点击我们的手机屏幕时,所有应用程序都震惊了,我可以选择一个删除它。 另一个问题是这个自定义视图中的对话框显示,我需要点击四次然后对话就可以解雇了。任何人都知道为什么?对不起我的英文很差,如果你不明白我的问题,任何问题都会让我知道。谢谢。
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();
}
}
}
好。 当您触摸屏幕并删除手指事件时:
ACTION_DOWN - 第一次触摸时
ACTION_MOVE - 当你在屏幕上移动手指时
ACTION_UP - 当你从屏幕上移开手指时
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.