java试图添加光标,移动到用户在屏幕[Android]上触摸设备的任何位置
下面是我创建“游标”的类:
public class Ball extends View {
private final float x;
private final float y;
private final int r;
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public Ball(Context context, float x, float y, int r) {
super(context);
mPaint.setColor(0xFFFF0000);
this.x = x;
this.y = y;
this.r = r;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(x, y, r, mPaint);
}
}
这是我的开关/箱子,我正努力使它工作
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
final TextView textView = (TextView)findViewById(R.id.textView);
final View touchView = (View) findViewById(R.id.touchView);
main.setOnTouchListener(new View.OnTouchListener() {
Ball ball;
@Override
public boolean onTouch(View v, MotionEvent event) {
int eventAction = event.getAction();
textView.setText(String.valueOf(" Azimuth: " + -1*(Math.round((event.getX()/13.33333333)-30)))
+ "\n Elevation: " + String.valueOf(-1*(Math.round((event.getY()/18.33333333)-30))));
float x = event.getX();
float y = event.getY();
FrameLayout flView = (FrameLayout) v;
//This creates a ton of circles. I just want one to appear and then go away.
//flView.addView(ball);
switch(eventAction){
case MotionEvent.ACTION_DOWN:
ball = new Ball(findViewById(R.id.main_view).getContext(), x, y, 5);
flView.addView(ball);
break;
case MotionEvent.ACTION_UP:
flView.removeView(ball);
break;
case MotionEvent.ACTION_MOVE:
ball = null;
break;
}
return true;
}
});
}
基本上,我可以在触摸屏幕时创建一个球/圆/光标,但我希望在单击其他位置时删除它并创建另一个。如果可能的话,我试图找到一种方法来删除之前的,因为这样我就可以一次一个地生成新的球/圆/光标
共 (0) 个答案