EDIT:
After doing a method trace, whenever I switch with the purple square, the graph rises a lot. When I switch with a blue or green circle, it stays low:
Here are the methods of the swap with the purple square:
I have an app which swaps two buttons. There is one red button, when it gets clicked, it picks a random button from the other three, and swaps.
I tried doing this one way by just swapping images. In the onClickListener, I only listened for clicks with buttons with the red button.
- Get random image
- Make it blank
- Look for red button
- Give red button background that random image had (swap)
- Give random image (which is blank now) red background
If this is confusing...leave it. But:
Whenever I switch images from a larger image to a smaller image, the process takes a much longer time. It is much faster when swapping between two smaller images. How do I fix this. Is there a certain image size which is optimal for android?
Here is the layout code:
https://gist.github.com/anonymous/813883bce89606d2a82e
As you can see in the image, it takes a much (MUCH!) shorter time swapping background images with the blue circle than the purple square. Why...?
Why is the way (where I just change the images and check for a button with a red image) slower based on the image sizes? What image size should I use?
Thanks so much for your time
My first guess would be that you have some big PNGs that take a long time to load.
But, we have nice tools to help us actually find out -- so we don't have to guess!
Do a method trace (available in the CPU tab in Android Studio, or the DDMS view in Eclipse+ADT) while doing the switch, and you should see what is taking time.
I dont have the exact answer for your question but just 2 recommendations:
1) Make sure you scale the image first before drawing.
2) Using RelativeLayout causes your ViewGroup to measure your children twice instead of one. So Make sure that you dont do any that trigger re-layout on click events
3) I would suggest that you put these View in a GridView or GridLayout and then when you need to swap your views, just swap the positions of your view in an data (Array, for example), and call notifyDataSetChanged();
Hope this help
Apparently, the image size can affect the speed. So, what I did was re size my image. I did this in photo shop, but you can do it in paint
or you can do it in an image-re sizer website. Just search that up in google, and enter the dimentions 56 x 56
. For some reason, those dimensions work best.
Here is one image resizer tool:
http://www.picresize.com/
On this website, just select your image from your computer, and then the next screen will say to resize it:
To do it in paint
:
Although the bounty is over, I have just posted this to help people know how to do it. I STILL DON"T KNOW WHY THE IMAGE SIZE MATTERS I would really appreciate it if someone explained why I must fix the image size, although I know how.
Thanks,
NullPointerException