2016-10-24 25 views
1

私はキャンバスに似ているカスタム表示を持っています。私はキャンバス上に直線を描くことができます。私はパスで行を描画することができます。誰でも私を助けることができますか?それは私たちが長い間私たちのモバイル画面をクリックすると、すべてのアプリがショックを受け、それを削除するように選択するようなものです。 もう1つの質問は、このカスタム表示では、このカスタム表示では、私は4回クリックしてダイアログが表示されなくなることができます。アンドロイド画面上の指でタッチを削除する

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(); 
     } 
    } 

} 
+0

パスで描画された行を削除することはできません。http://stackoverflow.com/questions/37261428/android-remove-lines-from-path/37520479#37520479 –

+0

ええ、そうです、ありがとうございますあなたは2つのポイント(座標)と指のスライド(最初のタッチは開始点を決め、ダイナミックフローフィンガースライドを描く)の直線を描くことができる良いアイデアを持っていますか?私はこれを長い間働いているので、 。 –

+0

@Manikandan Kからの答えは正しい解決策につながると思います。 'ACTION_DOWN'があなたのスタートポイントを設定したとき、' ACTION_UP'がエンドポイントを設定したときに、パスを描きます。 –

答えて

0

あなたは画面をタッチして削除すると、あなたの指のイベントは呼び出し:

ACTION_DOWN - あなたが最初

ACTION_MOVEに触れたとき - あなたは、画面上で指を動かしているとき

ACTION_UPを - あなたは、画面

から指を離したときに

幸運を祈る!

+0

ありがとう、私はすべて知っていますあなたがいった。 –

関連する問題