Android resolution adaptation method

Source: Internet
Author: User

In the past, when I was working on a project in the company, I encountered a resolution adaptation problem. At that time, it was quite tangled. Because there was no Internet connection, this problem was all black and dark, and I tried many methods, finally, I worked out this method with the disciples. I can't do it. I 'd like to share it with you first. This method cannot be omnipotent, but at least it solves the relationship between resolution and density, however, the layout will be distorted due to small image resources. This also requires the cooperation of the artist's comrades:
Step 1: first create a view information javabean class: Copy codeThe Code is as follows: package com. zte. layout. adapter;
Import android. view. View;
/**
* JavaBean class for storing View information
*
* @ Author
*
*/
Public class LayoutInformation
{
/**
* View object
*/
Private View view;
/**
* View width
*/
Private double viewWidth;
/**
* View height
*/
Private double viewHeight;
/**
* The distance from the View to the left, that is, marginLeft
*/
Private double viewMarginLeft;
/**
* View distance from the top, that is, MarginTop;
*/
Private double viewMarginTop;
/**
* The parent class layout type is relative layout.
*/
Public static int R =-1;
/**
* The parent class layout is linear.
*/
Public static int L =-2;
/**
* Type of the parent layout of the View
*/
Private int parentLayoutType;
/**
* JavaBean class for storing View information
*
* @ Param view
* View object
* @ Param viewWidth
* View width
* @ Param viewHeight
* View height
* @ Param viewMarginLeft
* View distance from left
* @ Param viewMargdoubleop
* View distance from top
* @ Param parentLayoutType
* LayoutInformation. R
* (Relative layout) or LayoutInformation. L (linear layout)
*/
Public LayoutInformation (View view, double viewWidth, double viewHeight,
Double viewMarginLeft, double viewMarginTop, int parentLayoutType)
{
This. view = view;
This. viewWidth = viewWidth;
This. viewHeight = viewHeight;
This. viewMarginLeft = viewMarginLeft;
This. viewMarginTop = viewMarginTop;
This. parentLayoutType = parentLayoutType;
}
/**
* Get the View object
*
* [Url = home. php? Mod = space & uid = 7300] @ return [/url] View object
*/
Public View getView ()
{
Return view;
}
/**
* Set the View object
*/
Public void setView (View view)
{
This. view = view;
}
/**
* Obtain the View width.
*
* @ Return View width, double
*/
Public double getViewWidth ()
{
Return viewWidth;
}
/**
* Set the View width to double.
*
* @ Param viewWidth
*/
Public void setViewWidth (double viewWidth)
{
This. viewWidth = viewWidth;
}
/**
* Obtain the View height.
*
* @ Return View height, double type
*/
Public double getViewHeight ()
{
Return viewHeight;
}
/**
* Set the View height, double
*
* @ Param viewHeight
*/
Public void setViewHeight (double viewHeight)
{
This. viewHeight = viewHeight;
}
/**
* Obtain the distance from the View to the left.
*
* @ Return View: the distance from the left side. double type.
*/
Public double getViewMarginLeft ()
{
Return viewMarginLeft;
}
/**
* Set the distance from the View to the left, double
*
* @ Param viewMarginLeft
*/
Public void setViewMarginLeft (double viewMarginLeft)
{
This. viewMarginLeft = viewMarginLeft;
}
/**
* Obtain the distance from the View to the upper part.
*
* @ Return View distance from the upper part, double
*/
Public double getViewMarginTop ()
{
Return viewMarginTop;
}
/**
* Set the distance between the View and the upper part, double
*
* @ Param viewMargdoubleop
*/
Public void setViewMarginTop (double viewMarginTop)
{
This. viewMarginTop = viewMarginTop;
}
/**
* Obtain the layout type of the parent class.
* @ Return parentLayoutType, int type
*/
Public int getParentLayoutType ()
{
Return parentLayoutType;
}
/**
* Set the layout type of the parent class.
* @ Param parentLayoutType
*/
Public void setParentLayoutType (int parentLayoutType)
{
This. parentLayoutType = parentLayoutType;
}
}

