Setting a time to close an image when opened

1

I have a question about how to set a time for image that was opened close. Would it be a tie?

I'm using ImageView :

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView1"
    android:visibility="invisible"
    android:src="@drawable/nomeDaSuaImagem" />

In the Activity code, on the button's onClick, make it visible:

Button button1;
ImageView imageView1;

imageView1 = (ImageView) findViewById(R.id.imageView1);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        imageView1.setVisibility(View.VISIBLE);
    }
});

The image is visible, so when you spend a few seconds it becomes invisible.

    
asked by anonymous 27.01.2015 / 16:55

2 answers

2

Do something like this after displaying the image:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        imageView1.setVisibility(View.INVISIBLE); // Ou View.GONE
    }
}, 3000);

Time is given in milliseconds, so the image will be invisible after 3 seconds by my example.

    
27.01.2015 / 17:22
1

You can do this in a "dry" and lively way (which I think is best)

To do this in an animated way, you can use ObjectAnimator to decrease Alpha:

private void fadeOutImage() {
    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(seuImageView, View.ALPHA, 0);
    //Tempo, em milisegundos, da sua animação. Caso não coloque nenhum, o default é 300.
    objectAnimator.setDuration(200);
    /*Aqui esta a mágica. Você define o tempo (em milisegundos) para sua animação começar.
    * Ou seja, depois de 2 segundos, sua ImageView ira começar a desaparecer
    */
    objectAnimator.setStartDelay(2000);
    //Caso você queira um Listener para o termino da animação
    objectAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {

        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });
    objectAnimator.start();
}
    
27.01.2015 / 17:27