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) { } });