Image optimization for Android app memory optimization

Source: Internet
Author: User

Do not set the background of the button to selector

?? In the layout file and code, you can set background as selector for the button, which makes it easy to implement the front and back effects of the buttons, but the actual trace finds that if you set the button's background to selector, When the button is initialized, the positive and negative images are loaded in memory (specifically, you can view the Android source code, the method of the class Drawable.java createFromXmlInner to parse the image, the method of final call Drawable inflate ), The equivalent of a button occupies two images of the same size used memory, if the button is a lot of buttons or a large number of buttons, just the memory of the button is very large, you can set the button in the layout file in the normal state of the background picture, and then in the code to listen to the button's click State, When the button is pressed, the picture that sets the reverse effect for the button is reset to the background under normal state when lifted.

If you think that a button to provide a positive and negative selection of two images will cause the size of the APK, you can use the following way to achieve the reverse selection of button Click, this way there is no button to occupy twice times the memory of the case, but also reduce the size of the APK (Android A similar effect can be achieved with Tintcolor in 5.0):

ImageButton personalinfobtn = (ImageButton) Findviewbyid (R.id.personalbtnid);
Personalinfobtn.setontouchlistener (New Ontouchlistener () {
@SuppressLint ("Clickableviewaccessibility")
@Override
public boolean OnTouch (View V, motionevent event) {
int action = Event.getaction ();
if (action = = Motionevent.action_down) {
((ImageButton) v). Setcolorfilter (Getresources (). GetColor (0x50000000));
}else if (action = = MOTIONEVENT.ACTION_UP | | action = = motionevent.action_cancel) {
((ImageButton) v). Clearcolorfilter ();
}
The return value should be false in order not to affect the onclick callback of the listener button
return false;
}
});

Other optimization methods

1, as far as possible to the activity in the small picture and background to merge, a small picture is not only a waste of the layout of time, but also to increase the memory footprint;

2. Do not set the default background image for activity in the activity theme, which will cause the activity to double the memory consumption:

<!--千万不要在主题中为Activity设置默认背景 <style name="Activity_Style"parent="@android:Theme.Holo.Light.NoActionBar"><item name="android:background">@drawable/*</item></style>

3, for the picture or layout displayed when needed, you can use the Viewstub label, through the sdk/ The Hierarchyviewer.bat view layout file under the Tools directory will find that components that use the viewstub tag consume little layout time, and then instantiate in code to help improve the layout efficiency of the activity and save the memory consumed by activity.

Note: If you use the WebView, video playback, handwriting, animation and other functions, turn off the hardware acceleration can seriously sound program running efficiency, this situation can only turn off some of the activity in the hardware acceleration of some view, the entire activity hardware acceleration does not shut down.

?? If a view in the activity needs to turn off hardware acceleration, but the entire activity cannot be closed, you can call the view level to turn off hardware acceleration:

setLayerType(View.LAYER_TYPE_SOFTWARE, null);  // view.setLayerType || 在定义view的构造方法中调用该方法

Image optimization for Android app memory optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.