using android animation-list

前端 未结 7 455
鱼传尺愫
鱼传尺愫 2020-12-28 08:42

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 seem

相关标签:
7条回答
  • 2020-12-28 08:53

    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>
    
    0 讨论(0)
  • 2020-12-28 08:58

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

    0 讨论(0)
  • 2020-12-28 09:01

    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

    0 讨论(0)
  • 2020-12-28 09:03

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

    0 讨论(0)
  • 2020-12-28 09:04

    Check GitHub project here I have implemented: Check here

    My MainActivity.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:background="@color/colorPrimaryDark"
        tools:context=".MainActivity">
    
        <ImageView
            android:id="@+id/animation_imageview"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerHorizontal="true"
            android:src="@drawable/animation_frame"
            android:scaleType="fitCenter"
            android:layout_marginBottom="50dp"/>
    
        <TextView
            android:layout_below="@+id/animation_imageview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="#fff"
            android:fontFamily="sans-serif-thin"
            android:text="Converting Please wait..."/>
    
    </RelativeLayout>
    

    My animation-list in drawable

    <?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/ic_covert_f1" android:duration="90"/>
        <item android:drawable="@drawable/ic_covert_f2" android:duration="90"/>
        <item android:drawable="@drawable/ic_covert_f3" android:duration="90"/>
        <item android:drawable="@drawable/ic_covert_f4" android:duration="90"/>
        <item android:drawable="@drawable/ic_covert_f5" android:duration="90"/>
        <item android:drawable="@drawable/ic_covert_f6" android:duration="90"/>
        <item android:drawable="@drawable/ic_covert_f7" android:duration="90"/>
        <item android:drawable="@drawable/ic_covert_f8" android:duration="90"/>
        <item android:drawable="@drawable/ic_covert_f9" android:duration="90"/>
        <item android:drawable="@drawable/ic_covert_f10" android:duration="90"/>
        <item android:drawable="@drawable/ic_covert_f11" android:duration="90"/>
    
    </animation-list>
    

    My MainActivty.java
    Always remember to use Runnable to start the animation

    public class MainActivity extends AppCompatActivity {
    
        AnimationDrawable progressAnimation;
        ImageView imageView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            imageView = (ImageView)findViewById(R.id.animation_imageview);
            progressAnimation =    (AnimationDrawable)imageView.getDrawable();
            progressAnimation.setCallback(imageView);
            progressAnimation.setVisible(true, true);
            imageView.post(new Starter());
        }
    
        class Starter implements Runnable {
            public void run() {
                progressAnimation.start();
            }
        }
    }
    



    Works Perfectly Good Luck :)

    0 讨论(0)
  • 2020-12-28 09:07

    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" />
    
    0 讨论(0)
提交回复
热议问题