1

这是我创建“光标”的课程:

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

基本上我可以在触摸屏幕时创建一个球/圆/光标,但我希望它在我点击其他地方时删除并创建另一个。如果可能的话,我正在尝试找到一种删除以前的方法,因为这样我就可以一次生成一个新的球/圆/光标。

4

0 回答 0