Android结合AutoLayout屏幕适配解决TextView自适应

package com.zwj.demo_popupwindow;

import android.content.Context;

import android.util.Log;

import android.util.TypedValue;

import android.view.WindowManager;

import android.widget.TextView;

/**

*

*/

public class AutoSize {

/**

* 1,使用前必须确定设计模板的尺寸

* 2,在Activity的onCreate方法当中调用init()进行初始化

* 3,为TextView设置大小的时候,调用autoTextView()方法进行

*/

private static int design_width = 720;

private static int design_height = 1280;

private static double width_per;

private static double height_per;

public static void init(Context mContext) {

WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);

width_per = (double) wm.getDefaultDisplay().getWidth() / design_width;

height_per = (double) wm.getDefaultDisplay().getHeight() / design_height;

if (width_per == 0 || height_per == 0) {

try {

throw new Throwable("没有获取到屏幕的宽高");

} catch (Throwable throwable) {

throwable.printStackTrace();

}

} else {

Log.i("AutoSize:", "屏幕宽的比率是:" + width_per + "屏幕高的比率是:" + height_per);

}

}

/**

* 适配宽

*

* @param size

* @return

*/

public static int autoWeight(int size) {

int autoSize = (int) (width_per * size);

Log.i("AutoSize:适配后的width为:", autoSize + "");

return autoSize;

}

/**

* 设配高

*

* @param size

* @return

*/

public static int autoHeight(int size) {

int autoSize = (int) (height_per * size);

Log.i("AutoSize:适配后的height为:", autoSize + "");

return autoSize;

}

/**

* 适配TextView字体

*

* @param view

* @param size

* @return

*/

public static void autoTextView(TextView view, float size) {

view.setTextSize(TypedValue.COMPLEX_UNIT_PX, autoSize(size));

}

private static float autoSize(float size) {

float autoSize = (float) (width_per * size);

Log.i("AutoSize:适配后的height为:", autoSize + "");

return autoSize;

}

}

你可能感兴趣的:(Android结合AutoLayout屏幕适配解决TextView自适应)