一、概述
Android做屏幕适配有多种方式,比如多套切图、多套dimens参数、百分比布局、动态获取屏幕高宽动态设置控件大小、使用鸿洋大神的antolayout库等方案。本文主要介绍通过AutoUtils来做屏幕适配。
二、优点
AutoUtils优点在于直接通过UI标注的px单位直接设置到布局文件当中,再也不需要去算px和dp的转换。
三、使用步骤
1.创建工具类antoUtils
文章末尾附带源码。
2.初始化UI设计图尺寸
在application的onCreate方法中初始化UI设计稿的大小,比如本文为1920*1080
3.布局文件
布局文件中按着UI设计图直接引用px单位设置高宽
其中imageView需要设置属性android:scaleType="fitXY"且高宽设置为图片的大小(右键属性宽*高),本地图片路径存放在drawable-nodpi目录下。
4.activity fragment的onCreat初始化
5 其他
如果是listview,recycleView 需要在adapter 初始化view中适配。
完成以上步骤可完成屏幕适配,不管分辨率多大的手机都会显示一样的效果。
源码:
package com.Test.utils;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
/**
* Android屏幕自动适配工具类
*/
public class AutoUtils {
private static int displayWidth;
private static int displayHeight;
private static int designWidth;
private static int designHeight;
private static boolean inited;
public static double textPixelsRate;
public static void setSize(Context context, boolean hasStatusBar, int designWidth, int designHeight) {
if (context ==null || designWidth <1 || designHeight <1)return;
WindowManager mWManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = mWManager.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
if (hasStatusBar) {
height -=getStatusBarHeight(context);
}
AutoUtils.displayWidth = width;
AutoUtils.displayHeight = height;
AutoUtils.designWidth = designWidth;
AutoUtils.designHeight = designHeight;
double displayDiagonal = Math.sqrt(Math.pow(AutoUtils.displayWidth, 2) + Math.pow(AutoUtils.displayHeight, 2));
double designDiagonal = Math.sqrt(Math.pow(AutoUtils.designWidth, 2) + Math.pow(AutoUtils.designHeight, 2));
AutoUtils.textPixelsRate = displayDiagonal / designDiagonal;
inited =true;
}
public static boolean isInited() {
return inited;
}
public static int getStatusBarHeight(Context context) {
int result =0;
try {
int resourceId = context.getResources().getIdentifier(
"status_bar_height", "dimen", "android");
if (resourceId >0) {
result = context.getResources().getDimensionPixelSize(
resourceId);
}
}catch (Resources.NotFoundException e) {
e.printStackTrace();
}
return result;
}
public static void auto(Activity act) {
if (act ==null ||displayWidth <1 ||displayHeight <1)return;
View view = act.getWindow().getDecorView();
auto(view);
}
public static void auto(View view) {
if (view ==null ||displayWidth <1 ||displayHeight <1)return;
AutoUtils.autoTextSize(view);
AutoUtils.autoSize(view);
AutoUtils.autoPadding(view);
AutoUtils.autoMargin(view);
if (viewinstanceof ViewGroup) {
auto((ViewGroup) view);
}
}
private static void auto(ViewGroup viewGroup) {
int count = viewGroup.getChildCount();
for (int i =0; i < count; i++) {
View child = viewGroup.getChildAt(i);
if (child !=null) {
auto(child);
}
}
}
public static void autoMargin(View view) {
if (!(view.getLayoutParams()instanceof ViewGroup.MarginLayoutParams))
return;
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
if (lp ==null)return;
lp.leftMargin =getDisplayWidthValue(lp.leftMargin);
lp.topMargin =getDisplayHeightValue(lp.topMargin);
lp.rightMargin =getDisplayWidthValue(lp.rightMargin);
lp.bottomMargin =getDisplayHeightValue(lp.bottomMargin);
}
public static void autoPadding(View view) {
int l = view.getPaddingLeft();
int t = view.getPaddingTop();
int r = view.getPaddingRight();
int b = view.getPaddingBottom();
l =getDisplayWidthValue(l);
t =getDisplayHeightValue(t);
r =getDisplayWidthValue(r);
b =getDisplayHeightValue(b);
view.setPadding(l, t, r, b);
}
public static void autoSize(View view) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp ==null)return;
if (lp.width >0) {
lp.width =getDisplayWidthValue(lp.width);
}
if (lp.height >0) {
lp.height =getDisplayHeightValue(lp.height);
}
}
public static void autoTextSize(View view) {
if (viewinstanceof TextView) {
double designPixels = ((TextView) view).getTextSize();
double displayPixels =textPixelsRate * designPixels;
((TextView) view).setIncludeFontPadding(false);
((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) displayPixels);
}
}
public static int getDisplayWidthValue(int designWidthValue) {
if (designWidthValue <2) {
return designWidthValue;
}
return designWidthValue *displayWidth /designWidth;
}
public static int getDisplayHeightValue(int designHeightValue) {
if (designHeightValue <2) {
return designHeightValue;
}
return designHeightValue *displayHeight /designHeight;
}
}