handling too many requests of animation on the main ui thread from other threads - android

僤鯓⒐⒋嵵緔 提交于 2019-12-08 09:01:16

问题


I am working on creating activity that has too many animations, and when starting this activity the logcat shows me this message again and again during the life time of this activity:

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

so I did many stuff in another threads, but still there is heavy access on the main UI thread. as well as, the animation is becoming really slow on some high resolution devices

what can be possible solution for handling this problem ?

UPDATED: added code

so here is the code of showing the views(which are 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);
            }
        });
    }
}

I trigger it in seperated thread like this:

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

In the same activity i have Ken Burns View, which implement ken burns effect on 2 images, The code of this kenBurnsView is in this link: KenBurnsView

so this is the main activity 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>

This is the main activity java part:

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:


I reduced the sizes and a little bit the resolution of images for Kenburns and the buttons backgrounds in order to get it working smoothly. Playing with threads did not solve the issue of lagging for me.



来源:https://stackoverflow.com/questions/29767997/handling-too-many-requests-of-animation-on-the-main-ui-thread-from-other-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!