0

I have a few controls that I don't want the user to see all the time, so they need to:

  • Fade out 5 seconds after activity is created
  • Fade in when user taps the screen
  • Fade out after 5 seconds of visibility or when user taps screen again (whichever comes first)

I've seen a lot of implementations of animations out there (including Thread, Handler, and Animation). Which method would work best in this situation?

Eddie K
  • 503
  • 4
  • 18

2 Answers2

2

Here I included the code which i had done in my project. Please check this out. It may help you.

Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
Animation animationFadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);

                handler = new Handle(this);

        animator = new Thread() {
            public void run() {
                try {
                    handler.sendMessage(handler.obtainMessage(1));
                    sleep(2000);
                    handler.sendMessage(handler.obtainMessage(2));
                    sleep(2000);
                    handler.sendMessage(handler.obtainMessage(3));

                } catch (Exception e) {
                    e.printStackTrace();

                }
            }
        };

        animator.start();

static class Handle extends Handler {       
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub          
            if (msg.what == 1) {
                iv.startAnimation(animationFadeIn);
            } else if (msg.what == 2) {
            } else if (msg.what == 3) {
                iv.startAnimation(animationFadeOut);
            }
            super.handleMessage(msg);

        }

    }
RIJO RV
  • 806
  • 6
  • 18
1

Use following code for fade in and fade out.

fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

</set>

fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0" />

</set>

happy coding !!

Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36
  • Thanks for your reply. I'm familiar with custom animations; I'm just curious about which method to use for the timing of these animations. – Eddie K Jan 02 '14 at 21:44