Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.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_Touch_Android Animation - Fatal编程技术网

Java 如何在两点之间画直线?

Java 如何在两点之间画直线?,java,android,touch,android-animation,Java,Android,Touch,Android Animation,我试图通过手指从第一个点移动到第二个点来实现代码中称为第一个点的一个点,如果第一个点没有到达第二个点,那么这条线应该在动作开始时消失 我的灵感来自: 我正试图通过使用CustomView的onDraw方法来实现它 以下是我的尝试: public class MyGFX extends View{ // setup initial color private final int paintColor = Color.BLACK; // defines paint and canvas

我试图通过手指从
第一个
点移动到
第二个
点来实现代码中称为
第一个
点的一个点,如果
第一个
点没有到达
第二个
点,那么这条线应该在动作开始时消失

我的灵感来自:

我正试图通过使用CustomView的onDraw方法来实现它

以下是我的尝试:

public class MyGFX extends View{

// setup initial color
  private final int paintColor = Color.BLACK;
  // defines paint and canvas
  private Paint drawPaint;

  Point first , second;

  private Path path = new Path();

public MyGFX(Context context,AttributeSet attrs) {
    super(context, attrs);
    setupPaint(); // same as before
    first = new Point(100,100);
    second = new Point(200,100);
}

// Get x and y and append them to the path
public boolean onTouchEvent(MotionEvent event) {
    float pointX = event.getX();
    float pointY = event.getY();
    // Checks for the event that occurs
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        boolean insideCircle = 
                (2*((int)pointX - (first.x + 2))) + (2*((int)pointY - (first.y + 2))) <= (2 * 2);

        if(insideCircle){
            // Starts a new line in the path
            path.moveTo(first.x,first.y);
            Toast.makeText(getContext(), "path.moveto is called \n first.x = "
            +first.x+" first.y = "+first.y+" px ="+pointX+"  py ="+pointY, 
                    Toast.LENGTH_SHORT).show();
        }

        break;
    case MotionEvent.ACTION_MOVE:
        boolean insideCircle2 = 
        (2*(pointX - (second.x + 2))) + (2*(pointY - (second.y + 2))) <= (2 * 2);

        if(insideCircle2){
            // Starts a new line in the path
            path.moveTo(pointX, pointY);
        }
        // Draws line between last point and this point
        path.lineTo(pointX, pointY);
        postInvalidate(); // Indicate view should be redrawn

        break;
    case MotionEvent.ACTION_UP:
        path.close();
        break;
    default:
        return false;
   }

   return true; // Indicate we've consumed the touch
}

 // Setup paint with color and stroke styles
  private void setupPaint() {
    drawPaint = new Paint();
    drawPaint.setColor(paintColor);
    drawPaint.setAntiAlias(true);
    drawPaint.setStrokeWidth(5);
    drawPaint.setStyle(Paint.Style.STROKE);
    drawPaint.setStrokeJoin(Paint.Join.ROUND);
    drawPaint.setStrokeCap(Paint.Cap.ROUND);
  }

// Draws the path created during the touch events
  @Override
  protected void onDraw(Canvas canvas) {
      canvas.drawCircle(first.x, first.y, 2, drawPaint);
      canvas.drawCircle(second.x, second.y, 2, drawPaint);

      if(!path.isEmpty())
          canvas.drawPath(path, drawPaint);


  }


}
公共类MyGFX扩展视图{
//设置初始颜色
私有最终int paintColor=Color.BLACK;
//定义绘画和画布
私人油漆;
第一点,第二点;
私有路径路径=新路径();
公共MyGFX(上下文、属性集属性){
超级(上下文,attrs);
setupPaint();//同上
第一个=新点(100100);
第二个=新点(200100);
}
//获取x和y并将它们附加到路径
公共布尔onTouchEvent(运动事件){
float pointX=event.getX();
float pointY=event.getY();
//检查发生的事件
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
布尔内圆=
(2*((int)pointX-(first.x+2)))+(2*((int)pointY-(first.y+2))使用

drawine(浮标起点tx、浮标起点、浮标终点x、浮标终点、油漆)

所以


canvas.drawine(10.0、5.0、11.0、12.6等);
试试这个

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

        <play.decisio.com.myapplication.ConnectDotsView
            android:layout_width="match_parent"
            android:id="@+id/dot"
            android:layout_height="match_parent" />

    </RelativeLayout>

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        view=(ConnectDotsView)findViewById(R.id.dot);
        List<Point> point=new ArrayList<Point>();
        for(int i=0;i<5;i++){
            point.add(new Point(i*100+100, i*100+10));
        }
        view.setPoints(point);
    }
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
视图=(ConnectDotsView)findViewById(R.id.dot);
列表点=新的ArrayList();

对于(int i=0;i普通人有人吗?请查看此信息…希望此信息有帮助…进一步询问:)@TheSpeaker我要试试,然后再发一次。没有合适的文档吗?@TheSpeaker你有什么例外?伙计,你太棒了,我已经连续两天找到了这个结果,谢谢你。如何在点击按钮时清除连接线?提前谢谢。