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

1

1

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();
    }
}
Kaylakayle answered 21/4, 2015 at 9:21 Comment(7)
Which type of animation you are using? Show your code.Franni
ok i will post parts of my code, i use animation for those functions, (showing many views sequentially, KenBurnsView, and hiding many views).Kaylakayle
Your new Thread will run at the same priority as the main thread (because it inherits its priority from the parent thread). Probably this isn't the cause of your problem, but it is good practice in Android to always set the priority of your threads to Thread.MIN_PRIORITY or android.os.Process.THREAD_PRIORITY_BACKGROUNDBagworm
@Bagworm Can the problem be the weights i am setting in the xml ? i have created the whole xml depending on weight to make it works for all screen sizesKaylakayle
Honestly don't know - not a situation I've encountered. Are your images very large? Can they be reduced? Does centerCrop actually do anything when you have wrap_content for both width and height? Presumably hardware acceleration is enabled (default in API-level > 14)?Bagworm
Hi, did you manage to get it to work?Hedelman
@Hedelman Yes, it was by reducing the size of the images used in that screen. I posted the answer down belowKaylakayle
K
0

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.

Kaylakayle answered 3/11, 2017 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.