How to set a time to close image when opening

Asked

Viewed 643 times

1

I’m having a doubt how to set a time to image that was opened close. It would be a loop?

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, in the onClick of the button, 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);
    }
});

ai imagen is visible, so when passing a few seconds it becomes invisible.

  • 1

    Your question is very wide, could you elaborate a little better? Try to post some code that you have tried to implement

  • 1

    improved the issue.

2 answers

3


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

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

1

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

To make animated way, you can use the ObjectAnimator to lower the 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();
}

Browser other questions tagged

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