简体   繁体   English

如何根据点数组列表绘制圆?

[英]How to draw circle according to point array list?

I am creating a test game where a circle will move to a selected position from the point array list when touched. 我正在创建一个测试游戏,当触摸时,圆圈将移动到点阵列列表中的选定位置。 However, it seems that it cannot move to the next position of points when clicked. 但是,单击时似乎无法移动到点的下一个位置。 Can u help me find where is the problem is and what solution I can use? 您能帮我找出问题出在哪里以及可以使用什么解决方案吗?

public class EyeTestActivity extends AppCompatActivity  {

private GestureDetectorCompat mDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(new TestView(this));
    // get the gesture detector
    mDetector = new GestureDetectorCompat(EyeTestActivity.this, new SwipeGestureDetector());

}

public boolean onTouchEvent(MotionEvent motionEvent) {
    this.mDetector.onTouchEvent(motionEvent);
    return super.onTouchEvent(motionEvent);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    super.dispatchTouchEvent(ev);
    return mDetector.onTouchEvent(ev);
}






public class TestView extends View {

    public ArrayList<Point> pointlist;


    Paint paint;


    public TestView(Context context) {
        super(context);
        init();

        setFocusable(true);
        setFocusableInTouchMode(true);
        createPointList();


    }

    public void init() {
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);

    }


    public void createPointList() {

        pointlist = new ArrayList<>();

        for (int i = 1; i <= 5; i++) {
            float a = 100 * i;
            float b = 100 * i;
            for (int j = 1; j <= 24; j++) {
                float x = (float) (a * Math.sin(Math.toRadians(15 * j)));
                float y = (float) (b * Math.cos(Math.toRadians(15 * j)));
                for (int k = 0; k < 120; k++) {
                    pointlist.add(new Point(x, y));

                    //Add the x and y coordinates to the Point
                }
            }
        }
    }


    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawColor(Color.BLACK);

        Point point2 = pointlist.get(z);
        canvas.drawCircle(point2.getX() + canvas.getWidth() / 2, point2.getY() + canvas.getHeight()/ 2, 15, paint);


    }


}

int z = 0;
public class SwipeGestureDetector implements GestureDetector.OnGestureListener {
    @Override
    public boolean onDown(MotionEvent e) {

        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {

        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {


        if (e1.getAction() == MotionEvent.ACTION_MOVE) {
            z++;

           if (z > 120) {
               z = 0;

           }
        }
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if(e1 == null || e2 == null)
            return false;
        if(e1.getPointerCount() > 1 || e2.getPointerCount() > 1)
            return false;
        else {
            try {
                float diffX = e2.getX() - e1.getX();
                float diffY = e2.getY() - e1.getY();
                if(Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > 100 && Math.abs(velocityX) > 1000) {
                        if ((diffX > 0) || (diffX < 0)) {

                            return false;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();

            }
            return false;
        }
    }
}
}

I expect the next circle to be drawn when printed. 我希望打印时画下一个圆圈。

First of all, you should have a reference to your TestView layout. 首先,您应该参考您的TestView布局。

public class EyeTestActivity extends AppCompatActivity  {

    private GestureDetectorCompat mDetector;
    private TestView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
     this.requestWindowFeature(Window.FEATURE_NO_TITLE);
     tv = new TestView(this);
     setContentView(tv);
     // get the gesture detector
     mDetector = new GestureDetectorCompat(EyeTestActivity.this, new SwipeGestureDetector());

}

Then, in your onScroll event, you should test e2 instead of e1 然后,在onScroll事件中,应该测试e2而不是e1

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    if (e2.getAction() == MotionEvent.ACTION_MOVE) {
        z++;
       if (z >= 120) {   // zero based arraylist, so, >= 120
           z = 0;
       }
       tv.invalidate; // this to redraw the point
    }
    return true;
}

I think your createPointList is not doing what you want. 我认为您的createPointList并未执行您想要的操作。 You are creating 120 times the same point! 您正在同一时间创建120倍! Total 120 * 5 * 24 = 14.400 points ! 总计120 * 5 * 24 = 14.400点!

It should be 它应该是

for (int i = 1; i <= 5; i++) {
    float a = 100 * i;
    float b = 100 * i;
    for (int j = 1; j <= 24; j++) {
        float x = (float) (a * Math.sin(Math.toRadians(15 * j)));
        float y = (float) (b * Math.cos(Math.toRadians(15 * j)));
        pointlist.add(new Point((int)x, (int)y));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM