The method of matching Android resolution _android

Source: Internet
Author: User
When I was working on a project in my company, I met the problem of resolution matching, said it was quite tangled, because there is no extranet, so this problem, are groping in the dark, tried a lot of methods, and finally with the disciples together to make this method, concrete line is not, first share with you, this method can not say omnipotent, But at least it solves the resolution and density of the relationship, but also led to a problem, is that the layout will be because of the small picture resources and distortion, so this also requires the art of comrade a lot of cooperation, nonsense do not say, paste code:
The first step is to create a JavaBean class that first view information
Copy Code code as follows:

Package com.zte.layout.adapter;
Import Android.view.View;
/**
* JavaBean class that stores view information
*
* @author
*
*/
public class Layoutinformation
{
/**
* Object of view
*/
Private view view;
/**
* Width of view
*/
Private double viewwidth;
/**
* Height of view
*/
Private double viewheight;
/**
* View distance from the left, that is, marginleft
*/
Private double viewmarginleft;
/**
* View distance from the top, that is, margintop;
*/
Private double viewmargintop;
/**
* The type of the parent class layout is relative layout
*/
public static int r=-1;
/**
* The type of the parent class layout is a linear layout
*/
public static int l=-2;
/**
* The type of the parent class layout for this view
*/
private int parentlayouttype;
/**
* JavaBean class that stores view information
*
* @param view
* Object of view
* @param viewwidth
* View's width
* @param viewheight
* View of the High
* @param viewmarginleft
* View distance from the left
* @param viewmargdoubleop
* View distance from the upper
* @param parentlayouttype
* The type of the parent class layout, LAYOUTINFORMATION.R
* (indicates relative layout) or LAYOUTINFORMATION.L (indicates a 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 object of the view
*
* [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;
}
/**
* Get the width of the view
*
* @return View width, double
*/
Public double Getviewwidth ()
{
return viewwidth;
}
/**
* Set the width of view, double
*
* @param viewwidth
*/
public void Setviewwidth (double viewwidth)
{
This.viewwidth = Viewwidth;
}
/**
* Get the height of the view
*
* @return The height of view, double type
*/
Public double Getviewheight ()
{
return viewheight;
}
/**
* Set the view height, double type
*
* @param viewheight
*/
public void Setviewheight (double viewheight)
{
This.viewheight = Viewheight;
}
/**
* Get the view distance from the left
*
* @return view distance from the left, double type
*/
Public double Getviewmarginleft ()
{
return viewmarginleft;
}
/**
* Set view distance from the left, double type
*
* @param viewmarginleft
*/
public void Setviewmarginleft (double viewmarginleft)
{
This.viewmarginleft = Viewmarginleft;
}
/**
* Get the view distance from the top
*
* @return view distance from the upper part, double type
*/
Public double Getviewmargintop ()
{
return viewmargintop;
}
/**
* Set the view distance from the top, double type
*
* @param viewmargdoubleop
*/
public void Setviewmargintop (double viewmargintop)
{
This.viewmargintop = Viewmargintop;
}
/**
* Get the type of the parent class layout
* @return Parentlayouttype,int Type
*/
public int Getparentlayouttype ()
{
return parentlayouttype;
}
/**
* Set the type of the parent class layout
* @param parentlayouttype
*/
public void Setparentlayouttype (int parentlayouttype)
{
This.parentlayouttype = Parentlayouttype;
}
}

Step Two: Create a calculation method
Copy Code code 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;
/**
* Distribution Rate Pass
*
* @author
*
*/
public class Mylayoutadapter
{
/**
* The width of the base resolution
*/
public double standard_screen_width;
/**
* High resolution of the base
*/
public double standard_screen_height;
/**
* The current resolution of the system is wide
*/
public double current_screen_width;
/**
* The current resolution of the system is high
*/
public double current_screen_height;
/**
* Base 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;
/**
* The width of the component datum
*/
Private double viewstandardwidth;
/**
* The height of the component datum
*/
Private double viewstandardheight;
/**
* The distance from the left of the component datum
*/
Private double viewstandardmarginleft;
/**
* The distance from the top of the component datum
*/
Private double viewstandardmargintop;
/**
* The current width of the component
*/
Private double viewcurrentwidth;
/**
* component is currently high
*/
Private double viewcurrentheight;
/**
* The current distance from the left of the component
*/
Private double viewcurrentmarginleft;
/**
* The distance from the top of the current component
*/
Private double viewcurrentmargintop;
/**
* Objects for the UI component
*/
Private view view;
/**
* The type of the parent class layout for this view
*/
private int parentlayouttype;
/**
* The type of the parent class layout is relative layout
*/
Private final int layout_type_relativelayout = LAYOUTINFORMATION.R;
/**
* The type of the parent class layout is a linear layout
*/
Private final int layout_type_linearlayout = LAYOUTINFORMATION.L;
/**
* Layout property is Wrap_content
*/
Private final int layoutparams_warp_content = layoutparams.wrap_content;
/**
* Layout property is Fill_parent
*/
Private final int layoutparams_fill_parent = layoutparams.fill_parent;
private context;
/**
* When class object is instantiated, set the datum screen width, height
*
* @param context
* Context
* @param standardwidth
* The width of the base screen
* @param standardheight
* The height of the benchmark screen
*/
Public Mylayoutadapter (context, double standardwidth,
Double standardheight)
{
This.context = context;
Getscreensize ();
Standard_screen_height = StandardHeight;
Standard_screen_width = StandardWidth;
Calculate the width-height ratio
Width_ratio = Current_screen_width/standard_screen_width;
Height_ratio = Current_screen_height/standard_screen_height;
}
/**
* Get current screen size and density
*/
private void Getscreensize ()
{
Displaymetrics displaymetrics = new Displaymetrics ();
(activity). Getwindowmanager (). Getdefaultdisplay ()
Getmetrics (Displaymetrics);
Current_screen_width = Displaymetrics.widthpixels;
Current_screen_height = Displaymetrics.heightpixels;
current_density = displaymetrics.densitydpi;
Density_ratio = standard_density/current_density;
}
/**
* For the distribution
*
* @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 property, set the properties of the layout
*/
private void Setlayoutparams ()
{
If the width of the datum is Wrap_content or fill_parent the original value is used, otherwise the value after the wildcard is calculated
if (Viewstandardwidth = = Layoutparams_warp_content
|| Viewstandardwidth = = layoutparams_fill_parent)
{
Viewcurrentwidth = Viewstandardwidth;
} else
{
Viewcurrentwidth = Viewstandardwidth * width_ratio;
}
If the width of the datum is Wrap_content or fill_parent the original value is used, otherwise the value after the wildcard is calculated
if (viewstandardheight = = Layoutparams_warp_content
|| Viewstandardheight = = layoutparams_fill_parent)
{
Viewcurrentheight = Viewstandardheight;
} else
{
Viewcurrentheight = Viewstandardheight * height_ratio;
}
}
/**
* Set the layout for this view by determining the layout type of this view parent class
*/
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 Font size
*
* @param standardsize
* Original Size
* @return int
*/
public int settextsize (int standardsize)
{
int currentsize;
CurrentSize = (int) (Standardsize * width_ratio * density_ratio);
return currentsize;
}
}

The third step is to write an interface
Copy Code code as follows:

public interface initallview{
/**
* Initialize the size of the control
*/
public void Initallview ();
}

Fourth Step: Code Control
Copy Code code as follows:

/**
* Pass Formula 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.