0
  • I doesn't know how to move an image right to left slide in SplashActivity.
  • Below I posted a code for SplashActivity.I got an output.But Images wasn't slide.It displays an image simply one after another.

SplashActivity.java:

public class SplashActivity extends FragmentActivity {

    // create object of progressbar
    ProgressBar prgLoading;

    // set variable for progress bar to 0
    int progress = 0;
    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_splash);

        Animation slide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_left);

        final ImageView splashImageView = (ImageView) findViewById(R.id.splashImageView);

        splashImageView.setBackgroundResource(R.anim.splash);

        final AnimationDrawable frameAnimation = (AnimationDrawable) splashImageView
                .getBackground();

        view.startAnimation(slide);

        Thread timer = new Thread() {
            public void run() {
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    Intent splash = new Intent(SplashActivity.this,
                            HomeActivity.class);
                    startActivity(splash);
                }
            }
        };

        timer.start();
        splashImageView.post(new Runnable() {

            @Override
            public void run() {
                frameAnimation.start();
            }
        });
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

}

layout_splash.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@android:id/tabs" >

                <ImageView 
                    android:id="@+id/splashImageView"
                    android:background="@drawable/spl1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    />

          </FrameLayout>
    </RelativeLayout>

</LinearLayout>

In res/anim/slide_in_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate android:duration="1000" android:fromXDelta="0%" android:toXDelta="-100%"/>
    <alpha android:duration="1000" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>

In res/anim/splash.xml:

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

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/flaganim"
    android:oneshot="false"
    >

    <item android:drawable="@drawable/spl1" android:duration="2000" />
    <item android:drawable="@drawable/spl2" android:duration="2000" />
    <item android:drawable="@drawable/spl3" android:duration="2000" />

    </animation-list>
  • I got an output as three images simply display one after another.But Images doesn't slide from Right to Left.
  • Anybody can help me if you know how to solve these.Thank you.
Stephen
  • 9,899
  • 16
  • 90
  • 137
  • 1
    The `sleep(5000);` is bad, don't do that, use Timers. Also check out [**one of my previous answers regarding animations**](http://stackoverflow.com/questions/19765938/show-and-hide-a-view-with-a-slide-up-down-animation/19766034#19766034). It's better to animate the `ImageView` itself. – Xaver Kapeller Jun 24 '14 at 10:47
  • @XaverKapeller I done everything exactly.But I am getting a null pointer exception at these line `view.startAnimation(slide);` – Stephen Jun 24 '14 at 11:25
  • Than most likely your `view` is null. Edit the logcat and your code into your question and I can help you pinpoint the problem. – Xaver Kapeller Jun 24 '14 at 11:27
  • @XaverKapeller I am edited the post and all changes. – Stephen Jun 24 '14 at 11:43
  • I will write up an answer, give me a few minutes. – Xaver Kapeller Jun 24 '14 at 11:46
  • @XaverKapeller ok.Will check and tell you. – Stephen Jun 24 '14 at 12:00

4 Answers4

2

I have removed all unnecessary portions of your code. Additionally I improved it a bit, for example I use a Timer instead of Thread.sleep() and that weird finish() in onPause() can be replaced with Intent flags. I also removed the call to your animation drawable. With the slide animation I assume you aren't going to need that anymore.

public class SplashActivity extends FragmentActivity {

    private final TimerTask spashScreenFinished = new TimerTask() {
        @Override
        public void run() {
            Intent splash = new Intent(SplashActivity.this, HomeActivity.class);     
            // We set these flags so the user cannot return to the SplashScreen
            splash.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(splash);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_splash);

        // We get the ImageView and set the background. (If possible do this in XML instead of code)
        final ImageView splashImageView = (ImageView) findViewById(R.id.splashImageView);
        splashImageView.setBackgroundResource(R.anim.splash);

        // We load the slide animation and apply it to the ImageView
        Animation slide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_left);
        splashImageView.startAnimation(slide);

        // We use a Timer to schedule a TimerTask for 5 seconds in the future!
        Timer timer = new Timer();
        timer.schedule(this.spashScreenFinished, 5000);
    }
}

Also: Don't use splash screens! They are always a sign of a badly designed app. If your app needs to load that much on startup then you are doing something wrong. And if it doesn't need to load anything than there is no reason to keep the user waiting for 5 seconds. A good app should start instantly and be usable as quick as possible.

Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
1

You can use TranslateAnimation

DisplayMetrics outMetrics = new DisplayMetrics();

getActivity().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);

TranslateAnimation imageAnimation = new TranslateAnimation(outMetrics.widthPixels, 0, 0, 0);
imageAnimation.setDuration(1500);

splashImageView.startAnimation(imageAnimation);

Then you can play with the values passed in the TranslateAnimation constructor to get the amount of animation you want.

Apoorv
  • 13,470
  • 4
  • 27
  • 33
  • can you explain these in detail.It never parse any xml code – Stephen Jun 24 '14 at 10:42
  • Yeah because `TranslateAnimation ` is a class for moving things around. Read [Translate Animation](http://developer.android.com/reference/android/view/animation/TranslateAnimation.html) – Apoorv Jun 24 '14 at 10:52
0

Android has built in slide in animations so you can use those.

You can call them by doing the following:

Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), android.R.id.slide_in_right);  

You can then do:

splashImageView.setAnimation(anim);

The image will then slide in from the right.

There is also android.R.anim.slide_out_left

Here is the source code for slide_in_right.xml for reference.

You call put that code in your project if you want and experiment with it to get different results but remember to remove the android. when calling it.

RED_
  • 2,997
  • 4
  • 40
  • 59
0

I dont have working knowledge on animation. But, i have solution jus try it. set two image left and right side which you want to display in splash screen. write a condition that, set visible for right side image for few second then set visible for left side image for few second. It also looks like a animation which you want to achieve. Make it simple :-)

iffu
  • 331
  • 1
  • 4
  • 16