In the example, a simple animation of moving an object from point A to B. The object moves by pressing a button. Is it possible to interact with the object while moving? For example, changing the image to another by pressing.

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private ImageView ImageV1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageV1 = (ImageView) findViewById(R.id.ImageV1); Button startFall = (Button) findViewById(R.id.Btn1); final Animation fallAnim = AnimationUtils.loadAnimation(this, R.anim.falling); startFall.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { ImageV1.startAnimation(fallAnim); } }); } } 

The animation itself:

 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator" > <translate android:duration="3000" android:fromYDelta="1500" android:toYDelta="-100%" /> </set> 

An example of a listener (does not respond to pressing a moving object)

 fallAnim.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { ImageV1.setOnClickListener(new ImageView.OnClickListener() { @Override public void onClick(View v) { startFall.setText("Прослушал удачно"); } }); } @Override public void onAnimationEnd(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } }); 
  • one
    You need to work with Property Animation, not with VIew. Look in the direction of ObjectAnimator. And not with Drawble Animation ... - Shwarz Andrei
  • @ShwarzAndrei Please tell me about compatibility with devices based on API10 +, will these methods work with the old API? - St. Ivan

1 answer 1

  1. Hang the listener on the animation.

  2. When it starts, in the onAnimationStart() method, change the click listener on the view.

  3. At its completion ( onAnimationEnd() ) - change back.

You can hang up an animation listener like this:

 fallAnim.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } }); 
  • Can you please more specifically describe the second paragraph. I'm just starting to figure it out. - St. Ivan
  • @ VANECHKA, updated the answer - Yuriy SPb
  • To be honest, I figured out how to hang the listener on the animation, did not understand what it means to change the click listener on the view, and even more I understood why change it back. If you understand correctly, then I ask the listener to look like this, but nothing happens when you click. ImageV1.setOnClickListener (new ImageView.OnClickListener () {@Override public void onClick (View v) {startFall.setText ("I listened successfully");}})); - St. Ivan
  • @ VANECHKA, well, you yourself say that you need to change something by pressing - so I propose to hang up the listener of clicks and change something in it somewhere. You are not very clearly stating what you need and what exactly makes it difficult for you. Wherefore, I can not give you an exact answer. - JuriySPb
  • My goal is to output about 50 images, in 3 lines that will move across the screen from point a to point b vertically. And, accordingly, I want to be able to influence these elements, for example, when I click it, so that any image could change to another, predetermined one. I gave an example of a listener that I use, but it does not bring any kind of result. - St. Ivan