开发者

Identify the View that triggered a touch-event

I am trying to drag a view over the screen

FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
final Ball bigball;
bigball = new Ball(this,50,50,25, 0xFFFF0000);
main.addView(bigball);

This draws a red circle on my screen. How and where do I need to implement my touchscreen-handling, if I want it only to trigger if someone touches the red circle rather than the remainder of the screen?

Ball.Java is a separate class:

public class Ball extends View 

I know I could check the coordinates 开发者_运维百科where the user touched the screen and then compare that to where the circle is, but there must be a way to simply check the ID or some other reference of the circle, right?

Everything I have looked at and tried so far would run my code regardless of where the touch started or stopped on the screen. (So I can move the ball, I can draw lines on the screen with my finger and everything. But I cannot tell if I touched the red circle.)

Can anyone expain this to me in simple words, please? (And forgive me for using equally simple words in my question. I no longer feel I have a clue what I'm doing here, and I by now I would just not use big words like "Listener" or "Event" correctly anymore.

Thanks.


you can override/implement onTouchEvent in Ball class which is extended from View.

public class Ball extends View {
  public boolean onTouchEvent(MotionEvent ev) {
    if(ev.getAction == /*check against all the desired action*/ {
      //handle touch and return true
    return super.onTouchEvent(ev);
}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