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