Andbase框架中有个AbViewUtil,经测试,确实可以实现屏幕适配。
下面是该文件源码:
这里写代码片/* * Copyright (C) 2012 www.amsoft.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.keqiang.borche.widget.andbase.utils;import android.annotation.SuppressLint;import android.app.Activity;import android.content.Context;import android.graphics.Paint;import android.os.Build.VERSION;import android.text.TextPaint;import android.util.DisplayMetrics;import android.util.TypedValue;import android.view.View;import android.view.View.MeasureSpec;import android.view.ViewGroup;import android.view.ViewGroup.MarginLayoutParams;import android.view.ViewParent;import android.widget.AbsListView;import android.widget.GridView;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.TextView;import com.keqiang.borche.widget.andbase.view.AbListViewFooter;import com.keqiang.borche.widget.andbase.view.AbListViewHeader;// TODO: Auto-generated Javadoc/** * 2012 amsoft.cn * 名称:AbViewUtil.java * 描述:View工具类. * * @author 还如一梦中 * @version v1.0 * @date:2013-01-17 下午11:52:13 */public class AbViewUtil { /** * 无效值 */ public static final int INVALID = Integer.MIN_VALUE; /** * 描述:重置AbsListView的高度. item 的最外层布局要用 * RelativeLayout,如果计算的不准,就为RelativeLayout指定一个高度 * * @param absListView * the abs list view * @param lineNumber * 每行几个 ListView一行一个item * @param verticalSpace * the vertical space */ public static void setAbsListViewHeight(AbsListView absListView, int lineNumber, int verticalSpace) { int totalHeight = getAbsListViewHeight(absListView, lineNumber, verticalSpace); ViewGroup.LayoutParams params = absListView.getLayoutParams(); params.height = totalHeight; ((MarginLayoutParams) params).setMargins(0, 0, 0, 0); absListView.setLayoutParams(params); } /** * 描述:获取AbsListView的高度. * * @param absListView the abs list view * @param lineNumber 每行几个 ListView一行一个item * @param verticalSpace the vertical space * @return the abs list view height */ public static int getAbsListViewHeight(AbsListView absListView, int lineNumber, int verticalSpace) { int totalHeight = 0; int w = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); int h = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); absListView.measure(w, h); ListAdapter mListAdapter = absListView.getAdapter(); if (mListAdapter == null) { return totalHeight; } int count = mListAdapter.getCount(); if (absListView instanceof ListView) { for (int i = 0; i < count; i++) { View listItem = mListAdapter.getView(i, null, absListView); listItem.measure(w, h); totalHeight += listItem.getMeasuredHeight(); } if (count == 0) { totalHeight = verticalSpace; } else { totalHeight = totalHeight + (((ListView) absListView).getDividerHeight() * (count - 1)); } } else if (absListView instanceof GridView) { int remain = count % lineNumber; if (remain > 0) { remain = 1; } if (mListAdapter.getCount() == 0) { totalHeight = verticalSpace; } else { View listItem = mListAdapter.getView(0, null, absListView); listItem.measure(w, h); int line = count / lineNumber + remain; totalHeight = line * listItem.getMeasuredHeight() + (line - 1) * verticalSpace; } } return totalHeight; } /** * 测量这个view * 最后通过getMeasuredWidth()获取宽度和高度. * @param view 要测量的view * @return 测量过的view */ public static void measureView(View view) { ViewGroup.LayoutParams p = view.getLayoutParams(); if (p == null) { p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width); int lpHeight = p.height; int childHeightSpec; if (lpHeight > 0) { childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY); } else { childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); } view.measure(childWidthSpec, childHeightSpec); } /** * 获得这个View的宽度 * 测量这个view,最后通过getMeasuredWidth()获取宽度. * @param view 要测量的view * @return 测量过的view的宽度 */ public static int getViewWidth(View view) { measureView(view); return view.getMeasuredWidth(); } /** * 获得这个View的高度 * 测量这个view,最后通过getMeasuredHeight()获取高度. * @param view 要测量的view * @return 测量过的view的高度 */ public static int getViewHeight(View view) { measureView(view); return view.getMeasuredHeight(); } /** * 从父亲布局中移除自己 * @param v */ public static void removeSelfFromParent(View v) { ViewParent parent = v.getParent(); if(parent != null){ if(parent instanceof ViewGroup){ ((ViewGroup)parent).removeView(v); } } } /** * 描述:dip转换为px. * * @param context the context * @param dipValue the dip value * @return px值 */ public static float dip2px(Context context, float dipValue) { DisplayMetrics mDisplayMetrics = AbAppUtil.getDisplayMetrics(context); return applyDimension(TypedValue.COMPLEX_UNIT_DIP,dipValue,mDisplayMetrics); } /** * 描述:px转换为dip. * * @param context the context * @param pxValue the px value * @return dip值 */ public static float px2dip(Context context, float pxValue) { DisplayMetrics mDisplayMetrics = AbAppUtil.getDisplayMetrics(context); return pxValue / mDisplayMetrics.density; } /** * 描述:sp转换为px. * * @param context the context * @param spValue the sp value * @return sp值 */ public static float sp2px(Context context, float spValue) { DisplayMetrics mDisplayMetrics = AbAppUtil.getDisplayMetrics(context); return applyDimension(TypedValue.COMPLEX_UNIT_SP,spValue,mDisplayMetrics); } /** * 描述:px转换为sp. * * @param context the context * @param pxValue the sp value * @return sp值 */ public static float px2sp(Context context, float pxValue) { DisplayMetrics mDisplayMetrics = AbAppUtil.getDisplayMetrics(context); return pxValue / mDisplayMetrics.scaledDensity; } /** * 描述:根据屏幕大小缩放. * * @param context the context * @param value the px value * @return the int */ public static int scaleValue(Context context, float value) { DisplayMetrics mDisplayMetrics = AbAppUtil.getDisplayMetrics(context); //为了兼容尺寸小密度大的情况 int width = mDisplayMetrics.widthPixels; int height = mDisplayMetrics.heightPixels; //解决横屏比例问题 if(width > height){ width = mDisplayMetrics.heightPixels; height = mDisplayMetrics.widthPixels; } if(mDisplayMetrics.scaledDensity >= AbAppConfig.UI_DENSITY){ //密度 if(width > AbAppConfig.UI_WIDTH){ value = value*(1.3f - 1.0f/mDisplayMetrics.scaledDensity); }else if(width < AbAppConfig.UI_WIDTH){ value = value*(1.0f - 1.0f/mDisplayMetrics.scaledDensity); } }else{ //密度小屏幕大:缩小比例 float offset = AbAppConfig.UI_DENSITY-mDisplayMetrics.scaledDensity; if(offset > 0.5f){ value = value * 0.9f; }else{ value = value * 0.95f; } } return scale(mDisplayMetrics.widthPixels,mDisplayMetrics.heightPixels, value); } /** * 描述:根据屏幕大小缩放文本. * * @param context the context * @param value the px value * @return the int */ public static int scaleTextValue(Context context, float value) { return scaleValue(context, value); } /** * 描述:根据屏幕大小缩放. * * @param displayWidth the display width * @param displayHeight the display height * @param pxValue the px value * @return the int */ public static int scale(int displayWidth, int displayHeight, float pxValue) { if(pxValue == 0 ){ return 0; } float scale = 1; try { int width = displayWidth; int height = displayHeight; //解决横屏比例问题 if(width > height){ width = displayHeight; height = displayWidth; } float scaleWidth = (float) width / AbAppConfig.UI_WIDTH; float scaleHeight = (float) height / AbAppConfig.UI_HEIGHT; scale = Math.min(scaleWidth, scaleHeight); } catch (Exception e) { } return Math.round(pxValue * scale + 0.5f); } /** * TypedValue官方源码中的算法,任意单位转换为PX单位 * @param unit TypedValue.COMPLEX_UNIT_DIP * @param value 对应单位的值 * @param metrics 密度 * @return px值 */ public static float applyDimension(int unit, float value, DisplayMetrics metrics){ switch (unit) { case TypedValue.COMPLEX_UNIT_PX: return value; case TypedValue.COMPLEX_UNIT_DIP: return value * metrics.density; case TypedValue.COMPLEX_UNIT_SP: return value * metrics.scaledDensity; case TypedValue.COMPLEX_UNIT_PT: return value * metrics.xdpi * (1.0f/72); case TypedValue.COMPLEX_UNIT_IN: return value * metrics.xdpi; case TypedValue.COMPLEX_UNIT_MM: return value * metrics.xdpi * (1.0f/25.4f); } return 0; } /** * * 描述:View树递归调用做适配. * AbAppConfig.uiWidth = 1080; * AbAppConfig.uiHeight = 700; * scaleContentView((RelativeLayout)findViewById(R.id.rootLayout)); * 要求布局中的单位都用px并且和美工的设计图尺寸一致,包括所有宽高,Padding,Margin,文字大小 * @param contentView */ public static void scaleContentView(ViewGroup contentView){ AbViewUtil.scaleView(contentView); if(contentView.getChildCount()>0){ for(int i=0;i=16){ //最大最小宽高 int minWidth = scaleValue(view.getContext(),view.getMinimumWidth()); int minHeight = scaleValue(view.getContext(),view.getMinimumHeight()); view.setMinimumWidth(minWidth); view.setMinimumHeight(minHeight); } } /** * * 描述:是否需要Scale. * @param view * @return */ public static boolean isNeedScale(View view){ if (view instanceof AbListViewHeader){ return false; } if (view instanceof AbListViewFooter){ return false; } return true; } /** * 缩放文字大小 * @param textView button * @param size sp值 * @return */ public static void setSPTextSize(TextView textView,float size) { float scaledSize = scaleTextValue(textView.getContext(),size); textView.setTextSize(scaledSize); } /** * 缩放文字大小,这样设置的好处是文字的大小不和密度有关, * 能够使文字大小在不同的屏幕上显示比例正确 * @param textView button * @param sizePixels px值 * @return */ public static void setTextSize(TextView textView,float sizePixels) { float scaledSize = scaleTextValue(textView.getContext(),sizePixels); textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,scaledSize); } /** * 缩放文字大小 * @param context * @param textPaint * @param sizePixels px值 * @return */ public static void setTextSize(Context context,TextPaint textPaint,float sizePixels) { float scaledSize = scaleTextValue(context,sizePixels); textPaint.setTextSize(scaledSize); } /** * 缩放文字大小 * @param context * @param paint * @param sizePixels px值 * @return */ public static void setTextSize(Context context,Paint paint,float sizePixels) { float scaledSize = scaleTextValue(context,sizePixels); paint.setTextSize(scaledSize); } /** * 设置View的PX尺寸 * @param view 如果是代码new出来的View,需要设置一个适合的LayoutParams * @param widthPixels * @param heightPixels */ public static void setViewSize(View view,int widthPixels, int heightPixels){ int scaledWidth = scaleValue(view.getContext(), widthPixels); int scaledHeight = scaleValue(view.getContext(), heightPixels); ViewGroup.LayoutParams params = view.getLayoutParams(); if(params == null){ AbLogUtil.e(AbViewUtil.class, "setViewSize出错,如果是代码new出来的View,需要设置一个适合的LayoutParams"); return; } if (widthPixels != INVALID){ params.width = scaledWidth; } if (heightPixels != INVALID && heightPixels!=1){ params.height = scaledHeight; } view.setLayoutParams(params); } /** * 设置PX padding. * * @param view the view * @param left the left padding in pixels * @param top the top padding in pixels * @param right the right padding in pixels * @param bottom the bottom padding in pixels */ public static void setPadding(View view, int left, int top, int right, int bottom) { int scaledLeft = scaleValue(view.getContext(), left); int scaledTop = scaleValue(view.getContext(), top); int scaledRight = scaleValue(view.getContext(), right); int scaledBottom = scaleValue(view.getContext(), bottom); view.setPadding(scaledLeft, scaledTop, scaledRight, scaledBottom); } /** * setting PX margin. * * @param view the view * @param left the left margin in pixels * @param top the top margin in pixels * @param right the right margin in pixels * @param bottom the bottom margin in pixels */ public static void setMargin(View view, int left, int top, int right, int bottom) { int scaledLeft = scaleValue(view.getContext(), left); int scaledTop = scaleValue(view.getContext(), top); int scaledRight = scaleValue(view.getContext(), right); int scaledBottom = scaleValue(view.getContext(), bottom); if(view.getLayoutParams() instanceof MarginLayoutParams){ MarginLayoutParams mMarginLayoutParams = (MarginLayoutParams) view .getLayoutParams(); if (mMarginLayoutParams != null){ if (left != INVALID) { mMarginLayoutParams.leftMargin = scaledLeft; } if (right != INVALID) { mMarginLayoutParams.rightMargin = scaledRight; } if (top != INVALID) { mMarginLayoutParams.topMargin = scaledTop; } if (bottom != INVALID) { mMarginLayoutParams.bottomMargin = scaledBottom; } view.setLayoutParams(mMarginLayoutParams); } } } }
下面简单介绍其用法。
1)首先,自定义一个Application子类。
2)将自定义Application子类添加到Manifest文件中。
3)在Application子类的onCreate()方法中添加美工尺寸。
//美工效果图分辨率,用于适配 AbAppConfig.UI_WIDTH = 640; AbAppConfig.UI_HEIGHT = 1136;4)然后,在布局文件的最外层容器中添加一个id。
android:id="@+id/act_main_root_layout"5)如果是Activity,那么在Activity的onCreate()方法中添加如下内容。
setContentView(R.layout.activity_forget_pwd); //根据屏幕分辨率和屏幕密度自定缩放 AbViewUtil.scaleContentView((LinearLayout) findViewById(R.id.act_main_root_layout));如果是Fragment,那么在Fragment的onCreateView()方法中添加如下内容。
inflater = LayoutInflater.from(getActivity()); rootView = inflater.inflate(R.layout.fragment_function, container, false); AbViewUtil.scaleContentView((LinearLayout) rootView.findViewById(R.id.fgm_main_root_layout));6)注意,布局文件中的宽高和文字大小都统一使用美工提供的分辨率的值,单位统一使用px,至此。一个Activity的屏幕适配就完成了。