如何根据点数组列表画圆?

我正在创建一个测试游戏,其中一个圆圈在被触摸时将移动到点数组列表中的选定位置。但是,单击时似乎无法移动到点的下一个位置。你能帮我找出问题出在哪里以及我可以使用什么解决方案吗?


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


    }


我希望在打印时绘制下一个圆圈。


翻阅古今
浏览 60回答 1
1回答

汪汪一只猫

首先,您应该参考您的TestView布局。public class EyeTestActivity extends AppCompatActivity&nbsp; {&nbsp; &nbsp; private GestureDetectorCompat mDetector;&nbsp; &nbsp; private TestView tv;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp;super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp;getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,&nbsp; &nbsp; &nbsp; &nbsp; WindowManager.LayoutParams.FLAG_FULLSCREEN);&nbsp; &nbsp; &nbsp;this.requestWindowFeature(Window.FEATURE_NO_TITLE);&nbsp; &nbsp; &nbsp;tv = new TestView(this);&nbsp; &nbsp; &nbsp;setContentView(tv);&nbsp; &nbsp; &nbsp;// get the gesture detector&nbsp; &nbsp; &nbsp;mDetector = new GestureDetectorCompat(EyeTestActivity.this, new SwipeGestureDetector());}然后,在您的onScroll事件中,您应该测试e2而不是e1@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {&nbsp; &nbsp; if (e2.getAction() == MotionEvent.ACTION_MOVE) {&nbsp; &nbsp; &nbsp; &nbsp; z++;&nbsp; &nbsp; &nbsp; &nbsp;if (z >= 120) {&nbsp; &nbsp;// zero based arraylist, so, >= 120&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;z = 0;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;tv.invalidate; // this to redraw the point&nbsp; &nbsp; }&nbsp; &nbsp; return true;}我认为你createPointList没有做你想做的事。您正在创造 120 次相同的点!总计 120 * 5 * 24 = 14.400 点!它应该是for (int i = 1; i <= 5; i++) {&nbsp; &nbsp; float a = 100 * i;&nbsp; &nbsp; float b = 100 * i;&nbsp; &nbsp; for (int j = 1; j <= 24; j++) {&nbsp; &nbsp; &nbsp; &nbsp; float x = (float) (a * Math.sin(Math.toRadians(15 * j)));&nbsp; &nbsp; &nbsp; &nbsp; float y = (float) (b * Math.cos(Math.toRadians(15 * j)));&nbsp; &nbsp; &nbsp; &nbsp; pointlist.add(new Point((int)x, (int)y));&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java