Ответ 1
Решил мою собственную проблему, вы не можете запускать анимацию в oncreate. Он должен быть в приемнике onclick или внутри исполняемого файла.
У меня есть небольшая проблема с тем, чтобы анимированный загрузчик загрузки работал на странице всплеска. Ничего не появляется, когда я пытаюсь запустить следующий код. Какие-либо предложения? Похоже, что у многих людей есть проблемы с этим в google, но я не понимаю, почему мой не работает. Спасибо!
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>
Решил мою собственную проблему, вы не можете запускать анимацию в oncreate. Он должен быть в приемнике onclick или внутри исполняемого файла.
Я думаю, что самый элегантный и универсальный вариант - это расширение от класса 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();
}
});
}
}
Загружаемый файл .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" />
Вы можете играть/запускать animation
из onWindowFocusChanged(boolean hasFocus)
.
Внутри этого обработчика анимация не полностью привязана к окну, поэтому анимация не может быть запущена; вместо этого это обычно делается в результате действия пользователя (например, нажатия кнопки) или внутри onWindowFocusChangedhandler
.
обратитесь: профессиональная разработка приложений для Android 4
Не устанавливайте ресурс изображения в 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>
В действии я 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();
}
}
Рисованный файл
<?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 Working Good:)