Android之屏幕适配
把之前的工作辞了,在杭州找了一份工作.大概找了一周的样子.之前又忙着把项目和之后接手的人交接.所以好长一段时间没有写了.找了个完全我自己主导项目的工作,虽然之前都是独立开发的,但都是老大说我去实现.现在这次是对我工作能力的一种很大的提升.
今天来和大家讲一下Android中最恶心的问题之一的----------屏幕适配
目前Android手机的屏幕尺寸种类大概有1000多种
大概就是这个样子.而IOS就那么几款手机 4(差不多快没人用了),6p,7等
所以Android要做屏幕适配.屏幕适配有六种方法.分别是 尺寸适配 权重适配 代码适配 布局适配 图片适配 百分比适配
我用我觉得最简单的方式加上代码来和大家说
1.尺寸适配 :其实尺寸适配是我们用的最常见的 也就是控件用dp 文字用sp (不清楚sp,dp的麻烦自己去查一下)
2.权重适配 : 就是LinearLayout中的weight权重 ,因为权重是控件除去有的尺寸剩下的来分所以我们一般设比例的方法尺寸都设0dp
3.代码适配 : 就是在代码中动态设置控件的大小(自己写的自定义控件)
public class RatioLayout extends FrameLayout{ private int sizeWidth; private int sizeHeight; private float proportion; public static final int RELATIVE_WIDTH = 0;//已知宽度,动态计算高度 public static final int RELATIVE_HEIGHT = 1;//已知高度,动态计算宽度 private int mRelative; // 相对谁来计算值 public RatioLayout(Context context) { this(context,null); } public RatioLayout(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = UIUtils.getContext().obtainStyledAttributes(attrs, R.styleable.RatioLayout); proportion = typedArray.getFloat(R.styleable.RatioLayout_picRatio, 1); mRelative = typedArray.getInt(R.styleable.RatioLayout_relative, RELATIVE_WIDTH); typedArray.recycle(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int count = getChildCount(); int modeWidth = MeasureSpec.getMode(widthMeasureSpec); int modeHeight = MeasureSpec.getMode(heightMeasureSpec); if(modeWidth==MeasureSpec.EXACTLY&&mRelative==RELATIVE_WIDTH){ sizeWidth = MeasureSpec.getSize(widthMeasureSpec); sizeHeight= (int) (sizeWidth/proportion+.5f); setMeasuredDimension(sizeWidth,sizeHeight); measureChild(widthMeasureSpec, heightMeasureSpec, count); }else if(modeHeight==MeasureSpec.EXACTLY&&mRelative==RELATIVE_HEIGHT){ sizeHeight = MeasureSpec.getSize(heightMeasureSpec); sizeWidth= (int) (sizeWidth*proportion+.5f); setMeasuredDimension(sizeWidth,sizeHeight); measureChild(widthMeasureSpec, heightMeasureSpec, count); }else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } private void measureChild(int widthMeasureSpec, int heightMeasureSpec, int count) { for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); int childWidthMeasureSpec; if (lp.width == LayoutParams.MATCH_PARENT) { int width =getMeasuredWidth() - lp.leftMargin - lp.rightMargin; childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); } else { childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,lp.leftMargin + lp.rightMargin,lp.width); } int childHeightMeasureSpec; if (lp.height == LayoutParams.MATCH_PARENT) { int height =getMeasuredHeight() -lp.topMargin - lp.bottomMargin; childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); } else { childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, lp.topMargin + lp.bottomMargin,lp.height); } child.measure(childWidthMeasureSpec, childHeightMeasureSpec); } } } }
4.布局适配 : 其实就是根据不同的版本号,和屏幕大小在res目录下写多个xml文件.系统会找到对应的文件
5.图片适配 : 其实就是根据不同的版本号,和屏幕大小在res目录下放张图片(我表示开发三年都没用过,因为图片是我们Android最占内存的,本来后期还要处理APK瘦身,放多套图片就更加不好搞了)
6.百分比适配 : 我原来是不知道百分比适配的.有一次和前端的哥们出去吃饭.我和他聊到屏幕适配.我就问他你们网页是怎么适配的啊.他说百分比适配啊.我去查了下资料后发现Android中也有这么个东西.Android中有原生的API.鸿洋大神也写了个三方库.Android原生的API我就不说了大家想了解的自己可以去网上搜.主要是原生的API是在编译的时候动态生成对应控件的比例尺寸,所以相对与性能有一定影响.
三方的库叫AutoLayout提前在res目录下生成比例.对于华为手机有一个天坑.现在华为手机的市场使用量还是很多的,所以我们必须做适配.
华为手机下面有个虚拟按键三方库生成的时候是按整个屏幕来生成的.而且目前就华为有虚拟按键.如果运行判断在生成三方库就没有意义了(白开心一场,这个问题我找了一段时间才发现是虚拟按键的问题)
本库的地址:https://github.com/hongyangAndroid/AndroidAutoLayout