SoFunction
Updated on 2025-03-08

Android resolution adaptation method


import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* Distribution rate wild classification
*
* @author
*
*/
public class MyLayoutAdapter
{
/**
* Width of 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 high resolution of the system
*/
public double CURRENT_SCREEN_HEIGHT;
/**
*Basic 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 component reference
*/
private double viewStandardWidth;
/**
* Height of component reference
*/
private double viewStandardHeight;
/**
* The distance to the left of the component reference
*/
private double viewStandardMarginLeft;
/**
* The distance from the top of the component reference
*/
private double viewStandardMarginTop;
/**
* The current width of the component
*/
private double viewCurrentWidth;
/**
* The current high of the component
*/
private double viewCurrentHeight;
/**
* The current distance from the left of the component
*/
private double viewCurrentMarginLeft;
/**
* The current distance from the top of the component
*/
private double viewCurrentMarginTop;
/**
* Objects of UI components
*/
private View view;
/**
* Type of parent class layout of this View
*/
private int parentLayoutType;
/**
* The parent class layout type is relative layout
*/
private final int LAYOUT_TYPE_RELATiVELAYOUT = ;
/**
* The parent class layout type is linear layout
*/
private final int LAYOUT_TYPE_LINEARLAYOUT = ;
/**
* 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 instantiating a class object, set the reference screen width and height
*
* @param context
* Context
* @param standardWidth
* The width of the reference screen
* @param standardHeight
* High of the baseline screen
*/
public MyLayoutAdapter(Context context, double standardWidth,
double standardHeight)
{
= context;
getScreenSize();
STANDARD_SCREEN_HEIGHT = standardHeight;
STANDARD_SCREEN_WIDTH = standardWidth;
// Calculate aspect ratio
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().getDefaultDisplay()
getMetrics(displayMetrics);
CURRENT_SCREEN_WIDTH = ;
CURRENT_SCREEN_HEIGHT = ;
CURRENT_DENSITY = ;
DENSITY_RATIO = STANDARD_DENSITY / CURRENT_DENSITY;
}
/**
* Perform aligned
*
* @param listdata
*/
public void setViewLayout(List<LayoutInformation> listdata)
{
for (int i = 0; i < (); i++)
{
view = (i).getView();
viewStandardWidth = (i).getViewWidth();
viewStandardHeight = (i).getViewHeight();
viewStandardMarginLeft = (i).getViewMarginLeft();
viewStandardMarginTop = (i).getViewMarginTop();
setLayoutParams();
viewCurrentMarginLeft = viewStandardMarginLeft * WIDTH_RATIO;
viewCurrentMarginTop = viewStandardMarginTop * HEIGHT_RATIO;
parentLayoutType = (i).getParentLayoutType();
setLayoutByParentLayoutType();
}
}
/**
* Determine the value of the layout attribute and set the layout attributes
*/
private void setLayoutParams()
{
// If the width of the reference is wrap_content or fill_parent, use the original value, otherwise calculate the wild-paired value
if (viewStandardWidth == LAYOUTPARAMS_WARP_CONTENT
|| viewStandardWidth == LAYOUTPARAMS_FILL_PARENT)
{
viewCurrentWidth = viewStandardWidth;
} else
{
viewCurrentWidth = viewStandardWidth * WIDTH_RATIO;
}
// If the width of the reference is wrap_content or fill_parent, use the original value, otherwise calculate the wild-paired value
if (viewStandardHeight == LAYOUTPARAMS_WARP_CONTENT
|| viewStandardHeight == LAYOUTPARAMS_FILL_PARENT)
{
viewCurrentHeight = viewStandardHeight;
} else
{
viewCurrentHeight = viewStandardHeight * HEIGHT_RATIO;
}
}
/**
* Set the layout type of this View parent class
*/
private void setLayoutByParentLayoutType()
{
if (parentLayoutType == LAYOUT_TYPE_RELATiVELAYOUT)
{
params = new (
(int) viewCurrentWidth, (int) viewCurrentHeight);
((int) viewCurrentMarginLeft,
(int) viewCurrentMarginTop, 0, 0);
(params);
} else if (parentLayoutType == LAYOUT_TYPE_LINEARLAYOUT)
{
params = new (
(int) viewCurrentWidth, (int) viewCurrentHeight);
((int) viewCurrentMarginLeft,
(int) viewCurrentMarginTop, 0, 0);
(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;
}
}