活动运行时,将从数组列表中绘制圆。当用户向右移动时,数组列表中的点将保存到另一个arraylist(pointa)中。在另一个类中,应该将arraylist(pointa)绘制为圆形。但是,似乎无法获取arraylist(pointa)的x和y点。您能帮我确定问题出在哪里吗?谢谢。
public class EyeTestActivity extends AppCompatActivity {
int z = 0;
private GestureDetectorCompat mDetector;
TestView testview;
public ArrayList<Point> pointa;
public ArrayList<Point> pointlist;
@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);
testview = new TestView(this);
setContentView(testview);
// get the gesture detector
mDetector = new GestureDetectorCompat(EyeTestActivity.this, new SwipeGestureDetector(testview));
}
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 {
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)));
pointlist.add(new Point(x, y));
Log.i("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);
Log.i("onDraw " , " canvas drawing for z " + z + " at position " + point2.getX() + canvas.getWidth() / 2 + ", " + point2.getY() + canvas.getHeight() / 2);
}
}
public class SwipeGestureDetector implements GestureDetector.OnGestureListener {
ArrayList<Point> pointa = new ArrayList<Point>();
Intent intent = new Intent(EyeTestActivity.this, ResultExplanationActivity.class);
TestView testview;
public SwipeGestureDetector(TestView testview) {
this.testview = testview;
}
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) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
testview.invalidate();
Log.i("onFling", " testview invalidate");
if (e1.getX() < e2.getX()) {
z++;
if (z >=120) {
finish();
startActivity(intent);
}
testview.invalidate();
Point point2 = pointlist.get(z);
pointa.add(new Point(point2.getX(), point2.getY()));
return true;
}
if (e1.getX() > e2.getX()) {
z++;
if (z >=120) {
finish();
startActivity(intent);
}
testview.invalidate();
return true;
}
if (e1.getY() < e2.getY()) {
}
if (e1.getY() > e2.getY()) {
}
return false;
}
}
public ArrayList<Point> getPointa() {
return this.pointa;
}
}
//another class( draw circles from pointa)
public class ResultExplanationActivity extends AppCompatActivity {
private EyeTestActivity eyetest;
ResultView resultView;
public ResultExplanationActivity() {
eyetest = new EyeTestActivity();
ArrayList<Point> pointa = eyetest.getPointa();
}
@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);
resultView = new ResultExplanationActivity.ResultView(this);
setContentView(resultView);
ArrayList<Point> pointa = eyetest.getPointa();
// get the gesture detector
}
public class ResultView extends View {
ArrayList<Point> pointa = eyetest.getPointa();
Paint paint;
Paint painta;
public ResultView(Context context) {
super(context);
init();
setFocusable(true);
setFocusableInTouchMode(true);
}
public void init() {
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.FILL);
painta = new Paint();
painta.setColor(Color.RED);
painta.setStrokeWidth(5);
painta.setStyle(Paint.Style.FILL);
}
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
paint.setStyle(Paint.Style.FILL);
canvas.drawColor(Color.BLACK);
for (int i=0;i<pointa.size();i++){
canvas.drawCircle(pointa.getX() + canvas.getWidth() / 2, pointa.getY() + canvas.getHeight() / 2, 10, paint);
}
}
}
}