Activate an event when it is on the move

2

I have ImageView moving on the screen, I need to execute an animation when it is touched by the user, however image.setOnClickListener and also image.onTouchListener are not being activated when the image is clicked.

res / anim / translate.xml

<?xml version="1.0" encoding="utf-8"?>

<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:interpolator/decelerate_quad"
    android:fromXDelta="-100%"
    android:toXDelta="0%"
    android:fromYDelta="0%"
    android:toYDelta="0%"
    android:duration="2000"
    android:zAdjustment="top" />

activity that contains animation start

private void IniciarAnimacoes() {

    //Animaçoes dos pinguins
    anim = AnimationUtils.loadAnimation(this, R.anim.movimento_pinguim);
    anim.reset();

    imgPiguim = (ImageView) findViewById(R.id.imgPinguim);
    imgPiguim.setBackgroundResource(R.drawable.background_pinguim_anima);
    animaPiguim = (AnimationDrawable)imgPiguim.getBackground();
    imgPiguim.clearAnimation();
    imgPiguim.startAnimation(anim);

In the code the penguin jumps on the screen, I need it when the user touches it at some point to trigger an event.

Can anyone help me with this, with some example or what other parameters should be used?

    
asked by anonymous 06.10.2016 / 06:15

1 answer

0

In the onCreate() method, get a reference to ImageView and give it an onClickListener :

    imgPiguim = (ImageView) findViewById(R.id.imgPinguim);

    imgPiguim.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Código a ser executado quando a imagem for clicada
            Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
        }
    });

In the onClick() method place the code that should be executed when the image is clicked.

Change the IniciarAnimacoes() method to use the imgPiguim object obtained in onCreate()

private void IniciarAnimacoes() {

    //Animaçoes dos pinguins
    anim = AnimationUtils.loadAnimation(this, R.anim.movimento_pinguim);
    anim.reset();

    //Isto deveria ser passado para o 'onCreate()'
    imgPiguim.setBackgroundResource(R.drawable.background_pinguim_anima);

    //Comentei por que não é utilizada
    //animaPiguim = (AnimationDrawable)imgPiguim.getBackground();

    imgPiguim.clearAnimation();
    imgPiguim.startAnimation(anim);
}
    
07.10.2016 / 14:39