【发布时间】:2018-08-10 15:10:44
【问题描述】:
我正在编写一个基本的 Android 应用程序。我确实创建了一个 AnimationDrawable 对象来在我的布局中显示我的多个图像并设置它。在那之后,我的应用程序开始变慢。我什至尝试在新线程上处理该代码块,但它并没有像我想象的那样工作。我做错了什么?或者有什么解决办法吗?提前致谢。
我在我的 onCreate() 方法上调用了这个方法;
/*
* This method configures the background animation
* */
private void configureAnimation(){
animation = new AnimationDrawable();
for (int i = 0; i < bgImage.length; i++) {
animation.addFrame(getResources().getDrawable(bgImage[i]), 5000);
}
animation.setEnterFadeDuration(1000);
animation.setOneShot(false);
linearLayout = (LinearLayout) findViewById(R.id.mLayout);
linearLayout.setBackground(animation);
animation.start();
}
全班;
package com.example.yekta.loginapp;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
private AnimationDrawable animation;
private int []bgImage = {R.drawable.wallpaper00, R.drawable.wallpaper01};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.placeholder_fragment, new LoginFragment());
ft.commit();
hideActionBar();
changeStatusBarColor(R.color.black);
// First Approach
//configureAnimation();
// Second Approach with using Thread
//startProgress();
// Third Approach
animation = new AnimationDrawable();
handleAnimation(100,animation);
animation.start();
}
/*
* This is simple method that changes the color of status bar.
* @param colorCode is the color code that given in value file
* */
private void changeStatusBarColor(final int colorCode) {
if (Build.VERSION.SDK_INT >= 21) {
Window window = this.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(this, colorCode));
}
}
/*
* This is simple method that hides the Action bar.
* */
private void hideActionBar(){
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
}
/*
* This method configures the background animation
* */
private void configureAnimation(){
animation = new AnimationDrawable();
for (int i = 0; i < bgImage.length; i++) {
animation.addFrame(getResources().getDrawable(bgImage[i]), 5000);
}
animation.setEnterFadeDuration(1000);
animation.setOneShot(false);
linearLayout = (LinearLayout) findViewById(R.id.mLayout);
linearLayout.setBackground(animation);
animation.start();
}
public void startProgress() {
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
configureAnimation();
}
});
}
private void handleAnimation(final int time, final AnimationDrawable animation) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
for (int i = 0; i < bgImage.length; i++) {
animation.addFrame(getResources().getDrawable(bgImage[i]), 5000);
}
animation.setEnterFadeDuration(1000);
animation.setOneShot(false);
linearLayout = (LinearLayout) findViewById(R.id.mLayout);
linearLayout.setBackground(animation);
}
}, time);
}
}
【问题讨论】:
-
bgImage.length的值是多少?还有背景图片有多大 -
@Lino 目前该数组中只有两个图像,所以现在是 2 个。
-
让我看看,你想在登录屏幕之前制作一个启动屏幕动画吗?请从 OnCreate 中删除 animation.start()
标签: java android animation animationdrawable