Trigger onClick on current position of Button/ImageView animated with XML

503 views Asked by At

I am making a simple game that displays balloons (Button/ImageView with background pic of a balloon). I've animated the balloons using the anim.xml files and the balloons move pretty well around the screen.

The problem is that when you touch them, they don't do anything. The location I placed them in XML retains the power to click on them. On touching the moving pics, nothing happens.

How do I make the balloons pop on touch (i.e. change background to a broken balloon image and produce a sound)?

4

There are 4 answers

0
npace On BEST ANSWER

Don't use animation XML files for this, use ViewPropertyAnimator instead. Just call `.animate()' on your balloon view and set a direction and duration of the animation. For example this code will move your balloon by 200 pixels to the right over 1 second:

balloonView.animate().translationXBy(200).setDuration(1000);

0
Konstantin Kiriushyn On

Check this link out. This is an example of how to create views which are moving and can react on touches. This is part of Coursera Android Development course. If you are interested - take part in that course for free.

Best

0
Sivaranjani On

Refer this link to know the property animation working. also use object animator to animate view. so only the click functionality of the view will remain in the view wherever we move.

0
Rohit Heera On
Yes you create a SeperateClass For touch and Click listener and pass the Necessarydata.
e.g.

class BaloonClickListener implements onCLickListener
{
Button button;
public BaloonClickListener(Button button)
{
this.button=button;
} 
@override
public void onCLick(View view)
{
button.setBackgroundResource(bustedImage);
}

}


Set Listener:

balloonView.setOnClickListener(new BaloonClickListener(balloonView));


I think this may solve your problem.