【问题标题】:My AnimationDrawable object's run method slows down my application, any solutions?我的 AnimationDrawable 对象的 run 方法减慢了我的应用程序,有什么解决方案吗?
【发布时间】: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


【解决方案1】:

我认为调用它 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);
        }

【讨论】:

  • 还是一样。我什至尝试在它们之间切换 animation.start() 行和 handleAnimation() 实现行。但 UI 运行速度仍然相当缓慢。
  • 你能发布完整的课程吗?
【解决方案2】:

我发现自己的问题:D。这是一个愚蠢的问题,它基于我安装到 Android Studio、Android Drawable Importer 的资产插件。问题是图像太大,无法加载到高清屏幕模拟器,所以它自然会减慢应用程序的速度。当我将其配置为应有的正常尺寸时,它解决了我的问题。很抱歉占用您的时间:D。感谢所有试图帮助我的人。祝大家编码愉快。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-30
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多