Step 2: Create a calculation method:Copy codeThe Code is as follows: import java. util. List;
Import android. app. Activity;
Import android. content. Context;
Import android. util. DisplayMetrics;
Import android. view. View;
Import android. view. ViewGroup. LayoutParams;
Import android. widget. LinearLayout;
Import android. widget. RelativeLayout;
/**
* Allocation rate configuration class
*
* @ Author
*
*/
Public class MyLayoutAdapter
{
/**
* Baseline resolution width
*/
Public double STANDARD_SCREEN_WIDTH;
/**
* High benchmark resolution
*/
Public double STANDARD_SCREEN_HEIGHT;
/**
* Current resolution width of the system
*/
Public double CURRENT_SCREEN_WIDTH;
/**
* The current resolution of the system is high.
*/
Public double CURRENT_SCREEN_HEIGHT;
/**
* Baseline screen Density
*/
Public static final double STANDARD_DENSITY = 160;
/**
* Current screen Density
*/
Private double CURRENT_DENSITY;
/**
* Screen density ratio
*/
Private double DENSITY_RATIO;
/**
* Screen Width Ratio
*/
Private double WIDTH_RATIO;
/**
* Screen height ratio
*/
Private double HEIGHT_RATIO;
/**
* Width of the widget reference
*/
Private double viewStandardWidth;
/**
* Component baseline height
*/
Private double viewStandardHeight;
/**
* Distance from the component benchmark to the left
*/
Private double viewStandardMarginLeft;
/**
* Distance from the component baseline to the top
*/
Private double viewStandardMarginTop;
/**
* Current component width
*/
Private double viewCurrentWidth;
/**
* Current component height
*/
Private double viewCurrentHeight;
/**
* Distance from the current component to the left
*/
Private double viewCurrentMarginLeft;
/**
* Current distance from the component to the top
*/
Private double viewCurrentMarginTop;
/**
* UI Component Object
*/
Private View view;
/**
* Type of the parent layout of the View
*/
Private int parentLayoutType;
/**
* The parent class layout type is relative layout.
*/
Private final int LAYOUT_TYPE_RELATiVELAYOUT = LayoutInformation. R;
/**
* The parent class layout is linear.
*/
Private final int LAYOUT_TYPE_LINEARLAYOUT = LayoutInformation. L;
/**
* The layout attribute is wrap_content.
*/
Private final int LAYOUTPARAMS_WARP_CONTENT = LayoutParams. WRAP_CONTENT;
/**
* The layout attribute is fill_parent.
*/
Private final int LAYOUTPARAMS_FILL_PARENT = LayoutParams. FILL_PARENT;
Private Context context;
/**
* When the class object is instantiated, set the baseline screen width and height
*
* @ Param context
* Context
* @ Param standardWidth
* Baseline screen width
* @ Param standardHeight
* Height of the baseline screen
*/
Public MyLayoutAdapter (Context context, double standardWidth,
Double standardHeight)
{
This. context = context;
GetScreenSize ();
STANDARD_SCREEN_HEIGHT = standardHeight;
STANDARD_SCREEN_WIDTH = standardWidth;
// Calculate the ratio of width to height
WIDTH_RATIO = CURRENT_SCREEN_WIDTH/STANDARD_SCREEN_WIDTH;
HEIGHT_RATIO = CURRENT_SCREEN_HEIGHT/STANDARD_SCREEN_HEIGHT;
}
/**
* Get the current screen size and density
*/
Private void getScreenSize ()
{
DisplayMetrics displayMetrics = new DisplayMetrics ();
(Activity) context). getWindowManager (). getdefadisplay display ()
GetMetrics (displayMetrics );
CURRENT_SCREEN_WIDTH = displayMetrics. widthPixels;
CURRENT_SCREEN_HEIGHT = displayMetrics. heightPixels;
CURRENT_DENSITY = displayMetrics. densityDpi;
DENSITY_RATIO = STANDARD_DENSITY/CURRENT_DENSITY;
}
/**
* Wildcard Configuration
*
* @ Param listdata
*/
Public void setViewLayout (List <LayoutInformation> listdata)
{
For (int I = 0; I <listdata. size (); I ++)
{
View = listdata. get (I). getView ();
ViewStandardWidth = listdata. get (I). getViewWidth ();
ViewStandardHeight = listdata. get (I). getViewHeight ();
ViewStandardMarginLeft = listdata. get (I). getViewMarginLeft ();
ViewStandardMarginTop = listdata. get (I). getViewMarginTop ();
SetLayoutParams ();
ViewCurrentMarginLeft = viewStandardMarginLeft * WIDTH_RATIO;
ViewCurrentMarginTop = viewStandardMarginTop * HEIGHT_RATIO;
ParentLayoutType = listdata. get (I). getParentLayoutType ();
SetLayoutByParentLayoutType ();
}
}
/**
* Determine the value of the layout attribute and set the layout attribute
*/
Private void setLayoutParams ()
{
// If the baseline width is wrap_content or fill_parent, the original value is used. Otherwise, the value after configuration is calculated.
If (viewStandardWidth = LAYOUTPARAMS_WARP_CONTENT
| ViewStandardWidth = LAYOUTPARAMS_FILL_PARENT)
{
ViewCurrentWidth = viewStandardWidth;
} Else
{
ViewCurrentWidth = viewStandardWidth * WIDTH_RATIO;
}
// If the baseline width is wrap_content or fill_parent, the original value is used. Otherwise, the value after configuration is calculated.
If (viewStandardHeight = LAYOUTPARAMS_WARP_CONTENT
| ViewStandardHeight = LAYOUTPARAMS_FILL_PARENT)
{
ViewCurrentHeight = viewStandardHeight;
} Else
{
ViewCurrentHeight = viewStandardHeight * HEIGHT_RATIO;
}
}
/**
* Determine the layout type of the parent View class and set the layout for this View.
*/
Private void setLayoutByParentLayoutType ()
{
If (parentLayoutType = LAYOUT_TYPE_RELATiVELAYOUT)
{
RelativeLayout. LayoutParams params = new RelativeLayout. LayoutParams (
(Int) viewCurrentWidth, (int) viewCurrentHeight );
Params. setMargins (int) viewCurrentMarginLeft,
(Int) viewCurrentMarginTop, 0, 0 );
View. setLayoutParams (params );
} Else if (parentLayoutType = LAYOUT_TYPE_LINEARLAYOUT)
{
LinearLayout. LayoutParams params = new LinearLayout. LayoutParams (
(Int) viewCurrentWidth, (int) viewCurrentHeight );
Params. setMargins (int) viewCurrentMarginLeft,
(Int) viewCurrentMarginTop, 0, 0 );
View. setLayoutParams (params );
}
}
/**
* Set the font size.
*
* @ Param standardSize
* Original size
* @ Return int
*/
Public int setTextSize (int standardSize)
{
Int currentSize;
CurrentSize = (int) (standardSize * WIDTH_RATIO * DENSITY_RATIO );
Return currentSize;
}
}

