2012-01-04 43 views
15

我在获取动画加载微调器以用于启动页面时遇到了一些问题。当我尝试运行下面的代码时没有任何显示。有什么建议么?似乎有不少人在谷歌上有这个问题,但我不明白为什么我的工作失败。谢谢!使用android动画列表

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> 
+0

具有u对包括本次活动manifest.xml文件è? – sampathpremarathna 2012-01-04 03:44:24

+0

是的,我有。该活动显示为不带动画 – tier1 2012-01-04 04:43:20

回答

22

解决我自己的问题,你不能开始在OnCreate动画。它必须位于onclick监听器或可运行的内部。

+4

,但我的作品是oncreate,而不是onclick。 – 2014-07-11 08:36:33

+3

根据http://developer.android.com/guide/topics/graphics/drawable-animation.html你不应该在onCreate()上调用它。 – rude 2014-12-08 10:21:35

4

您可以从onWindowFocusChanged(boolean hasFocus)方法播放/开始animation

15

我觉得最优雅的和通用的选择是从ImageView的类扩展:

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

位于提拉文件夹中的loader.xml:

<?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> 

现在,包括你的一些看法如此简单:

<com.yourpackage.Loader 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
+0

有3个设备,只能运行在运行Android 4.2.2的Nexus 4上,无法用于蜂窝或姜饼设备。可能必须坚持viewflippers – CQM 2013-06-27 21:45:45

+0

我已更新init方法添加兼容性与蜂窝和姜饼。核实。 – 2013-06-28 07:28:05

+0

作品像一个魅力,谢谢! – CQM 2013-06-28 13:53:22

2

在此处理程序中,动画未完全附加到t他的窗户,所以动画不能开始;相反,这通常是通过用户操作(例如按下按钮)或在onWindowFocusChangedhandler内完成的。

参考:专业的安卓4.0应用程序的开发

0

不要设置在XML代码的图像资源。

我的XML是:

<?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> 

在活动我做

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

可绘制文件

<?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> 

它的工作好:)