Sam321pbs
Sam321pbs

Reputation: 301

Android onClick() is not working properly when ImageView is being animated

I am attempting to set an on clickListener on a image view as it is animating. The ImageView is moving from the bottom of the screen to the top. However, when the imageView is at the bottom of the screen and I touch the top of the screen it allows it to be clicked. I only want where the imageview is to be clicked and not other locations in its path.

Animation in xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
         android:interpolator="@android:anim/linear_interpolator"
         android:fillAfter="true">

    <translate
             android:fromYDelta="75%p"
             android:toYDelta="0%p"
             android:duration="5000"
             android:repeatCount="infinite"/>

    <scale xmlns:android="http://schemas.android.com/apk/res/android"
             android:duration="5000"
             android:fromXScale="1.4"
             android:fromYScale="1.4"
             android:pivotX="50%"
             android:pivotY="50%"
             android:toXScale=".6"
             android:toYScale=".6"
            android:repeatCount="infinite">
</scale>


</set>

The code

mBalloonPortrait1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MediaPlayer mediaPlayer =    MediaPlayer.create(MainBalloonActivity.this,
                    mBalloonArrayList.get(0).getAudioId());
            mediaPlayer.start();



            balloonFallAndFloat(mBalloonPortrait1);

        }
    });


private void balloonFallAndFloat(final ImageView balloon){
    Thread thread = new Thread() {
        @Override
        public void run() {


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        balloon.startAnimation(AnimationUtils.loadAnimation(
                                getApplicationContext(), R.anim.start_game_animation));
                    }
                });


        }
    };

    thread.start();

}

Upvotes: 1

Views: 1049

Answers (1)

noev
noev

Reputation: 980

As far as I understand android animations, the touchable bounds of the view are moved to the destination position immediately when the animation starts. While your view is being animated your onClick event is not being fired because the touchable bounds are already at the final position.

This SO question discusses some ways to update your touchable bounds while your view is being animated.

Another link you should look into.

Upvotes: 1

Related Questions