Android设备繁多,屏幕标准不一,更有让人头疼的定制rom,开发中有时候真的很蛋疼,这点上就没IOS好,下面我分享下我在项目中是如何适配的:
我们UI只切了一套图,IOS、Android组共用这一套,是按6plus切的,没办法,项目多,而UI就一个,不会像书上说的那样几个drawable目录放图,否则做UI的妹子要累死,具体方法很简单,只需要这样一个类就可以:
package com.....config;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
/**
* @author wfy
*
*/
public class SceenMannage {
private static int STANDARD_WIDTH = 720;// 标准宽的像素
private static int STANDARD_HEIGHT = 1280;// 标准高的像素
private float WIDTH_RATE;
private float HEIGHT_RATE;
private float ZOOM_RATE;
private float CHANGE_RATE; // dip转px转换比例
/**字体缩放比例 */
private float fontScale;
/** 屏幕密度 */
public float DENSITY;
/** 每英寸像素数 */
public int DENSITYDPI;
/**屏幕高度像素*/
public int height;
public SceenMannage(Context context) {
init(context);
}
private void init(Context context) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int large = (metrics.widthPixels > metrics.heightPixels) ? metrics.widthPixels
: metrics.heightPixels;
int small = (metrics.widthPixels < metrics.heightPixels) ? metrics.widthPixels
: metrics.heightPixels;
fontScale =metrics.scaledDensity;
DENSITYDPI = metrics.densityDpi;
DENSITY = metrics.density;
WIDTH_RATE = small * 1f / STANDARD_WIDTH;
HEIGHT_RATE = large * 1f / STANDARD_HEIGHT;
ZOOM_RATE = (WIDTH_RATE < HEIGHT_RATE) ? WIDTH_RATE : HEIGHT_RATE;
CHANGE_RATE= DENSITYDPI * 1f / 160;
}
/**
* 关于相对布局适配 不用适配传值为“0”
*
* @param view
* 适配的对象
* @param width
* 宽 dip
* @param hight
* 高 dip
* @param topMargin
* 顶距离 dip
* @param leftMargin
* 左距离 dip
* @param rightMargin
* 右距离 dip
* @param buttomMargin
* 底距离 dip
*/
public void LinearLayoutParams(View view, float width, float hight,
float topMargin, float leftMargin, float rightMargin,
float buttomMargin) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view
.getLayoutParams();
if (width != 0) {
params.width = (int) (width * ZOOM_RATE);
}
if (hight != 0) {
params.height = (int) (hight * ZOOM_RATE);
}
if (topMargin != 0) {
params.topMargin = (int) (topMargin * HEIGHT_RATE);
}
if (buttomMargin != 0) {
params.bottomMargin = (int) (buttomMargin * HEIGHT_RATE);
}
if (leftMargin != 0) {
params.leftMargin = (int) (leftMargin * WIDTH_RATE);
}
if (rightMargin != 0) {
params.rightMargin = (int) (rightMargin * WIDTH_RATE);
}
}
/**
* 关于相对布局适配 不用适配传值为“0”
*
* @param view
* 适配的对象
* @param width
* 宽 dip
* @param hight
* 高 dip
* @param topMargin
* 顶距离 dip
* @param leftMargin
* 左距离 dip
* @param rightMargin
* 右距离 dip
* @param buttomMargin
* 底距离 dip
*/
public void RelativeLayoutParams(View view, float wight, float hight,
float topMargin, float leftMargin, float rightMargin,
float buttomMargin) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view
.getLayoutParams();
if (wight != 0) {
params.width = (int) (wight * ZOOM_RATE);
}
if (hight != 0) {
params.height = (int) (hight * ZOOM_RATE);
}
if (topMargin != 0) {
params.topMargin = (int) (topMargin * HEIGHT_RATE);
}
if (buttomMargin != 0) {
params.bottomMargin = (int) (buttomMargin * HEIGHT_RATE);
}
if (leftMargin != 0) {
params.leftMargin = (int) (leftMargin * WIDTH_RATE);
}
if (rightMargin != 0) {
params.rightMargin = (int) (rightMargin * WIDTH_RATE);
}
}
/**
* 关于相对布局适配 不用适配传值为“0”
*
* @param view
* 适配的对象
* @param width
* 宽 dip
* @param hight
* 高 dip
* @param topMargin
* 顶距离 dip
* @param leftMargin
* 左距离 dip
* @param rightMargin
* 右距离 dip
* @param buttomMargin
* 底距离 dip
*/
public void FragmentLayoutParams(View view, float wight, float hight,
float topMargin, float leftMargin, float rightMargin,
float buttomMargin) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view
.getLayoutParams();
if (wight != 0) {
params.width = (int) (wight * ZOOM_RATE);
}
if (hight != 0) {
params.height = (int) (hight * ZOOM_RATE);
}
if (topMargin != 0) {
params.topMargin = (int) (topMargin * HEIGHT_RATE);
}
if (buttomMargin != 0) {
params.bottomMargin = (int) (buttomMargin * HEIGHT_RATE);
}
if (leftMargin != 0) {
params.leftMargin = (int) (leftMargin * WIDTH_RATE);
}
if (rightMargin != 0) {
params.rightMargin = (int) (rightMargin * WIDTH_RATE);
}
}
/**
* 11.30 新增:返回ZOOM_RATE 无视父布局类型设置宽高
* @param view
* @param wight
* @param hight
*/
public float getRate(){
return ZOOM_RATE;
}
/**
* 水平方向的适配
* @param width px
* @return
*/
public int changeHorizontal (int width) {
return (int) (width * WIDTH_RATE);
}
/**
* 垂直方向的适配
* @param hight
* @return
*/
public int changeVertical(int hight) {
return (int) (hight * HEIGHT_RATE);
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
* @param dpValue
* @return px(像素)
*/
public int dip2px(float dpValue) {
return (int) (dpValue * CHANGE_RATE + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
* @param pxValue
* @return dp
*/
public int px2dip(float pxValue) {
return (int) (pxValue / CHANGE_RATE + 0.5f);
}
/**
* 将px值转换为sp值,保证文字大小不变
* @param pxValue
* @return
*/
public int px2sp(float pxValue) {
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 将sp值转换为px值,保证文字大小不变
* @param spValue
* @return
*/
public int sp2px(float spValue) {
return (int) (spValue * fontScale + 0.5f);
}
/**
* 获取屏幕高的像素
* @param context
* @return
*/
public int getHeight(Context context){
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
height=metrics.heightPixels;
return height;
}
/**
* 11.27 新增
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
注释写的还算详细,没什么说的,就是对Android中单位换算的方法做了再次封装,便于使用。