android resolution adaptation method

  • 2020-05-07 20:23:08
  • OfStack

Project to do in the company before, the resolution of the adaptation problems, it was very tangled, because there is no outside network, so the question, is the dark, tried many methods, finally and his disciples 1 up this method, the specific line not line, to share with you the first, this method can't say everything, but at least it solved the resolution to the relationship between the intensity, but also lead to a problem, is the layout by the distortion of image resources, so it also need artist comrades joined together, a lot of nonsense, post code:
step 1, first create an javabean class of view information:
 
package com.zte.layout.adapter; 
import android.view.View; 
/** 
*  storage View The information of JavaBean class  
* 
* @author 
* 
*/ 
public class LayoutInformation 
{ 
/** 
* View The object of  
*/ 
private View view; 
/** 
* View The width of the  
*/ 
private double viewWidth; 
/** 
* View The height of the  
*/ 
private double viewHeight; 
/** 
* View The distance to the left, that is marginLeft 
*/ 
private double viewMarginLeft; 
/** 
* View The distance from the top , namely MarginTop; 
*/ 
private double viewMarginTop; 
/** 
*  The parent layout is of type relative  
*/ 
public static int R=-1; 
/** 
*  The type of parent layout is a linear layout  
*/ 
public static int L=-2; 
/** 
*  this View The type of the parent layout  
*/ 
private int parentLayoutType; 
/** 
*  storage View The information of JavaBean class  
* 
* @param view 
* View The object of  
* @param viewWidth 
* View The width of  
* @param viewHeight 
* View The high  
* @param viewMarginLeft 
* View The distance to the left  
* @param viewMargdoubleop 
* View The distance from the top  
* @param parentLayoutType 
*  The type of the parent layout ,LayoutInformation.R 
* ( Represents relative layout ) or LayoutInformation.L( Represents 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; 
} 
/** 
*  To obtain View The object of  
* 
* [url=home.php?mod=space&uid=7300]@return[/url] View object  
*/ 
public View getView() 
{ 
return view; 
} 
/** 
*  Set up the View The object of  
*/ 
public void setView(View view) 
{ 
this.view = view; 
} 
/** 
*  To obtain View The width of the  
* 
* @return View The width of the ,double type  
*/ 
public double getViewWidth() 
{ 
return viewWidth; 
} 
/** 
*  Set up the View The width of the double type  
* 
* @param viewWidth 
*/ 
public void setViewWidth(double viewWidth) 
{ 
this.viewWidth = viewWidth; 
} 
/** 
*  To obtain View The height of the  
* 
* @return View The height of the ,double type  
*/ 
public double getViewHeight() 
{ 
return viewHeight; 
} 
/** 
*  Set up the View The height of the, double type  
* 
* @param viewHeight 
*/ 
public void setViewHeight(double viewHeight) 
{ 
this.viewHeight = viewHeight; 
} 
/** 
*  To obtain View The distance to the left  
* 
* @return View The distance to the left, double type  
*/ 
public double getViewMarginLeft() 
{ 
return viewMarginLeft; 
} 
/** 
*  Set up the View The distance to the left ,double type  
* 
* @param viewMarginLeft 
*/ 
public void setViewMarginLeft(double viewMarginLeft) 
{ 
this.viewMarginLeft = viewMarginLeft; 
} 
/** 
*  To obtain View Distance from the top  
* 
* @return View The distance from the top, double type  
*/ 
public double getViewMarginTop() 
{ 
return viewMarginTop; 
} 
/** 
*  Set up the View The distance from the top, double type  
* 
* @param viewMargdoubleop 
*/ 
public void setViewMarginTop(double viewMarginTop) 
{ 
this.viewMarginTop = viewMarginTop; 
} 
/** 
*  Gets the type of the parent layout  
* @return parentLayoutType . int type  
*/ 
public int getParentLayoutType() 
{ 
return parentLayoutType; 
} 
/** 
*  Sets the type of the parent layout  
* @param parentLayoutType 
*/ 
public void setParentLayoutType(int parentLayoutType) 
{ 
this.parentLayoutType = parentLayoutType; 
} 
} 

step 2: create a calculation method :
 
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; 
/** 
*  Distributive ratio generalizes classes  
* 
* @author 
* 
*/ 
public class MyLayoutAdapter 
{ 
/** 
*  The width of the reference resolution  
*/ 
public double STANDARD_SCREEN_WIDTH; 
/** 
*  High reference resolution  
*/ 
public double STANDARD_SCREEN_HEIGHT; 
/** 
*  The width of the current resolution of the system  
*/ 
public double CURRENT_SCREEN_WIDTH; 
/** 
*  The current resolution of the system is high  
*/ 
public double CURRENT_SCREEN_HEIGHT; 
/** 
*  Reference 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 benchmark  
*/ 
private double viewStandardWidth; 
/** 
*  The height of the component reference  
*/ 
private double viewStandardHeight; 
/** 
*  The distance to the left of the component reference  
*/ 
private double viewStandardMarginLeft; 
/** 
*  The distance between the component reference and the top  
*/ 
private double viewStandardMarginTop; 
/** 
*  The current width of the component  
*/ 
private double viewCurrentWidth; 
/** 
*  The current height of the component  
*/ 
private double viewCurrentHeight; 
/** 
*  The distance from the current component to the left  
*/ 
private double viewCurrentMarginLeft; 
/** 
*  The current distance of the component from the top  
*/ 
private double viewCurrentMarginTop; 
/** 
* UI Object of a component  
*/ 
private View view; 
/** 
*  this View The type of the parent layout  
*/ 
private int parentLayoutType; 
/** 
*  The parent layout is of type relative  
*/ 
private final int LAYOUT_TYPE_RELATiVELAYOUT = LayoutInformation.R; 
/** 
*  The type of parent layout is a linear layout  
*/ 
private final int LAYOUT_TYPE_LINEARLAYOUT = LayoutInformation.L; 
/** 
*  The layout property is wrap_content 
*/ 
private final int LAYOUTPARAMS_WARP_CONTENT = LayoutParams.WRAP_CONTENT; 
/** 
*  The layout property is fill_parent 
*/ 
private final int LAYOUTPARAMS_FILL_PARENT = LayoutParams.FILL_PARENT; 
private Context context; 
/** 
*  Class object instantiated , Set up the   Reference screen width , highly  
* 
* @param context 
* Context 
* @param standardWidth 
*  The width of the benchmark screen  
* @param standardHeight 
*  The height of the reference 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; 
} 
/** 
*  Gets the current screen size and density  
*/ 
private void getScreenSize() 
{ 
DisplayMetrics displayMetrics = new DisplayMetrics(); 
((Activity) context).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 wildcard  
* 
* @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(); 
} 
} 
/** 
*  Determines the value of the layout property and sets the properties of the layout  
*/ 
private void setLayoutParams() 
{ 
//  If the base width 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 base width 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; 
} 
} 
/** 
*  By judging this View The layout type of the parent class , To this View Set the layout  
*/ 
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 1 interface :
 
public interface InitAllView{ 
/** 
*  Size of the initialization control  
*/ 
public void initAllView(); 
} 

step 4: code control :
 
/** 
*  Wildcard 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 articles: