需要帮助onTouch Event

时间:2016-06-17 19:33:06

标签: java android ontouchevent

我正在为一个学校项目编写一个小应用程序:概念很简单:在短暂的时间内在屏幕上随机播放圆圈时播放一首歌曲,用户必须点击屏幕才能获得积分。

我找到了正常工作的圆圈的代码:

var/let value: CGFLoat!

所以,我的问题是:如何在Circle上实现onTouch事件,其位置不断变化?或者我是否需要考虑另一种方法,比如当他弹出时在圆圈的中心定义一个隐形按钮?

我发现这是使用onTouch事件,但我真的不明白如何在我的情况下实现它:

public class Partie extends AppCompatActivity  {

RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new SplashLaunch(this));
}

public class SplashLaunch extends View {

    Handler cool = new Handler();
    ObjectAnimator aa = new ObjectAnimator();
    Paint newPaint = new Paint();
    int randomWidthOne = 0;
    int randomHeightOne = 0;
    private int radiusOne = 150;
    final int redColorOne = Color.RED;
    final int greenColorOne = Color.GREEN;
    private int lastColorOne;
    private final Random theRandom = new Random();

    public SplashLaunch(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    private final Runnable circleUpdater = new Runnable() {
        @SuppressLint("NewApi")
        @Override
        public void run() {
            lastColorOne = theRandom.nextInt(2) == 1 ? redColorOne : greenColorOne;
            newPaint.setColor(lastColorOne);
            cool.postDelayed(this, 1500);  // Time between each circle to pop on the screen
            int x = 0;
            while (x <= 200) {  // x = transparency : 0 ==> non visible  , 255 ==> visible
                newPaint.setAlpha(x);
                x++;
            }
            invalidate();
        }
    };

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        cool.post(circleUpdater);
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        cool.removeCallbacks(circleUpdater);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        if (theRandom == null) {
            randomWidthOne = (int) (theRandom.nextInt(Math.abs(getWidth() - radiusOne / 2)) + radiusOne / 2f);
            randomHeightOne = (theRandom.nextInt((int) Math.abs((getHeight() - radiusOne / 2 + radiusOne / 2f))));
        } else {
            randomWidthOne = (int) (theRandom.nextInt(Math.abs(getWidth() - radiusOne / 2)) + radiusOne / 2f);
            randomHeightOne = (theRandom.nextInt((int) Math.abs((getHeight() - radiusOne / 2 + radiusOne / 2f))));
        }
   } } }

我是初学者,所以欢迎任何帮助:)。

提前致谢!

0 个答案:

没有答案