/*
* Copyright (C) 2016 android@19code.com
*
* 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.code19.library;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
/**
* Blog : http://blog.csdn.net/u011240877
*/
public class ViewUtils {
public static void removeSelfFromParent(View view) {
if (view != null) {
ViewParent parent = view.getParent();
if (parent != null && parent instanceof ViewGroup) {
ViewGroup group = (ViewGroup) parent;
group.removeView(view);
}
}
}
public static void requestLayoutParent(View view, boolean isAll) {
ViewParent parent = view.getParent();
while (parent != null && parent instanceof View) {
if (!parent.isLayoutRequested()) {
parent.requestLayout();
if (!isAll) {
break;
}
}
parent = parent.getParent();
}
}
public static boolean isTouchInView(MotionEvent ev, View v) {
int[] vLoc = new int[2];
v.getLocationOnScreen(vLoc);
float motionX = ev.getRawX();
float motionY = ev.getRawY();
return motionX >= vLoc[0] && motionX <= (vLoc[0] + v.getWidth()) && motionY >= vLoc[1] && motionY <= (vLoc[1] + v.getHeight());
}
public static Bitmap bigImage(Bitmap bmp, float big) {
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(big, big);
return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
}
public static void setTVUnderLine(TextView textView) {
textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
textView.getPaint().setAntiAlias(true);
}
static PopupWindow popupWindow;
public static View showPopupWindow(Context context, int resId, View root, int paramsType) {
View popupView;
popupView = LayoutInflater.from(context).inflate(resId, null);
switch (paramsType) {
case 1:
popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
break;
case 2:
popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
break;
case 3:
popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
break;
case 4:
popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
break;
default:
popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
break;
}
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAsDropDown(root);
return popupView;
}
public static void dismissPopup() {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
}
public static Bitmap captureView(View v) {
v.setDrawingCacheEnabled(true);
v.buildDrawingCache();
return v.getDrawingCache();
}
public static Bitmap createViewBitmap(View v) {
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
return bitmap;
}
public static Bitmap convertViewToBitmap(View view) {
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
return view.getDrawingCache();
}
public static Bitmap getActivityBitmap(Activity activity) {
View view = activity.getWindow().getDecorView().findViewById(android.R.id.content);
view.setDrawingCacheEnabled(true);
return view.getDrawingCache();
}
public static int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
public static int getToolbarHeight(Context context) {
final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(new int[]{R.attr.actionBarSize});
int toolbarHeight = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
return toolbarHeight;
}
public static int getNavigationBarHeight(Activity activity) {
Resources resources = activity.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}
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, 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);
}
public static int getViewWidth(View view) {
measureView(view);
return view.getMeasuredWidth();
}
public static int getViewHeight(View view) {
measureView(view);
return view.getMeasuredHeight();
}
public static Activity getActivity(View view) {
Context context = view.getContext();
while (context instanceof ContextWrapper) {
if (context instanceof Activity) {
return (Activity) context;
}
context = ((ContextWrapper) context).getBaseContext();
}
throw new IllegalStateException("View " + view + " is not attached to an Activity");
}
public static void showToast(Context context, String content) {
Toast.makeText(context, content, Toast.LENGTH_SHORT).show();
}
public static void showToast(Context context, int res) {
Toast.makeText(context, res, Toast.LENGTH_SHORT).show();
}
}