一直以来android适配非常头疼,今天给大家分享一个android适配的工具类,我用这个工具类很久了,目前还没发现什么问题。
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.widget.TextView;
/**
* Created by fySpring
* Date : 2019/4/11
* To do :自适应工具类
*/
public class AutoUtils {
private static int displayWidth;
private static int displayHeight;
private static int designWidth;
private static int designHeight;
private static double textPixelsRate;
public static void setSize(Activity act, boolean hasStatusBar, int designWidth, int designHeight) {
if (act == null || designWidth < 1 || designHeight < 1) return;
Display display = act.getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
if (hasStatusBar) {
height -= getStatusBarHeight(act);
}
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;
}
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 (view instanceof 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 (view instanceof 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;
}
}
通常UI给你的图会有一个标准的尺寸的,我的一般是按1280*800来,添加下面的代码来设置标准尺寸:(我通常加到SplashActivity中,因为这段代码只需写一次就可以了)
AutoUtils.setSize(this, true, 1280, 800);//参数二 表示设计尺寸的宽高是否包含状态栏
下面这一段代码就要在BaseActivity、BaseFragment、以及你的自定义View中添加:
AutoUtils.auto(this);
接下来也是最关键的,在android中设置宽高的单位 为 dp,字体的单位为 sp,在这里 我们把单位全部换成 px! ,是的,你没有看错 是 px ! px ! px ! 所以在标注的时候,让UI 把单位标注为 px 。
如果你哪里的控件大小不对,那一定是你的 单位忘了设置,或者忘了加上 AutoUtils.auto(this);
好了,就是这些。使用中有问题的话可以私信我。