Step 3: Write an Interface:Copy codeThe Code is as follows: public interface InitAllView {
/**
* Size of the initialized Control
*/
Public void initAllView ();
}

Step 4: code control:Copy codeThe Code is as follows :/**
* Configuration method
*/
Private void initWildcard (){
MyLayout = new MyLayoutAdapter (this, 320,480 );
ListInfo = new ArrayList <LayoutInformation> ();
ListInfo. add (new LayoutInformation (mBtn1, LayoutParams. WRAP_CONTENT,
LayoutParams. WRAP_CONTENT, 0, 0, LayoutInformation. R ));
ListInfo. add (new LayoutInformation (mNowRegisterBtn, 80, 27.3, 14.7, 0,
LayoutInformation. R ));
ListInfo. add (new LayoutInformation (mNextRegisterBtn, 80, 27.3, 14.7, 0,
LayoutInformation. R ));
// ListInfo. add (new LayoutInformation (mCheckBtn, 17.3, 17.3, 14.7, 0,
// LayoutInformation. L ));
MBtn1.setTextSize (myLayout. setTextSize (12 ));
MNowRegisterBtn. setTextSize (myLayout. setTextSize (12 ));
MNextRegisterBtn. setTextSize (myLayout. setTextSize (12 ));
MyLayout. setViewLayout (listInfo );
}

Related Article

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.