android动态屏幕适配(不需要多套图,多布局)

1.工具类:

public class SupportDisplay {

	/**
	 * 基准屏横
	 */
	private static final float BASIC_SCREEN_WIDTH = 720f;
	/**
	 * 基准屏竖
	 */
	private static final float BASIC_SCREEN_HEIGHT = 1280f;

	/**
	 * 基准屏dpi
	 */
	private static final float BASIC_DENSITY = 2.0f;

	/**
	 * 误差率
	 */
	private static final float COMPLEMENT_VALUE = 0.5f;

	/**
	 * 手机屏宽
	 */
	private static int mDisplayWidth;
	/**
	 * 手机屏高
	 */
	private static int mDisplayHeight;
	/**
	 * 水平比例
	 */
	private static float mLayoutScale;
	/**
	 * 垂直比例
	 */
	private static float mVerticalScale;

	private static float mDensityScale;

	private static float mDensityMetrics;

	/**
	 * 初始化
	 * 
	 * @param context
	 *            context
	 */
	public static void initLayoutSetParams(Context context) {
		if (context == null) {
			return;
		}
		final WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		final Display disp = wm.getDefaultDisplay();
		final Point point = new Point();
		getSize(disp, point);
		mDisplayWidth = point.x;
		mDisplayHeight = point.y;
		mLayoutScale = mDisplayWidth / BASIC_SCREEN_WIDTH;
		mVerticalScale = mDisplayWidth / BASIC_SCREEN_HEIGHT;
		mDensityMetrics = context.getResources().getDisplayMetrics().density;
		mDensityScale = BASIC_DENSITY / mDensityMetrics;
	}

	public static float getLayoutScale() {
		return mLayoutScale;
	}

	public static int getmDisplayWidth() {
		return mDisplayWidth;
	}


	public static int getmDisplayHeight() {
		return mDisplayHeight;
	}


	public static float getmVerticalScale() {
		return mVerticalScale;
	}

