-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathViewUtil.java
More file actions
275 lines (249 loc) · 10.2 KB
/
ViewUtil.java
File metadata and controls
275 lines (249 loc) · 10.2 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
package common.base.utils;
import android.app.Activity;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.lang.reflect.Field;
import common.base.R;
/**
* User: fee(1176610771@qq.com)
* Date: 2016-06-14
* Time: 15:19
* DESC:
*/
public class ViewUtil{
public static <T> T findViewInContainer(ViewGroup containerView, int toFindViewResId) {
if (toFindViewResId < 1 || containerView == null) {
return null;
}
return (T) containerView.findViewById(toFindViewResId);
}
public static <T> T findAViewById(Activity curActivity, int toFindViewResId) {
if (curActivity == null || toFindViewResId < 1) {
return null;
}
return (T) curActivity.findViewById(toFindViewResId);
}
/**
* Convert Dp to Pixel
*/
public static int dpToPx(float dp, Resources resources){
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
return (int) px;
}
public static int getRelativeTop(View myView) {
// if (myView.getParent() == myView.getRootView())
if(myView.getId() == android.R.id.content)
return myView.getTop();
else
return myView.getTop() + getRelativeTop((View) myView.getParent());
}
public static int getRelativeLeft(View myView) {
// if (myView.getParent() == myView.getRootView())
if(myView.getId() == android.R.id.content)
return myView.getLeft();
else
return myView.getLeft() + getRelativeLeft((View) myView.getParent());
}
/**
* 改变一个View背景的资源的颜色
* @param view
* @param argbColor
*/
public static void changeViewBgDrawableColor(View view, int argbColor) {
if (view != null) {
GradientDrawable gradientDrawable;
try {
gradientDrawable = (GradientDrawable) view.getBackground();
gradientDrawable.setColor(argbColor);
} catch (Exception e) {
}
}
}
/**
* 给图像着色
* @param originDrawable 源图像,可以是View的背景图
* @param colors
* @return 着色后的Drawable
* 参考:http://www.race604.com/tint-drawable/
*/
public static Drawable tintDrawable(Drawable originDrawable, ColorStateList colors) {
final Drawable wrappedDrawable = DrawableCompat.wrap(originDrawable);
if (wrappedDrawable != null) {//avoid nullpointer exception
DrawableCompat.setTintList(wrappedDrawable, colors);
}
return wrappedDrawable;
}
/**
* 用来改变ImageView的饱和度的
* @param target
* @param saturationValue
*/
public static void changeImageViewSaturation(ImageView target, int saturationValue) {
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(saturationValue);
ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
target.setColorFilter(colorMatrixColorFilter);
}
/**
* 修改一张源位图的饱和度来生成一个新位图
* @param sourceBitmap 源位图
* @param expectantSaturation 期望的饱和度值
* @return 更改饱和度后的新位图
*/
public static Bitmap changeSrcBitmapSaturation(Bitmap sourceBitmap,int expectantSaturation) {
int width = sourceBitmap.getWidth();
int height = sourceBitmap.getHeight();
Bitmap compoundResultBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(compoundResultBitmap);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(expectantSaturation);
ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(colorMatrixFilter);
canvas.drawBitmap(sourceBitmap, 0, 0, paint);
return compoundResultBitmap;
}
/**
* 给输入框控件View的光标着色
* Android 3.1 (API 12) 开始就支持了 textCursorDrawable,
* 也就是可以自定义光标的 Drawable。遗憾的是,这个方法只能在 xml 中使用
* 所以通过反射来设置
* @param curEditText
* @param expectedCursorColor 所期望的光标颜色
* 参考:http://www.race604.com/tint-drawable/
*/
public static void tintCursorDrawable(EditText curEditText, int expectedCursorColor) {
Field fCursorDrawableRes = ReflectUtil.getFidelOfClass(TextView.class, "mCursorDrawableRes");
if (fCursorDrawableRes == null) {
return;
}
try {
//拿到TextView类中所定义的mCursorDrawableRes 当前的值
int mCursorDrawableRes = fCursorDrawableRes.getInt(curEditText);
if (mCursorDrawableRes <= 0) {
return;
}
//拿到cursorDrawable 的资源图像对象
Drawable cursorDrawable = curEditText.getContext().getResources().getDrawable(mCursorDrawableRes);
if (cursorDrawable == null) {
return;
}
//拿到TextView中Editor属性对象,Editor类被Android系统隐藏了@hide
Field fieldMEditor = ReflectUtil.getFidelOfClass(TextView.class, "mEditor");
Object mEditor = fieldMEditor.get(curEditText);
//从Editro类中获取mCursorDrawable属性
Field mCursorDrawableField = ReflectUtil.getFieldOfObject(mEditor, "mCursorDrawable");
Drawable tintedCursorDrawable = tintDrawable(cursorDrawable, ColorStateList.valueOf(expectedCursorColor));
Drawable[] newCursorDrawables = new Drawable[]{tintedCursorDrawable, tintedCursorDrawable};
//更改mEditor中mCursorDrawable属性对象的值
mCursorDrawableField.set(curEditText, newCursorDrawables);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 校验文本输入框当前是否输入的是空格
* @param curEditText
* @return true 当前输入的是空格; false:不是
*/
public static boolean checkEditTextInputedNull(EditText curEditText) {
boolean curInputedNull = false;
if (curEditText == null) {
return curInputedNull;
}
String textAfterInputed = curEditText.getText().toString();
int newLen = textAfterInputed.length();
if (newLen > 0) {
char[] chars = textAfterInputed.toCharArray();
curInputedNull = chars[newLen - 1] == ' ';
if (curInputedNull) {
if (newLen == 1) {
curEditText.setText("");//替换成空字符串,此空字符串的Len = 0;
}
else{//输入后的文本不只有1个字符的情况,把空字符给去除掉
String trimedStr = textAfterInputed.trim();
curEditText.setText(trimedStr);
curEditText.setSelection(trimedStr.length());//并将光标移到文本末尾
}
}
}
return curInputedNull;
}
/**
* 阻止快速点击视图
* @param clickedView 被点击的View控件
* @param needGapMillTimes 两次点击需要间隔的毫秒时间
* @return true:需要阻止此次点击;false:不需要,即为有效点击
*/
public static boolean preventFastClickView(View clickedView, long needGapMillTimes) {
int tagKey = R.id.view_double_click_tag_id;
String lastClickTimeStr = (String) clickedView.getTag(tagKey);
boolean need2PreVent = false;
long lastClickMillTime = 0;
if (!Util.isEmpty(lastClickTimeStr)) {
try {
lastClickMillTime = Long.parseLong(lastClickTimeStr);
} catch (Exception e) {
}
}
long curClickMillTime = System.currentTimeMillis();
if ((curClickMillTime - lastClickMillTime) < needGapMillTimes) {
need2PreVent = true;
}
clickedView.setTag(tagKey, curClickMillTime + "");
return need2PreVent;
}
/**
* 依据ListView的Chilc Views 计算所需要绘制的高度
* @param listView
*/
public static void resolveListViewWholeH(ListView listView) {
if (listView == null || listView.getAdapter() == null) {
return;
}
ListAdapter listAdapter = listView.getAdapter();
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// params.height += 5;// if without this statement,the listview will be
// a
// little short
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params);
}
/**
* 获取一个Activity中所在的window中的用来填充该activity布局的容器视图
* @param curActivity
* @return 可能通过
*/
public static FrameLayout getContentContainerView(Activity curActivity) {
FrameLayout contentLayout = null;
if (curActivity != null) {
return (FrameLayout) curActivity.getWindow().getDecorView().findViewById(android.R.id.content);
}
return contentLayout;
}
}