Make a flashing Imageview

Asked

Viewed 339 times

0

My app has a ImagemView and I’m trying to make him blink. This method keeps repeating for 6 seconds.

@Override
public void onSensorChanged(SensorEvent event) {
    verificarAndamento();

    if(emCaputura){
        Log.e("Tempo" , ""+tempoDecorrido);
        if(tempoDecorrido % 1000==0){
            imagem.setVisibility(View.INVISIBLE);
            //Toast.makeText(this, "1000", Toast.LENGTH_SHORT).show();

        }else if(tempoDecorrido % 500==0){
            imagem.setVisibility(View.VISIBLE);

          //Toast.makeText(this, "500", Toast.LENGTH_SHORT).show();
        }//Jeito que tentei fazer repedir

        synchronized (this) {
            long current_time = event.timestamp;

            curX = event.values[0];
            curY = event.values[1];
            curZ = event.values[2];

            if (prevX == 0 && prevY == 0 && prevZ == 0) {
                last_update = current_time;
                last_movement = current_time;
                prevX = curX;
                prevY = curY;
                prevZ = curZ;
            }

            long time_difference = current_time - last_update;
            if (time_difference > 0) {
                float movement = Math.abs((curX + curY + curZ) - (prevX - prevY - prevZ)) / time_difference;
                int limit = 1500;
                float min_movement = 1E-6f;
                if (movement > min_movement) {
                    if (current_time - last_movement >= limit) {   
                        movements.add(movement);
                    }
                    last_movement = current_time;
                }
                prevX = curX;
                prevY = curY;
                prevZ = curZ;
                last_update = current_time;
            }
        }
    }
}

I want the image to flash while the method is running.

1 answer

2


Use this:

Flicker.xml

<?xml version="1.0" encoding="utf-8"?>
<set
  xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:duration="500" android:repeatCount="infinite" android:repeatMode="reverse" android:fromAlpha="0.5" android:toAlpha="1.0" />
</set>

In the code, do this:

imageView.startAnimation(AnimationUtils.loadAnimation(context, R.anim.flicker));

Configure how many times you want to repeat the animation. In this case, you are android:repeatCount="infinite"

As you wish for 6 seconds, put inandroid:duration="6000 "

  • Place this xml in which folder in the project?

  • You should create an "anim" folder inside the "res" folder. "

  • Thank you very much worked

  • For nothing, mark as answer please ;)

  • okay. only one more doubt , I changed the duration and she even so did not stop , I have a I use the method onStop() , have as I stop this animation inside?

  • No, change android:repeatCount to 10 , 20, 30 times if applicable, but anything other than Infinite

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.