	/**
	 * 
	 * @param display
	 *            System display.
	 * @param outSize
	 *            Target size.
	 */
	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	private static void getSize(Display display, Point outSize) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			display.getSize(outSize);
		} else {
			outSize.x = display.getWidth();
			outSize.y = display.getHeight();
		}
	}

	/**
	 * 
	 * @param sizedp
	 * @return
	 */
	private static int calculateActualControlerSize(float sizedp) {
		return (int) ((sizedp / mDensityMetrics) * mLayoutScale + COMPLEMENT_VALUE);
	}

	/**
	 * 对文字大小计算
	 * 
	 * @param textView
	 *            TextView
	 * @param textSizedp
	 */
	private static void resetContrlerTextSize(TextView textView,
			float textSizedp) {
		textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizedp
				* mLayoutScale * mDensityScale);

	}

	/**
	 * 对Button计算
	 * 
	 * @param button
	 *            Button
	 * @param textSizedp
	 */
	public static void resetContrlerTextSize(Button button, float textSizedp) {
		resetContrlerTextSize((TextView) button, textSizedp);

	}

	/**
	 * 重新计算所有Layout
	 * 
	 * @param rootView
	 */
	public static void resetAllChildViewParam(ViewGroup rootView) {
		int childCount = rootView.getChildCount();
		for (int i = 0; i < childCount; i++) {
			View childView = rootView.getChildAt(i);
			if (childView instanceof ViewGroup) {
				setChildViewParam((ViewGroup) childView);
			} else {
				setViewParam(childView);
			}
		}
	}

	/**
	 * 重新计算所有Layout
	 * 
	 * @param parentView
	 */
	private static void setChildViewParam(ViewGroup parentView) {
		if (parentView == null) {
			return;
		}
		setViewParam(parentView);
		int childCount = parentView.getChildCount();
		if (childCount == 0) {
			return;
		}
		for (int i = 0; i < childCount; i++) {
			View childView = parentView.getChildAt(i);
			if (childView instanceof ViewGroup) {
				setChildViewParam((ViewGroup) childView);
			} else {
				setViewParam(childView);
			}
		}
	}

	/**
	 * 重新计算所有Layout
	 * 
	 * @param view
	 */
	private static void setViewParam(View view) {
		ViewGroup.LayoutParams lp = view.getLayoutParams();
		int height = lp.height;
		if (height != -1 && height != -2) {
			lp.height = calculateActualControlerSize(height);
		}
		int width = lp.width;
		if (width != -1 && width != -2) {
			lp.width = calculateActualControlerSize(width);
		}

		if (!(lp instanceof RelativeLayout.LayoutParams
				|| lp instanceof LinearLayout.LayoutParams
				|| lp instanceof FrameLayout.LayoutParams || lp instanceof TableLayout.LayoutParams)) {
			return;
		}
		if (lp instanceof RelativeLayout.LayoutParams) {
			RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) view
					.getLayoutParams();
			int leftMargin = param.leftMargin;
			if (leftMargin != -1) {
				param.leftMargin = calculateActualControlerSize(leftMargin);
			}
			int rightMargin = param.rightMargin;
			if (rightMargin != -1) {
				param.rightMargin = calculateActualControlerSize(rightMargin);
			}
			int topMargin = param.topMargin;
			if (topMargin != -1) {
				param.topMargin = calculateActualControlerSize(topMargin);
			}
			int bottomMargin = param.bottomMargin;
			if (bottomMargin != -1) {
				param.bottomMargin = calculateActualControlerSize(bottomMargin);
			}
		} else if (lp instanceof LinearLayout.LayoutParams) {
			LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) view
					.getLayoutParams();
			int leftMargin = param.leftMargin;
			if (leftMargin != -1) {
				param.leftMargin = calculateActualControlerSize(leftMargin);
			}
			int rightMargin = param.rightMargin;
			if (rightMargin != -1) {
				param.rightMargin = calculateActualControlerSize(rightMargin);
			}
			int topMargin = param.topMargin;
			if (topMargin != -1) {
				param.topMargin = calculateActualControlerSize(topMargin);
			}
			int bottomMargin = param.bottomMargin;
			if (bottomMargin != -1) {
				param.bottomMargin = calculateActualControlerSize(bottomMargin);
			}

		} else if (lp instanceof FrameLayout.LayoutParams) {
			FrameLayout.LayoutParams param = (FrameLayout.LayoutParams) view
					.getLayoutParams();
			int leftMargin = param.leftMargin;
			if (leftMargin != -1) {
				param.leftMargin = calculateActualControlerSize(leftMargin);
			}
			int rightMargin = param.rightMargin;
			if (rightMargin != -1) {
				param.rightMargin = calculateActualControlerSize(rightMargin);
			}
			int topMargin = param.topMargin;
			if (topMargin != -1) {
				param.topMargin = calculateActualControlerSize(topMargin);
			}
			int bottomMargin = param.bottomMargin;
			if (bottomMargin != -1) {
				param.bottomMargin = calculateActualControlerSize(bottomMargin);
			}

		} else if (lp instanceof TableLayout.LayoutParams) {
			TableLayout.LayoutParams param = (TableLayout.LayoutParams) view
					.getLayoutParams();
			int leftMargin = param.leftMargin;
			if (leftMargin != -1) {
				param.leftMargin = calculateActualControlerSize(leftMargin);
			}
			int rightMargin = param.rightMargin;
			if (rightMargin != -1) {
				param.rightMargin = calculateActualControlerSize(rightMargin);
			}
			int topMargin = param.topMargin;
			if (topMargin != -1) {
				param.topMargin = calculateActualControlerSize(topMargin);
			}
			int bottomMargin = param.bottomMargin;
			if (bottomMargin != -1) {
				param.bottomMargin = calculateActualControlerSize(bottomMargin);
			}
		}
		int leftPadding = view.getPaddingLeft();
		if (leftPadding != -1) {
			leftPadding = calculateActualControlerSize(leftPadding);
		}
		int rightPadding = view.getPaddingRight();
		if (rightPadding != -1) {
			rightPadding = calculateActualControlerSize(rightPadding);
		}
		int topPadding = view.getPaddingTop();
		if (topPadding != -1) {
			topPadding = calculateActualControlerSize(topPadding);
		}
		int bottomPadding = view.getPaddingBottom();
		if (bottomPadding != -1) {
			bottomPadding = calculateActualControlerSize(bottomPadding);
		}
		view.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
		if (view instanceof EditText || view instanceof TextView) {
			TextView tv = (TextView) view;
			float textSize = tv.getTextSize();
			resetContrlerTextSize(tv, textSize);
		}
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
			if (view instanceof GridView) {
				GridView gridView = (GridView) view;
				int horizontalSpacing = gridView.getHorizontalSpacing();
				int verticalSpacing = gridView.getVerticalSpacing();
				if (horizontalSpacing != -1) {
					gridView.setHorizontalSpacing(calculateActualControlerSize(horizontalSpacing));
				}
				if (verticalSpacing != -1) {
					gridView.setVerticalSpacing(calculateActualControlerSize(verticalSpacing));
				}
			}
		}

	}
}
2.使用需要在BaseActivity或者程序的开始位置进行初始化

SupportDisplay.initLayoutSetParams(this);

3.在初始化其他控件前对布局的最外层进行计算

        root = (ViewGroup) findViewById(R.id.main_root);
        SupportDisplay.resetAllChildViewParam(root);

此方法能解决大部分的属性。如果有一些其他方法。读者可以在工具类中自行添加

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值