Skip to content

Commit 33cf3e1

Browse files
author
371718330@qq.com
committed
修改view工具类
1 parent f9e1063 commit 33cf3e1

File tree

1 file changed

+254
-4
lines changed
  • CommonUtil/src/main/java/com/jingewenku/abrahamcaijin/commonutil

1 file changed

+254
-4
lines changed

CommonUtil/src/main/java/com/jingewenku/abrahamcaijin/commonutil/ViewUtils.java

Lines changed: 254 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
package com.jingewenku.abrahamcaijin.commonutil;
22

3-
import android.view.MotionEvent;
4-
import android.view.View;
5-
import android.view.ViewGroup;
6-
import android.view.ViewParent;
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.content.ContextWrapper;
6+
import android.content.res.Resources;
7+
import android.content.res.TypedArray;
8+
import android.graphics.Bitmap;
9+
import android.graphics.Canvas;
10+
import android.graphics.Matrix;
11+
import android.graphics.Paint;
12+
import android.graphics.drawable.BitmapDrawable;
13+
import android.view.*;
14+
import android.view.View.MeasureSpec;
15+
import android.widget.PopupWindow;
16+
import android.widget.TextView;
717

818
/**
919
* @Description:主要功能:
@@ -47,4 +57,244 @@ public static boolean isTouchInView(MotionEvent ev, View v) {
4757
&& motionY >= vLoc[1] && motionY <= (vLoc[1] + v.getHeight());
4858
}
4959

60+
/**
61+
*
62+
* @param view
63+
* @param isAll
64+
*/
65+
public static void requestLayoutParent(View view, boolean isAll) {
66+
ViewParent parent = view.getParent();
67+
while (parent != null && parent instanceof View) {
68+
if (!parent.isLayoutRequested()) {
69+
parent.requestLayout();
70+
if (!isAll) {
71+
break;
72+
}
73+
}
74+
parent = parent.getParent();
75+
}
76+
}
77+
78+
/**
79+
*
80+
* @param bmp
81+
* @param big
82+
* @return
83+
*/
84+
public static Bitmap bigImage(Bitmap bmp, float big) {
85+
int bmpWidth = bmp.getWidth();
86+
int bmpHeight = bmp.getHeight();
87+
Matrix matrix = new Matrix();
88+
matrix.postScale(big, big);
89+
return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
90+
}
91+
92+
/**
93+
* 给TextView设置下划线
94+
* @param textView
95+
*/
96+
public static void setTVUnderLine(TextView textView) {
97+
textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
98+
textView.getPaint().setAntiAlias(true);
99+
}
100+
101+
102+
static PopupWindow popupWindow;
103+
104+
/**
105+
* 显示PopupWindow
106+
* @param context
107+
* @param resId
108+
* @param root
109+
* @param paramsType
110+
* @return
111+
*/
112+
public static View showPopupWindow(Context context, int resId, View root, int paramsType) {
113+
View popupView;
114+
popupView = LayoutInflater.from(context).inflate(resId, null);
115+
116+
switch (paramsType) {
117+
case 1:
118+
popupWindow = new PopupWindow(popupView,
119+
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
120+
break;
121+
case 2:
122+
popupWindow = new PopupWindow(popupView,
123+
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
124+
break;
125+
case 3:
126+
popupWindow = new PopupWindow(popupView,
127+
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
128+
break;
129+
case 4:
130+
popupWindow = new PopupWindow(popupView,
131+
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
132+
break;
133+
default:
134+
popupWindow = new PopupWindow(popupView,
135+
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
136+
break;
137+
}
138+
popupWindow.setFocusable(true);
139+
popupWindow.setOutsideTouchable(true);
140+
popupWindow.setTouchable(true);
141+
popupWindow.setBackgroundDrawable(new BitmapDrawable());
142+
popupWindow.showAsDropDown(root);
143+
return popupView;
144+
}
145+
146+
/**
147+
* 关闭PopupWindow
148+
*/
149+
public static void dismissPopup() {
150+
if (popupWindow != null && popupWindow.isShowing()) {
151+
popupWindow.dismiss();
152+
popupWindow = null;
153+
}
154+
}
155+
156+
/**
157+
*截图
158+
* @param v
159+
* @return
160+
*/
161+
public static Bitmap captureView(View v) {
162+
v.setDrawingCacheEnabled(true);
163+
v.buildDrawingCache();
164+
return v.getDrawingCache();
165+
}
166+
167+
/**
168+
*截图
169+
* @param v
170+
* @return
171+
*/
172+
public static Bitmap createViewBitmap(View v) {
173+
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
174+
Canvas canvas = new Canvas(bitmap);
175+
v.draw(canvas);
176+
return bitmap;
177+
}
178+
179+
/**
180+
* 截图
181+
* @param view
182+
* @return
183+
*/
184+
public static Bitmap convertViewToBitmap(View view) {
185+
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
186+
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
187+
view.buildDrawingCache();
188+
return view.getDrawingCache();
189+
}
190+
191+
/**
192+
* 获取Activity的截图
193+
* @param activity
194+
* @return
195+
*/
196+
public static Bitmap getActivityBitmap(Activity activity) {
197+
View view = activity.getWindow().getDecorView().findViewById(android.R.id.content);
198+
view.setDrawingCacheEnabled(true);
199+
return view.getDrawingCache();
200+
}
201+
202+
/**
203+
* 获取状态栏高度
204+
* @param context
205+
* @return
206+
*/
207+
public static int getStatusBarHeight(Context context) {
208+
int result = 0;
209+
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
210+
if (resourceId > 0) {
211+
result = context.getResources().getDimensionPixelSize(resourceId);
212+
}
213+
return result;
214+
}
215+
216+
/**
217+
* 获取工具栏高度
218+
* @param context
219+
* @return
220+
*/
221+
public static int getToolbarHeight(Context context) {
222+
final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(new int[]{R.attr.actionBarSize});
223+
int toolbarHeight = (int) styledAttributes.getDimension(0, 0);
224+
styledAttributes.recycle();
225+
return toolbarHeight;
226+
}
227+
228+
/**
229+
* 获取导航栏高度
230+
* @param activity
231+
* @return
232+
*/
233+
public static int getNavigationBarHeight(Activity activity) {
234+
Resources resources = activity.getResources();
235+
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
236+
if (resourceId > 0) {
237+
return resources.getDimensionPixelSize(resourceId);
238+
}
239+
return 0;
240+
}
241+
242+
/**
243+
* 测量view
244+
* @param view
245+
*/
246+
public static void measureView(View view) {
247+
ViewGroup.LayoutParams p = view.getLayoutParams();
248+
if (p == null) {
249+
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
250+
}
251+
252+
int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0, p.width);
253+
int lpHeight = p.height;
254+
int childHeightSpec;
255+
if (lpHeight > 0) {
256+
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
257+
} else {
258+
childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
259+
}
260+
view.measure(childWidthSpec, childHeightSpec);
261+
}
262+
263+
/**
264+
* 获取view的宽度
265+
* @param view
266+
* @return
267+
*/
268+
public static int getViewWidth(View view) {
269+
measureView(view);
270+
return view.getMeasuredWidth();
271+
}
272+
273+
/**
274+
* 获取view的高度
275+
* @param view
276+
* @return
277+
*/
278+
public static int getViewHeight(View view) {
279+
measureView(view);
280+
return view.getMeasuredHeight();
281+
}
282+
283+
/**
284+
* 获取view的上下文
285+
* @param view
286+
* @return
287+
*/
288+
public static Activity getActivity(View view) {
289+
Context context = view.getContext();
290+
while (context instanceof ContextWrapper) {
291+
if (context instanceof Activity) {
292+
return (Activity) context;
293+
}
294+
context = ((ContextWrapper) context).getBaseContext();
295+
}
296+
throw new IllegalStateException("View " + view + " is not attached to an Activity");
297+
}
298+
299+
50300
}

0 commit comments

Comments
 (0)