下面列出了android.view.View#getLocationInWindow ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void showWindow(View parent) {
int[] location = new int[2];
//location [0]--->x坐标,location [1]--->y坐标
parent.getLocationInWindow(location);
// 创建一个PopuWidow对象
popupWindow = new PopupWindow(popView, windowManager.getDefaultDisplay().getWidth(), UIHelp.dip2px(getActivity(), 56));
// 焦点
popupWindow.setFocusable(true);
// 设置允许在外点击消失
popupWindow.setOutsideTouchable(true);
// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAsDropDown(parent, location[0], 0);
}
/**
* 根据传入控件的坐标和用户的焦点坐标,判断是否隐藏键盘,如果点击的位置在控件内,则不隐藏键盘
*
* @param view
* 控件view
* @param event
* 焦点位置
* @return 是否隐藏
*/
public static void hideKeyboard(MotionEvent event, View view,
Activity activity) {
try {
if (view != null && view instanceof EditText) {
int[] location = { 0, 0 };
view.getLocationInWindow(location);
int left = location[0], top = location[1], right = left
+ view.getWidth(), bootom = top + view.getHeight();
// 判断焦点位置坐标是否在空间内,如果位置在控件外,则隐藏键盘
if (event.getRawX() < left || event.getRawX() > right
|| event.getY() < top || event.getRawY() > bootom) {
// 隐藏键盘
IBinder token = view.getWindowToken();
InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(token,
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 是否应该隐藏软键盘
*
* @param v
* @param event
* @return
*/
private boolean isShouldHideInput(View v, MotionEvent event) {
if (v != null && (v instanceof EditText)) {
int[] leftTop = {0, 0};
//获取输入框当前的location位置
v.getLocationInWindow(leftTop);
int left = leftTop[0];
int top = leftTop[1];
int bottom = top + v.getHeight();
int right = left + v.getWidth();
if (event.getX() > left && event.getX() < right
&& event.getY() > top && event.getY() < bottom) {
// 点击的是输入框区域,保留点击EditText的事件
return false;
} else {
return true;
}
}
return false;
}
/**
* 处理点击非 EditText 区域时,自动关闭键盘
*
* @param isAutoCloseKeyboard 是否自动关闭键盘
* @param currentFocusView 当前获取焦点的控件
* @param motionEvent 触摸事件
* @param dialogOrActivity Dialog 或 Activity
*/
public static void handleAutoCloseKeyboard(boolean isAutoCloseKeyboard, View currentFocusView, MotionEvent motionEvent, Object dialogOrActivity) {
if (isAutoCloseKeyboard && motionEvent.getAction() == MotionEvent.ACTION_DOWN && currentFocusView != null && (currentFocusView instanceof EditText) && dialogOrActivity != null) {
int[] leftTop = {0, 0};
currentFocusView.getLocationInWindow(leftTop);
int left = leftTop[0];
int top = leftTop[1];
int bottom = top + currentFocusView.getHeight();
int right = left + currentFocusView.getWidth();
if (!(motionEvent.getX() > left && motionEvent.getX() < right && motionEvent.getY() > top && motionEvent.getY() < bottom)) {
if (dialogOrActivity instanceof Dialog) {
XKeyboardUtils.closeKeyboard((Dialog) dialogOrActivity);
} else if (dialogOrActivity instanceof Activity) {
XKeyboardUtils.closeKeyboard((Activity) dialogOrActivity);
}
}
}
}
private void updateLayoutParamsForPosiion(View anchor, WindowManager.LayoutParams p, int yOffset) {
measureFloater();
int measuredHeight = mPopupView.getMeasuredHeight();
int paddingBottom = mPopupView.mMarker.getPaddingBottom();
anchor.getLocationInWindow(mDrawingLocation);
p.x = 0;
p.y = mDrawingLocation[1] - measuredHeight + yOffset + paddingBottom;
p.width = screenSize.x;
p.height = measuredHeight;
}
/**
* 带水波动画的Activity跳转
*/
@SuppressLint("NewApi")
protected void navigateWithRippleCompat(final Activity activity, final Intent intent,
final View triggerView, @ColorRes int color) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
ActivityOptionsCompat option = ActivityOptionsCompat.makeClipRevealAnimation(triggerView, 0, 0,
triggerView.getMeasuredWidth(), triggerView.getMeasuredHeight());
ActivityCompat.startActivity(activity, intent, option.toBundle());
return;
}
int[] location = new int[2];
triggerView.getLocationInWindow(location);
final int cx = location[0] + triggerView.getWidth() / 2;
final int cy = location[1] + triggerView.getHeight() / 2;
final ImageView view = new ImageView(activity);
view.setImageResource(color);
final ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
int w = decorView.getWidth();
int h = decorView.getHeight();
decorView.addView(view, w, h);
int finalRadius = (int) Math.sqrt(w * w + h * h) + 1;
Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
anim.setDuration(500);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
activity.startActivity(intent);
activity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
decorView.postDelayed(() -> decorView.removeView(view), 500);
}
});
anim.start();
}
private Pair<Integer, Integer> getFabPosition(View fromView, View containerView) {
int[] fromLocation = new int[2];
fromView.getLocationInWindow(fromLocation);
int[] containerLocation = new int[2];
containerView.getLocationInWindow(containerLocation);
int relativeLeft = fromLocation[0] - containerLocation[0];
int relativeTop = fromLocation[1] - containerLocation[1];
int mCx = fromView.getWidth() / 2 + relativeLeft;
int mCy = fromView.getHeight() / 2 + relativeTop;
return Pair.create(mCx, mCy);
}
private void showPopupWindowList(PopupWindow popupWindow, View view) {
popupWindow.setOutsideTouchable(true);
popupWindow.setWidth(getResources().getDimensionPixelSize(R.dimen.popup_width));
popupWindow.setHeight(getResources().getDimensionPixelSize(R.dimen.popup_width));
int[] location = new int[2];
view.getLocationInWindow(location);
int x = location[0] - popupWindow.getWidth();
int y = location[1] + view.getHeight() / 2 - popupWindow.getHeight() / 2;
popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, y);
}
public MTransitionView(View source) {
Assert.assertNotNull(source);
mSourceView = source;
mContent = new CloneView(source.getContext());
// TODO:处理有replace的情况
((CloneView) mContent).setSourceView(source);
source.getLocationInWindow(mLocation);
}
public static void setAnim(Activity activity , View imgphoto , View imgcar){
mActivity = activity;
mImgcar = imgcar;
// 一个整型数组,用来存储按钮的在屏幕的X、Y坐标
int[] start_location = new int[2];
// 这是获取购买按钮的在屏幕的X、Y坐标(这也是动画开始的坐标)
imgphoto.getLocationInWindow(start_location);
int[] start_location1 = new int[]{start_location[0], start_location[1]};
// buyImg是动画的图片,我的是一个小球(R.drawable.sign)
ImageView buyImg = new ImageView(mActivity);
// 设置buyImg的图片
buyImg.setImageResource(R.drawable.aii);
// 开始执行动画
startAnim(buyImg, start_location1);
}
private boolean canChildScroll(float rawX, float rawY){
int[] location = new int[2];
View childView;
scrollChildListIterator = scrollChildList.iterator();
while(scrollChildListIterator.hasNext()){
childView = scrollChildListIterator.next();
childView.getLocationInWindow(location);
rect.set(childView.getLeft(), location[1], childView.getRight(), location[1]+childView.getHeight());
if(rect.contains((int)rawX, (int)rawY)){
return true;
}
}
return false;
}
private boolean canChildScroll(float rawX, float rawY){
int[] location = new int[2];
View childView;
scrollChildListIterator = scrollChildList.iterator();
while(scrollChildListIterator.hasNext()){
childView = scrollChildListIterator.next();
childView.getLocationInWindow(location);
rect.set(childView.getLeft(), location[1], childView.getRight(), location[1]+childView.getHeight());
if(rect.contains((int)rawX, (int)rawY)){
return true;
}
}
return false;
}
public void showSearchPopWindow( View parent) {
int[] location = new int[2];
//location [0]--->x坐标,location [1]--->y坐标
parent.getLocationInWindow(location);
popupWindow.showAsDropDown(parent, location[0], 0);
}
public void getViewRectRelativeToSelf(View v, Rect r) {
int[] loc = new int[2];
getLocationInWindow(loc);
int x = loc[0];
int y = loc[1];
v.getLocationInWindow(loc);
int vX = loc[0];
int vY = loc[1];
int left = vX - x;
int top = vY - y;
r.set(left, top, left + v.getMeasuredWidth(), top + v.getMeasuredHeight());
}
@Override
public void setShapeInfo(View targetView, ViewGroup parent, int offset, Context context) {
if (targetView != null) {
minHeight = targetView.getMeasuredHeight() + (offset * 2);
minWidth = targetView.getMeasuredWidth() + (offset * 2);
maxHeight = parent.getHeight() * 2;
maxWidth = parent.getWidth() * 2;
int[] targetViewLocationInWindow = new int[2];
targetView.getLocationInWindow(targetViewLocationInWindow);
setCenterX(targetViewLocationInWindow[0] + targetView.getWidth() / 2);
setCenterY(targetViewLocationInWindow[1] + targetView.getHeight() / 2);
setLeft(targetViewLocationInWindow[0] - offset);
setRight(targetViewLocationInWindow[0] + (int)minWidth - offset);
setTop(targetViewLocationInWindow[1] - offset);
setBottom(targetViewLocationInWindow[1] + (int)minHeight - offset);
setWidth(minWidth);
setHeight(minHeight);
} else {
if (parent != null) {
minHeight = 0;
minWidth = 0;
maxHeight = parent.getHeight();
maxWidth = parent.getWidth();
setCenterX(parent.getMeasuredWidth() / 2);
setCenterY(parent.getMeasuredHeight() / 2);
setLeft(0);
setTop(0);
setRight(0);
setBottom(0);
}
}
currentHeight = maxHeight;
currentWidth = maxWidth;
}
public static int[] getLocationInWindow(View view) {
int[] location = new int[2];
view.getLocationInWindow(location);
return location;
}
public static int getTop(@NonNull View view) {
final int[] coordinates = new int[3];
view.getLocationInWindow(coordinates);
return coordinates[1];
}
public static Dialog showPopupFromAnchor(final View anchor, int idLayout, boolean cancelable) {
try {
if (anchor == null || idLayout == 0) {
return null;
}
final Context context = anchor.getContext();
View rootView = anchor.getRootView();
Rect rectAnchor = new Rect();
int[] location = new int[2];
anchor.getLocationInWindow(location);
rectAnchor.set(location[0], location[1], location[0] + anchor.getWidth(), location[1] + anchor.getHeight());
Rect rectRoot = new Rect();
rootView.getLocationInWindow(location);
rectRoot.set(location[0], location[1], location[0] + rootView.getWidth(), location[1] + rootView.getHeight());
final Dialog dialog = new Dialog(context,
android.R.style.Theme_Translucent_NoTitleBar);
View dialogView = LayoutInflater.from(context).inflate(idLayout, (ViewGroup)anchor.getRootView(), false);
dialogView.measure(
View.MeasureSpec.makeMeasureSpec(rectRoot.width(), View.MeasureSpec.AT_MOST),
View.MeasureSpec.makeMeasureSpec((rectAnchor.top - rectRoot.top), View.MeasureSpec.AT_MOST));
dialog.setTitle(null);
dialog.setContentView(dialogView);
dialog.setCancelable(true);
// Setting dialogview
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.x = rectAnchor.left - PopupDialog.dpToPx(20, context);
wlp.y = rectAnchor.top - dialogView.getMeasuredHeight();
wlp.width = dialogView.getMeasuredWidth();
wlp.height = dialogView.getMeasuredHeight();
wlp.gravity = Gravity.TOP | Gravity.START;
wlp.dimAmount = 0.6f;
wlp.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
if (cancelable) {
dialog.setCanceledOnTouchOutside(true);
}
View btnClose = dialogView.findViewById(R.id.btnClose);
if (btnClose != null) {
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
dialog.show();
return dialog;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static Bitmap doSanpForListOrScroller(View sanpView){
Bitmap b = null;
if(sanpView!=null){
int[] location = new int[2];
sanpView.getLocationInWindow(location);
int x = location[0];
int y = location[1];
sanpView = rootView;
sanpView.setDrawingCacheEnabled(true);
sanpView.buildDrawingCache();
// sanpView = ((View)sanpView.getParent().getParent());
Bitmap bitmap = sanpView.getDrawingCache();
// sanpView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
// View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
// sanpView.layout(0, 0, sanpView.getMeasuredWidth(), sanpView.getMeasuredHeight());
// sanpView.buildDrawingCache();
// Bitmap bitmap = sanpView.getDrawingCache();
// b = bitmap;
int sanpWidth = sanpView.getWidth();
Log.e("weex_test", "sanpView.getWidth=="+ sanpWidth);
int snapHeight = sanpView.getHeight();
Log.e("weex_test", "sanpView.getHeight==" + snapHeight);
// bitmap = Bitmap.createBitmap(sanpWidth+x, snapHeight+x,Bitmap.Config.ARGB_8888);
// int width = activity.getWindowManager().getDefaultDisplay().getWidth();
// int height = activity.getWindowManager().getDefaultDisplay().getHeight();
int baseWidth = 750;
int baseHeight = 1134;
// 计算缩放因子
// float heightScale = ((float) baseHeight) / scrollerHeight;
float widthScale = ((float) baseWidth) / sanpWidth;
// 新建立矩阵 按照宽度缩放因子自适应缩放
Matrix matrix = new Matrix();
matrix.postScale(widthScale, widthScale);
Log.e("weex_test", "widthScale=="+widthScale+ "|"+
"Real sanpWidth==" + sanpWidth*widthScale +"|" +
"Real snapHeight==" + widthScale*snapHeight +
"|" + "sanpView.x=" + x +
"|" + "sanpView.y= " + y);
b = Bitmap.createBitmap(bitmap, 0, 0, sanpWidth, snapHeight);
// b = Bitmap.createBitmap(bitmap, 0, 0, rootView.getWidth(), rootView.getHeight());
// 缩放
// Bitmap returnBmp = Bitmap.createBitmap((int) dw, (int) dh,
// Bitmap.Config.ARGB_4444);
b = Bitmap.createBitmap(bitmap,0, 0,
sanpWidth, snapHeight, matrix, true);
// b = Bitmap.createBitmap(bitmap, 0, 0, scrollerWidth,
// scrollerHeight, matrix, true);
// b = Bitmap.createBitmap(bitmap, 0, statusBarHeight + actionBarHeight, width,
// height - statusBarHeight - actionBarHeight, matrix, true);
sanpView.destroyDrawingCache();
}else {
Log.e("weex_test", "snapshot view is " + sanpView);
}
return b;
}
public void drawCursorView(Canvas canvas, View view, float scale, boolean focus){
if(view!=null){
canvas.save();
if (null == mLocation) {
mLocation = new int[2];
}
if (null == mFocusLocation) {
mFocusLocation = new int[2];
}
getLocationInWindow(mLocation);
view.getLocationInWindow(mFocusLocation);
int width = view.getWidth();
int height = view.getHeight();
if(view instanceof MirrorItemView ){
height = ((MirrorItemView)view).getContentView().getHeight();
}
int left = (int)(mFocusLocation[0]-mLocation[0]-width*(scale-1)/2);
int top = (int)(mFocusLocation[1]-mLocation[1]-height*(scale-1)/2);
canvas.translate(left, top);
canvas.scale(scale, scale);
//view.draw(canvas);
if(view instanceof MirrorItemView ){
Bitmap bmp = ((MirrorItemView)view).getReflectBitmap();
if(bmp != null)
canvas.drawBitmap(bmp, 0, height, null);
}
if(focus){
Rect padding = new Rect();
mDrawableShadow.getPadding(padding);
mDrawableShadow.setBounds(-padding.left, -padding.top, width+padding.right, height+padding.bottom);
mDrawableShadow.setAlpha((int)(255*(scale-1)*10));
mDrawableShadow.draw(canvas);
mDrawableWhite.getPadding(padding);
mDrawableWhite.setBounds(-padding.left-1, -padding.top-1, width+padding.right+1, height+padding.bottom+1);
mDrawableWhite.setAlpha((int)(255*(scale-1)*10));
mDrawableWhite.draw(canvas);
}
view.draw(canvas);
canvas.restore();
}
}