using android animation-list

2019-03-09 21:04发布

I'm having a little trouble getting an animated loading spinner to work for a splash page. Nothing shows up when I try to run the following code. Any suggestions? It seems that quite a few people have issues with this on google but I do not understand why mine is failing to work. Thanks!

animationloader.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/loadingspinner1" android:duration="200" />
<item android:drawable="@drawable/loadingspinner2" android:duration="200" />
<item android:drawable="@drawable/loadingspinner3" android:duration="200" />
<item android:drawable="@drawable/loadingspinner4" android:duration="200" />
<item android:drawable="@drawable/loadingspinner5" android:duration="200" />
<item android:drawable="@drawable/loadingspinner6" android:duration="200" />
<item android:drawable="@drawable/loadingspinner7" android:duration="200" />
<item android:drawable="@drawable/loadingspinner8" android:duration="200" />
<item android:drawable="@drawable/loadingspinner9" android:duration="200" />
<item android:drawable="@drawable/loadingspinner01" android:duration="200" />
<item android:drawable="@drawable/loadingspinner11" android:duration="200" />
<item android:drawable="@drawable/loadingspinner12" android:duration="200" />
</animation-list>

SplashScreen.java

package com.secure.inmatecanteen;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;

public class SplashScreen extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashscreen);

    //Beginning the loading animation as we attempt to verify registration with SIP
    ImageView ivLoader = (ImageView) findViewById(R.id.IVloadinganimation);
    ivLoader.setBackgroundResource(R.anim.animationloader);


    AnimationDrawable frameAnimation = (AnimationDrawable) ivLoader.getBackground();
    frameAnimation.start();
}
}

splashscreen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@android:color/white" >

 <ImageView
android:id="@+id/iclogo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/iclogo"
android:adjustViewBounds="true"
/>

 <ImageView
android:id="@+id/IVloadinganimation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
/>

    </LinearLayout>

6条回答
你好瞎i
2楼-- · 2019-03-09 21:34

I think the most elegant and versatile option is to extend from the ImageView class:

public class Loader extends ImageView {

    public Loader(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public Loader(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Loader(Context context) {
        super(context);
        init();
    }

    private void init() {
        setBackgroundResource(R.drawable.loader);
        final AnimationDrawable frameAnimation = (AnimationDrawable) getBackground();
        post(new Runnable(){
            public void run(){
                 frameAnimation.start();
             }
        });
    }
}

The loader.xml located in the drawable folder:

<?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/loader_1" android:duration="50" />
    <item android:drawable="@drawable/loader_2" android:duration="50" />
    <item android:drawable="@drawable/loader_3" android:duration="50" />
    <item android:drawable="@drawable/loader_4" android:duration="50" />
    .....
</animation-list>

Now include in your views something as simple as this:

<com.yourpackage.Loader
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
查看更多
再贱就再见
3楼-- · 2019-03-09 21:37

Solved my own problem, You cannot start animations in the oncreate. It has to be in an onclick listener or inside a runnable.

查看更多
在下西门庆
4楼-- · 2019-03-09 21:40

You can play/start animation from onWindowFocusChanged(boolean hasFocus) method.

查看更多
家丑人穷心不美
5楼-- · 2019-03-09 21:50

Within this handler the animation is not fully attached to the window, so the animations can’t be started; instead, this is usually done as a result to user action (such as a button press) or within the onWindowFocusChangedhandler.

refer: professional android 4 application development

查看更多
Viruses.
6楼-- · 2019-03-09 21:52

Elegant solution - create your own Animated View which will follow life-cycle rules.

public class AnimatedImageView extends android.support.v7.widget.AppCompatImageView {
    public AnimatedImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setImageDrawable(ContextCompat.getDrawable(
                context, R.drawable.animated_icon));
    }

    @Override
    protected void onAttachedToWindow() {
        // It's important to note that the start() method called on 
        // the AnimationDrawable cannot be called during the onCreate() 
        // method of your Activity, because the AnimationDrawable
        // is not yet fully attached to the window.
        super.onAttachedToWindow();
        AnimationDrawable tapAnimation = (AnimationDrawable) getDrawable();
        tapAnimation.start();
    }
}

here is animated_icon.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/tap_icon_1" android:duration="500" />
    <item android:drawable="@drawable/tap_icon_2" android:duration="500" />
</animation-list>
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-03-09 21:57

Don't set image resource in xml code.

My XML is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:id="@+id/splashLayout"
android:background="@color/colorPrimary"
android:layout_height="match_parent">


<ImageView
    android:layout_width="230dp"
    android:layout_height="230dp"
    android:id="@+id/iv_splash"
    android:layout_marginTop="-80dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />


</RelativeLayout>

In Activity i do

public class SplashActivity extends Activity {

ImageView iv_splash;

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

    iv_splash=(ImageView)findViewById(R.id.iv_splash);
    iv_splash.setBackgroundResource(R.drawable.splash);
    final AnimationDrawable progressAnimation =(AnimationDrawable)iv_splash.getBackground();
    progressAnimation.start();
    }
}

Drawable file

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >

<item android:drawable="@drawable/logo" android:duration="400"/>
<item android:drawable="@drawable/logo1" android:duration="400"/>

</animation-list>

It's Working Good :)

查看更多
登录 后发表回答