Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate a group of views efficiently?

I have a base layout that holds several views. I need to translate them along the x axis. Basically there are 2 types of translations happening the one that follows the finger all the way and the one that stops after certain threshold which gives a nice parallax-like effect.

So is it better to put the group of views that share same translation logic inside another container and translate the container or translate each view individually?

I know this might sound weird but Google always recommends to use flat view hierarchy because nested layouts are expensive to redraw, so does it mean that keeping just one layer of nesting and translating each view by hand is better?

Also this translation is applied to all the views in the RecyclerView, not just one.

like image 992
Arnis Shaykh Avatar asked May 22 '18 15:05

Arnis Shaykh


2 Answers

I think you should keep a flat layout as Google recommends, and translate each view individually. I would create a custom view were you can set a threshold. And once set, you can simply pass the x position of the finger to them all, and they'll decide if they should move or not.

like image 101
Anthony Cannon Avatar answered Oct 04 '22 13:10

Anthony Cannon


How to implement it requires more input or requirements before giving any suggestion. Besides performance problem, code maintainable should be considered too.

Base on my experience. With high-end phone and high API level (about >= 21), they have better hardware, effective way to manage memory and background task. The problem expensive to redraw you mentioned seems not too important with some simple animation. In this situation, code maintainability has higher priority, I will decide to have an extra FrameLayout container, wrap the View which have the same animation because you have less code => less logic. 0 or 2 FrameLayout have no noticeable difference in performance in this case

About low-end devices, if you want to target lower API users, performance becomes a top priority. Now both cases you mentioned have the effect on performance, one requires more memory to store more View and one requires more CPU to run animation. It's time for a trade-off. In your case when you have 2 translate animations run on any item in RecyclerView, I prefer to create 2 separate animations run on 2 View. This way I can save a bunch of extra containers => save memory, the animation is not run for all of the items so it only affects the CPU for a small amount of time during animation.

So to sum up, You have a different approach for each case, choose a top-priority for the case you choose, improve it, sacrifice the others that have less effect on the overall problem. No solution is 100% perfect, trade-off situation always happens when coding

like image 30
Tam Huynh Avatar answered Oct 04 '22 13:10

Tam Huynh