从其他线程处理主ui线程上的动画请求太多 - android

时间:2015-04-21 09:21:58

标签: java android multithreading ui-thread

我正在创建具有太多动画的活动,并且在开始此活动时,logcat会在此活动的生命周期内一次又一次地向我显示此消息:

I/Choreographer﹕ Skipped 36 frames!  The application may be doing too much work on its main thread.

所以我在另一个线程中做了很多东西,但仍然在主UI线程上有大量访问权限。而且,动画在一些高分辨率设备上变得非常慢

处理此问题的可行解决方案是什么?

更新:添加代码

所以这里是显示视图的代码(6个imageButtons),

private void setupAnimationForAllViews(ArrayList<View> listOfViews,
                                       int animationId,
                                       final boolean isAppearing) {
    int startDelay = mDelay; // milliseconds
    int numberOfViews = listOfViews.size();

    for (int i = 0; i < numberOfViews; i++) {
        final Animation animator = AnimationUtils.loadAnimation(mContext, animationId);
        animator.setStartOffset(startDelay);
        startDelay += mOffSet; // every view will start after 100 milliseconds from the other
        final View currentView = listOfViews.get(i);
        final int indexOfCurrentCheckedItem = i;

        mMainUIThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                currentView.startAnimation(animator);
            }
        });
    }
}

我在这样的分离线程中触发它:

new Thread(new Runnable() {
        @Override
        public void run() {
            setupAnimationForAllViews(tempListOfViews, 
                animationId, isAppearing);
        }
    }).start();

在同一活动中,我有Ken Burns View,它对2张图片实施ken burns效果,此kenBurnsView的代码在此链接中:KenBurnsView

所以这是主要的活动xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="#111"
              android:orientation="vertical"
              android:weightSum="7">

    <FrameLayout
        android:id="@+id/header"
        android:layout_weight="6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <yasinseyhan.com.yukselirgroup.Activities.GeneralActivities.KenBurnsView
            android:id="@+id/header_picture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/uphill2" />

        <ImageView
            android:id="@+id/header_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/header_white" />

    </FrameLayout>


    <LinearLayout
        android:id="@+id/dsd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:weightSum="3"
        android:orientation="vertical"
      >

         <View
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:background="@android:color/white"
            android:layout_weight="0.5"/>

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal">


            <ImageView
                android:id="@+id/btnKur"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/btn_kurumsal_selector"/>

            <ImageView
                android:id="@+id/btnGroup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/btn_groups"/>

            <ImageView
                android:id="@+id/btnSektorelFaa"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/btn_sektorel_selector"/>
        </LinearLayout>


        <LinearLayout
            android:id="@+id/secondLineLinearLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal">


            <ImageView
                android:id="@+id/btnInsanKayna"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/btn_ik_selector"/>

            <ImageView
                android:id="@+id/btnGale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/btn_galeri_selector"/>

            <ImageView
                android:id="@+id/btnIlet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/btn_iletisim_selector"/>

        </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_weight="0.5"
                android:background="@android:color/white"/>

    </LinearLayout>

</LinearLayout>

这是java的主要活动部分:

public class MainActivity extends Activity {

    //Buttons in the MainActivity

    AnimationHelper animationHelper;
    ArrayList<View> listOfButtons;
    Handler mUIThreadHandler = new Handler();
    private final int hideOnClickAnimation = R.anim.fade_out;
    private final int displayAnimation = R.anim.test_anim;

    //For Kenburns View with multible images
    private KenBurnsView mKenBurnsView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        initUI();
        initAnimation();
        init();

        // for testing, we may put those in initAnimation()
        animationHelper.setDelay(100);
        animationHelper.setOffSet(50);

        mKenBurnsView.setResourceIds(R.drawable.test16, R.drawable.uphill2);
    }

    private void initAnimation() {

        //Adding OnClickListeners to the Buttons in the MainActivity - End
        // The order is important here
        listOfButtons = new ArrayList<>();
        listOfButtons.add(btnGroupSirketleri);
        listOfButtons.add(btnGale);
        listOfButtons.add(btnSektorelFaa);
        listOfButtons.add(btnKur);
        listOfButtons.add(btnInsanKayna);
        listOfButtons.add(btnIlet);

        // Adding Animation
        animationHelper = new AnimationHelper(listOfButtons, this, mUIThreadHandler);
    }

    private void initUI() {
        //initializing the buttons in the mainactivity
        btnKur = (ImageView) findViewById(R.id.btnKur);
        btnGroup = (ImageView) findViewById(R.id.btnGroup);
        btnSektorelFaa = (ImageView) findViewById(R.id.btnSektorelFaa);
        btnInsanKayna = (ImageView) findViewById(R.id.btnInsanKayna);
        btnGale = (ImageView) findViewById(R.id.btnGale);
        btnIlet = (ImageView) findViewById(R.id.btnIlet);

        // KenBurns View
        mKenBurnsView = (KenBurnsView) findViewById(R.id.header_picture);
    }

    private void init() {
        //Adding OnClickListeners to the Buttons in the MainActivity - Start

        btnKur.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
            }
        });

        btnGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
            }
        });

        btnSektorelFaa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
            }
        });

        btnInsanKayna.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
            }
        });

        btnGale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
            }
        });

        btnIlet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();

        animationHelper.displayViews(displayAnimation, new IDoOnEndOfWholeAnimation() {
            @Override
            public void doIt() {
                if (!mKenBurnsView.isAnimating)
                    mKenBurnsView.startKenBurnsAnimation();
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mKenBurnsView.isAnimating)
            mKenBurnsView.stopKenBurnsAnimation();
    }
}

1 个答案:

答案 0 :(得分:0)

我缩小了Kenburns和按钮背景图像的大小和分辨率,以使其顺利运行。玩线程并没有解决我的滞后问题。