gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:40:45 24 4
gpt4 key购买 nike

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

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

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

最佳答案

首先,您应该有一个对 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());

}

然后,在您的 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;
}

我认为您的 createPointList 没有执行您想要的操作。你正在创造同一个点的 120 倍!总计 120 * 5 * 24 = 14.400 点!

应该是

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

关于java - 如何根据点数组列表画圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56016045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com