-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathAbViewUtil.java
More file actions
401 lines (364 loc) · 12.3 KB
/
AbViewUtil.java
File metadata and controls
401 lines (364 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* 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.ab.util;
import android.content.Context;
import android.graphics.Paint;
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.ab.global.AbAppConfig;
// TODO: Auto-generated Javadoc
/**
* © 2012 amsoft.cn
* 名称:AbViewUtil.java
* 描述:View工具类.
*/
public class AbViewUtil {
/**
* 无效值
*/
public static final int INVALID = Integer.MIN_VALUE;
/**
* 测量这个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 spValue 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 pxValue the px value
* @return the int
*/
public static int scale(Context context, float value) {
DisplayMetrics mDisplayMetrics = AbAppUtil.getDisplayMetrics(context);
return scale(mDisplayMetrics.widthPixels,
mDisplayMetrics.heightPixels, 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 {
float scaleWidth = (float) displayWidth / AbAppConfig.UI_WIDTH;
float scaleHeight = (float) displayHeight / 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<contentView.getChildCount();i++){
if(contentView.getChildAt(i) instanceof ViewGroup){
scaleContentView((ViewGroup)(contentView.getChildAt(i)));
}else{
scaleView(contentView.getChildAt(i));
}
}
}
}
/**
* 按比例缩放View,以布局中的尺寸为基准
* @param view
*/
public static void scaleView(View view){
if (view instanceof TextView){
TextView textView = (TextView) view;
setTextSize(textView,textView.getTextSize());
}
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) view.getLayoutParams();
if (null != params){
int width = INVALID;
int height = INVALID;
if (params.width != ViewGroup.LayoutParams.WRAP_CONTENT
&& params.width != ViewGroup.LayoutParams.MATCH_PARENT){
width = params.width;
}
if (params.height != ViewGroup.LayoutParams.WRAP_CONTENT
&& params.height != ViewGroup.LayoutParams.MATCH_PARENT){
height = params.height;
}
//size
setViewSize(view,width,height);
// Padding
setPadding(view,view.getPaddingLeft(),view.getPaddingTop(),view.getPaddingRight(),view.getPaddingBottom());
}
// Margin
if(view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams){
ViewGroup.MarginLayoutParams mMarginLayoutParams = (ViewGroup.MarginLayoutParams) view
.getLayoutParams();
if (mMarginLayoutParams != null){
setMargin(view,mMarginLayoutParams.leftMargin,mMarginLayoutParams.topMargin,mMarginLayoutParams.rightMargin,mMarginLayoutParams.bottomMargin);
}
}
}
/**
* 缩放文字大小
* @param textView button
* @param size sp值
* @return
*/
public static void setSPTextSize(TextView textView,float size) {
float scaledSize = scale(textView.getContext(),size);
textView.setTextSize(scaledSize);
}
/**
* 缩放文字大小,这样设置的好处是文字的大小不和密度有关,
* 能够使文字大小在不同的屏幕上显示比例正确
* @param textView button
* @param sizePixels px值
* @return
*/
public static void setTextSize(TextView textView,float sizePixels) {
float scaledSize = scale(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 = scale(context,sizePixels);
textPaint.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 = scale(view.getContext(), widthPixels);
int scaledHeight = scale(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){
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 = scale(view.getContext(), left);
int scaledTop = scale(view.getContext(), top);
int scaledRight = scale(view.getContext(), right);
int scaledBottom = scale(view.getContext(), bottom);
view.setPadding(scaledLeft, scaledTop, scaledRight, scaledBottom);
}
/**
* 设置 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 = scale(view.getContext(), left);
int scaledTop = scale(view.getContext(), top);
int scaledRight = scale(view.getContext(), right);
int scaledBottom = scale(view.getContext(), bottom);
if(view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams){
ViewGroup.MarginLayoutParams mMarginLayoutParams = (ViewGroup.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);
}
}
}
}