Android simplest screen fit, project with DP,SP just add a line of code

Source: Internet
Author: User

Autoscreenadaptation

Android screen adaptation items directly with the DP and SP can be added without modification directly into a code to achieve the adaptation

Not sure if there will be compatibility issues and performance issues that require time testing

At present, the different resolutions of the simulator are tested and the basic effect of different DPI is not much worse.

Rotating the screen layout scales the view by the size of the horizontal version and does not mess

Supports X y adaptation


720x1280
480x800

Main code

Public Classscreenadaptation {

Privateapplication.activitylifecyclecallbacksactivitylifecyclecallbacks;

Privateapplicationmapplication;

Private floatmwidth=720;

Private floatmheight=1280;

Publicscreenadaptation (Application application,floatwidth,intheight) {

mapplication= Application;

Mwidth= width;

mheight= height;

Activitylifecyclecallbacks=newapplication.activitylifecyclecallbacks () {

@Override

Public voidonactivitycreated (activity activity, Bundle savedinstancestate) {

Activate activity before execution

Resetdensity (Activity,mwidth,mheight);

}

@Override

Public voidonactivitystarted (activity activity) {

}

@Override

Public voidonactivityresumed (activity activity) {

}

@Override

Public voidonactivitypaused (activity activity) {

}

@Override

Public voidonactivitystopped (activity activity) {

}

@Override

Public voidonactivitysaveinstancestate (activity activity, Bundle outstate) {

}

@Override

Public voidonactivitydestroyed (activity activity) {

}

};

}

/**

* Registration

*/

Public Voidregister () {

Resetdensity (Mapplication,mwidth,mheight);

Mapplication.registeractivitylifecyclecallbacks (activitylifecyclecallbacks);

}

/**

* Logout

*/

Public Voidunregister () {

Set as Default

Mapplication.getresources (). Getdisplaymetrics (). Settodefaults ();

Mapplication.unregisteractivitylifecyclecallbacks (activitylifecyclecallbacks);

}

/**

* DP Adapter Getresources (). Getdisplaymetrics (). Density

* SP Adapter getresources (). Getdisplaymetrics (). scaleddensity

* PT Adapter Getresources (). Getdisplaymetrics (). xdpi

* @paramcontext

* Width of @paramwidthui design drawing

* @paramheightui the height of the design diagram

*/

private static voidresetdensity (Context context,floatwidth, floatheight) {

Point Point =newpoint ();

Get the value of the screen

((WindowManager) Context.getsystemservice (Window_service)). Getdefaultdisplay (). GetSize (point);

DP Adapter Getresources (). Getdisplaymetrics (). Density

Context.getresources (). Getdisplaymetrics (). density= point.x/width*2f;

Context.getresources (). Getdisplaymetrics (). density= point.y/height*2f;

SP Adapter getresources (). Getdisplaymetrics (). scaleddensity

Context.getresources (). Getdisplaymetrics (). scaleddensity= point.x/width*2f;

Context.getresources (). Getdisplaymetrics (). scaleddensity= point.y/height*2f;

}

}

Use:

Public classappextendsapplication{

@Override

Public Voidoncreate () {

Super.oncreate ();

The size that needs to be passed into the UI design, initialize

Newscreenadaptation (this,720,1280). Register ();

}

}



Warm start
Links: http://www.jianshu.com/p/97b107308923
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Android simplest screen fit, project with DP,SP just add a line of code

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.