我正在编写一个基本的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);
}
}
答案 0 :(得分:0)
我认为将其命名为onResume()更安全,如果在OnCreate()上启动动画,则可能会获得ANR,并且可以尝试以下方法:
animation = new AnimationDrawable();
animation.start();
handleAnimation(100,animation);
private void handleAnimation(final int time, final AnimationDrawable animationDrawable) {
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);
}
答案 1 :(得分:0)
我发现了自己的问题:D。那是一个愚蠢的问题,它基于我安装到Android Studio,Android Drawable Importer的资产插件。问题在于图像是如此之大,无法加载到高清屏幕模拟器中,因此自然降低了应用程序的速度。当我将其配置为应有的正常大小时,它解决了我的问题。很抱歉抽出您的时间:D。谢谢所有试图帮助我的人。祝大家编码愉快。