下面列出了android.view.View#getHeight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected int computeVerticalScrollOffset() {
if (mFirstPosition >= 0 && getChildCount() > 0 && mScrollVertically) {
final View view = getChildAt(0);
final int top = view.getTop();
int height = view.getHeight();
if (height > 0) {
final int numColumns = mNumColumns;
final int whichRow = mFirstPosition / numColumns;
final int rowCount = (mItemCount + numColumns - 1) / numColumns;
return Math.max(whichRow * 100 - (top * 100) / height +
(int) ((float) getScrollY() / getHeight() * rowCount * 100), 0);
}
}
return 0;
}
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
int pageHeight = view.getHeight();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 1) { // [-1,1]
// Modify the default slide transition to shrink the page as well
float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
float vertMargin = pageHeight * (1 - scaleFactor) / 2;
float horzMargin = pageWidth * (1 - scaleFactor) / 2;
if (position < 0) {
view.setTranslationX(horzMargin - vertMargin / 2);
} else {
view.setTranslationX(-horzMargin + vertMargin / 2);
}
// Scale the page down (between MIN_SCALE and 1)
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
// Fade the page relative to its size.
view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA));
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
@Override
public Animator onAppear(ViewGroup sceneRoot, View view,
TransitionValues startValues,
TransitionValues endValues) {
if (view == null || view.getHeight() == 0 || view.getWidth() == 0) return null;
ensureCenterPoint(sceneRoot, view);
return new AnimUtils.NoPauseAnimator(ViewAnimationUtils.createCircularReveal(
view,
center.x,
center.y,
startRadius,
getFullyRevealedRadius(view)));
}
@Override
public void onScrollStateChanged(int newState) {
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
isIdle = false;
Log.d("onScrollStateChanged DRAGGING");
//reset adjusted
gdx = 0;
gdy = 0;
adjusted = false;
//just in case
adjustOnScroll = false;
prevPos = getCurrentPage();
View currView = findViewByPosition(prevPos);
if (currView != null) {
vx = currView.getWidth();
vy = currView.getHeight();
}
} else if (newState == RecyclerView.SCROLL_STATE_SETTLING) {
isIdle = false;
Log.d("onScrollStateChanged SETTLING");
adjust();
} else if (newState == RecyclerView.SCROLL_STATE_IDLE) {
isIdle = true;
Log.d("onScrollStateChanged IDLE");
adjust();
}
}
private boolean isViewContains(View view, float rx, float ry) {
int[] location = new int[2];
view.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
int w = view.getWidth();
int h = view.getHeight();
return !(rx < x || rx > x + w || ry < y || ry > y + h);
}
@Override
public Animator onAppear(ViewGroup sceneRoot, View view,
TransitionValues startValues,
TransitionValues endValues) {
if (view == null || view.getHeight() == 0 || view.getWidth() == 0) return null;
ensureCenterPoint(sceneRoot, view);
return new PauseLessAnimator(ViewAnimationUtils.createCircularReveal(
view,
center.x,
center.y,
startRadius,
getFullyRevealedRadius(view)));
}
public ContentView(Activity activity) {
mActivity = activity;
LayoutInflater inflate = LayoutInflater.from(activity);
mView = (ViewGroup) inflate.inflate(R.layout.view_donate_thanks, null);
ObjectAnimator scaleXDown = ObjectAnimator.ofFloat(mView, "alpha", .8f);
scaleXDown.setRepeatMode(ValueAnimator.REVERSE);
scaleXDown.setDuration(1000);
scaleXDown.setRepeatCount(ValueAnimator.INFINITE);
scaleXDown.start();
Drawable heartDrawable = ToolbarIcons.icon(mView.getContext(), ToolbarIcons.IconID.HEART, 0xffffffff, mIconSize);
heartDrawable.setAlpha(233);
Bitmap heartBitmap = Utils.drawableToBitmap(heartDrawable, mIconSize, mIconSize);
int offsetX = 0;
int offsetY = 0;
View content = activity.getWindow().findViewById(android.R.id.content);
mDisplaySize = new Point(content.getWidth(), content.getHeight()); // Utils.getDisplaySize(activity);
int maxY = mDisplaySize.y - mIconSize;
int maxX = mDisplaySize.x - mIconSize;
for (int y = 0; y < maxY; y += mIconSize) {
for (int x = 0; x < maxX; x += mIconSize) {
addImageView(activity, heartBitmap, x, y);
}
}
showThankYouText();
}
/**
* 适配Android7.0
*
* @param anchor
*/
@Override
public void showAsDropDown(View anchor) {
setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
refreshData();
if (Build.VERSION.SDK_INT >= 25) {
Rect rect = new Rect();
anchor.getGlobalVisibleRect(rect);
int mHeight = Utils.getScreenHeight(mContext) - rect.bottom;
setHeight(mHeight);
super.showAsDropDown(anchor);
} else if (Build.VERSION.SDK_INT == 24) {
int[] a = new int[2];
anchor.getLocationInWindow(a);
int heignt = anchor.getHeight();
this.showAtLocation(mActivity.getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, a[1] + heignt);
this.update();
} else {
super.showAsDropDown(anchor);
}
int height = 0;
if (mRootView.getMeasuredHeight() == 0) {
height = getHeight();
} else {
height = mRootView.getMeasuredHeight();
}
createInAnimation(mContext, -height);
}
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
final int childWidth = releasedChild.getWidth();
final int childHeight = releasedChild.getHeight();
boolean fling = false;
int left = mInsets.left, top = 0;
if ((mTrackingEdge & EDGE_LEFT) != 0) {
if (Math.abs(xvel) > mFlingVelocity) {
fling = true;
}
left = xvel >= 0 && (fling || mScrollPercent > mScrollThreshold)
? childWidth + mInsets.left : mInsets.left;
}
if ((mTrackingEdge & EDGE_RIGHT) != 0) {
if (Math.abs(xvel) > mFlingVelocity) {
fling = true;
}
left = xvel <= 0 && (fling || mScrollPercent > mScrollThreshold)
? -childWidth + mInsets.left : mInsets.left;
}
if ((mTrackingEdge & EDGE_TOP) != 0) {
if (Math.abs(yvel) > mFlingVelocity) {
fling = true;
}
top = yvel >= 0 && (fling || mScrollPercent > mScrollThreshold)
? childHeight : 0;
}
if ((mTrackingEdge & EDGE_BOTTOM) != 0) {
if (Math.abs(yvel) > mFlingVelocity) {
fling = true;
}
top = yvel <= 0 && (fling || mScrollPercent > mScrollThreshold)
? -childHeight + getSystemTop() : 0;
}
mDragHelper.settleCapturedViewAt(left, top);
invalidate();
}
private void transformMatrix(Matrix m, View view) {
final float w = view.getWidth();
final float h = view.getHeight();
final boolean hasPivot = mHasPivot;
final float pX = hasPivot ? mPivotX : w / 2f;
final float pY = hasPivot ? mPivotY : h / 2f;
final float rX = mRotationX;
final float rY = mRotationY;
final float rZ = mRotationZ;
if ((rX != 0) || (rY != 0) || (rZ != 0)) {
final Camera camera = mCamera;
camera.save();
camera.rotateX(rX);
camera.rotateY(rY);
camera.rotateZ(-rZ);
camera.getMatrix(m);
camera.restore();
m.preTranslate(-pX, -pY);
m.postTranslate(pX, pY);
}
final float sX = mScaleX;
final float sY = mScaleY;
if ((sX != 1.0f) || (sY != 1.0f)) {
m.postScale(sX, sY);
final float sPX = -(pX / w) * ((sX * w) - w);
final float sPY = -(pY / h) * ((sY * h) - h);
m.postTranslate(sPX, sPY);
}
m.postTranslate(mTranslationX, mTranslationY);
}
/**
* @return Returns true this ScrollView can be scrolled
*/
private boolean canScroll() {
View child = getChildAt(0);
if (child != null) {
int childHeight = child.getHeight();
return getHeight() < (childHeight + getPaddingTop() + getPaddingBottom());
}
return false;
}
@Override
public void transformPage(View view, float position) {
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left page
view.setAlpha(1);
// Counteract the default slide transition
view.setTranslationX(view.getWidth() * -position);
//set Y position to swipe in from top
float yPosition = position * view.getHeight();
view.setTranslationY(yPosition);
view.setScaleX(1);
view.setScaleY(1);
} else if (position <= 1) { // [0,1]
view.setAlpha(1);
// Counteract the default slide transition
view.setTranslationX(view.getWidth() * -position);
// Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE
+ (1 - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
/**
* 点击点是否在指定View内
* @param view
* @param x
* @param y
* @return
*/
public static boolean pointInView(View view, int x, int y) {
int[] position = new int[2];
view.getLocationOnScreen(position);
int right = view.getWidth() + position[0];
int bottom = view.getHeight() + position[1];
return x >= position[0] && x <= right && y >= position[1] && y < bottom;
}
/**
* 判断target是否可滚动
*
* @param target
* @return
*/
private boolean isTargetScrollable(View target) {
LinkageScrollHandler linkageScrollHandler
= getLinkageScrollHandler(target);
boolean isScrollable = linkageScrollHandler.isScrollable();
return isScrollable && (target.getHeight() >= getHeight());
}
private boolean isStickyHeaderBeingPushedOffscreen(RecyclerView recyclerView, View stickyHeader) {
View viewAfterHeader = getFirstViewUnobscuredByHeader(recyclerView, stickyHeader);
int firstViewUnderHeaderPosition = recyclerView.getChildAdapterPosition(viewAfterHeader);
if (firstViewUnderHeaderPosition == RecyclerView.NO_POSITION) {
return false;
}
boolean isReverseLayout = mOrientationProvider.isReverseLayout(recyclerView);
if (firstViewUnderHeaderPosition > 0 && hasNewHeader(firstViewUnderHeaderPosition, isReverseLayout)) {
View nextHeader = mHeaderProvider.getHeader(recyclerView, firstViewUnderHeaderPosition);
mDimensionCalculator.initMargins(mTempRect1, nextHeader);
mDimensionCalculator.initMargins(mTempRect2, stickyHeader);
if (mOrientationProvider.getOrientation(recyclerView) == LinearLayoutManager.VERTICAL) {
int topOfNextHeader = viewAfterHeader.getTop() - mTempRect1.bottom - nextHeader.getHeight() - mTempRect1.top;
int bottomOfThisHeader = recyclerView.getPaddingTop() + stickyHeader.getBottom() + mTempRect2.top + mTempRect2.bottom;
if (topOfNextHeader < bottomOfThisHeader) {
return true;
}
} else {
int leftOfNextHeader = viewAfterHeader.getLeft() - mTempRect1.right - nextHeader.getWidth() - mTempRect1.left;
int rightOfThisHeader = recyclerView.getPaddingLeft() + stickyHeader.getRight() + mTempRect2.left + mTempRect2.right;
if (leftOfNextHeader < rightOfThisHeader) {
return true;
}
}
}
return false;
}
private static boolean hitTest(View child, float x, float y, float left, float top) {
return x >= left
&& x <= left + child.getWidth()
&& y >= top
&& y <= top + child.getHeight();
}
private void onUpdateScroll(int dx, int dy) {
if (recyclerView.getWidth() == 0) {
return;
}
registerAdapter();
View firstVisibleView = recyclerView.getChildAt(0);
if (firstVisibleView == null) {
return;
}
int firstVisiblePosition = recyclerView.getChildAdapterPosition(firstVisibleView);
int itemCount = recyclerView.getAdapter().getItemCount();
int columns = Math.round(recyclerView.getWidth() / firstVisibleView.getWidth());
if (visibleRange == -1) {
visibleRange = recyclerView.getChildCount();
}
// Add the percentage of the item the user has scrolled past already
float pastFirst = -firstVisibleView.getY() / firstVisibleView.getHeight() * columns;
float position = firstVisiblePosition + pastFirst;
// Scale this so as we move down the visible range gets added to position from 0 -> visible range
float scaledVisibleRange = position / (float) (itemCount - visibleRange) * visibleRange;
position += scaledVisibleRange;
float proportion = position / itemCount;
setBubbleAndHandlePosition(height * proportion);
if ((visibleRange * 2) < itemCount) {
if (!hasScrolled && (dx > 0 || dy > 0)) {
setVisibility(View.VISIBLE);
hasScrolled = true;
recyclerView.setVerticalScrollBarEnabled(false);
}
} else if (hasScrolled) {
setVisibility(View.GONE);
hasScrolled = false;
recyclerView.setVerticalScrollBarEnabled(true);
}
}
public void transformPage(View page, float position) {
final float alpha;
final float scale;
final float translationX;
switch (mTransformType) {
case FLOW:
page.setRotationY(position * -30f);
return;
case SLIDE_OVER:
if (position < 0 && position > -1) {
// this is the page to the left
scale = Math.abs(Math.abs(position) - 1) * (1.0f - SCALE_FACTOR_SLIDE) + SCALE_FACTOR_SLIDE;
alpha = Math.max(MIN_ALPHA_SLIDE, 1 - Math.abs(position));
int pageWidth = page.getWidth();
float translateValue = position * -pageWidth;
if (translateValue > -pageWidth) {
translationX = translateValue;
} else {
translationX = 0;
}
} else {
alpha = 1;
scale = 1;
translationX = 0;
}
break;
case DEPTH:
if (position > 0 && position < 1) {
// moving to the right
alpha = (1 - position);
scale = MIN_SCALE_DEPTH + (1 - MIN_SCALE_DEPTH) * (1 - Math.abs(position));
translationX = (page.getWidth() * -position);
} else {
// use default for all other cases
alpha = 1;
scale = 1;
translationX = 0;
}
break;
case ZOOM:
if (position >= -1 && position <= 1) {
scale = Math.max(MIN_SCALE_ZOOM, 1 - Math.abs(position));
alpha = MIN_ALPHA_ZOOM +
(scale - MIN_SCALE_ZOOM) / (1 - MIN_SCALE_ZOOM) * (1 - MIN_ALPHA_ZOOM);
float vMargin = page.getHeight() * (1 - scale) / 2;
float hMargin = page.getWidth() * (1 - scale) / 2;
if (position < 0) {
translationX = (hMargin - vMargin / 2);
} else {
translationX = (-hMargin + vMargin / 2);
}
} else {
alpha = 1;
scale = 1;
translationX = 0;
}
break;
default:
return;
}
page.setAlpha(alpha);
page.setTranslationX(translationX);
page.setScaleX(scale);
page.setScaleY(scale);
}
public void show(View anchor, boolean center) {
contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int contentWidth = contentView.getMeasuredWidth();
int contentHeight = contentView.getMeasuredHeight();
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
int xPos = 0;
int yPos = 0;
boolean onTop = true;
boolean onLeft = true;
int gravity = Gravity.NO_GRAVITY;
if (center) {
xPos = 0;
yPos = 0;
gravity = Gravity.CENTER;
} else {
int[] location = new int[2];
anchor.getLocationOnScreen(location);
Rect anchorRect = new Rect(location[0], location[1], location[0] + anchor.getWidth(), location[1] + anchor.getHeight());
if ((anchorRect.left + contentWidth) > screenWidth) {
if (anchorRect.right - contentWidth < 0) {
xPos = screenWidth - contentWidth - 4;
} else {
xPos = anchorRect.right - contentWidth;
}
onLeft = false;
} else {
xPos = anchorRect.left;
onLeft = true;
}
if (anchorRect.top < screenHeight / 2) {
yPos = anchorRect.bottom + 4;
onTop = true;
} else {
yPos = anchorRect.top - contentHeight - 4;
onTop = false;
}
if (onTop) {
if (onLeft) {
setAnimationStyle(R.style.Popups_Down_Left);
} else {
setAnimationStyle(R.style.Popups_Down_Right);
}
} else {
if (onLeft) {
setAnimationStyle(R.style.Popups_Up_Left);
} else {
setAnimationStyle(R.style.Popups_Up_Right);
}
}
}
setOnDismissListener(this);
onShow();
showAtLocation(anchor, gravity, xPos, yPos);
}
/**
* view是否处于某个坐标点下面,相对屏幕的坐标
*
* @param view
* @param x
* @param y
* @return
*/
public static boolean isViewUnderScreen(View view, int x, int y)
{
final int[] location = new int[2];
view.getLocationOnScreen(location);
return x >= location[0] && x < location[0] + view.getWidth()
&& y >= location[1] && y < location[1] + view.getHeight();
}