Optimizing paging between ImageViews in Android?
-
19-09-2019 - |
Question
For the application I'm currently working on, I created something like ImageSwitcher
for multiple images (imagine iPhone's UIScrollView
with paging enabled).
At first, I hard coded some ImageViews
and loaded their drawable resources on creation/inflation, but after some tweaks (and what I thought were improvements), I reduced the needed views to 3, of which 2 are ImageViews
used for displaying of the current page and animating between current and new page.
With this change, I began to use setImageResource
/setImageDrawable
for the dynamic loading of the new image in my 2 ImageViews
before sliding through pages (I am using PNG files, sized for HVGA displays).
To my disappointment, the performance got worse, to the extent of feeling some lag when sliding faster through the pages.
A quick look in TraceView
revealed that 17.4% of total time while using the app, over 5 times more than the next heavy method, was consumed from BitmapFactory.nativeDecodeAsset
, called by my dynamic setting of drawable resource.
I'm thinking of ways to circumvent this, and I want to do it in the best way possible, so any suggestions are welcome.
Solution
In which format do you have the images?
Android works natively with RGB565 images, so if you can convert your images to that format, decoding will be much faster.