下面列出了android.view.View#getLocalVisibleRect ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void scrollToView(final ScrollView scrollView, final View view)
{
view.requestFocus();
final Rect scrollBounds = new Rect();
scrollView.getHitRect(new Rect());
if (!view.getLocalVisibleRect(scrollBounds))
{
new Handler().post(new Runnable() {
@Override
public void run()
{
scrollView.smoothScrollTo(0, view.getBottom());
}
});
}
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
updateBackground(pressedDrawable);
this.setPadding(mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom);
break;
case MotionEvent.ACTION_MOVE:
Rect r = new Rect();
view.getLocalVisibleRect(r);
if (!r.contains((int) motionEvent.getX(), (int) motionEvent.getY() + 3 * mShadowHeight) &&
!r.contains((int) motionEvent.getX(), (int) motionEvent.getY() - 3 * mShadowHeight)) {
updateBackground(unpressedDrawable);
this.setPadding(mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom + mShadowHeight);
}
break;
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
updateBackground(unpressedDrawable);
this.setPadding(mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom + mShadowHeight);
break;
}
return false;
}
@SuppressLint("NewApi")
public void startRectAnimation(View view)
{
View someImage = findViewById(R.id.some_image);
Rect local = new Rect();
someImage.getLocalVisibleRect(local);
Rect from = new Rect(local);
Rect to = new Rect(local);
from.right = from.left + local.width()/4;
from.bottom = from.top + local.height()/2;
to.left = to.right - local.width()/2;
to.top = to.bottom - local.height()/4;
if (android.os.Build.VERSION.SDK_INT >= 18)
{
ObjectAnimator anim = ObjectAnimator.ofObject(someImage, "clipBounds", new RectEvaluator(), from, to);
anim.setDuration(2000);
anim.start();
}
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
updateBackground(pressedDrawable);
this.setPadding(mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom);
break;
case MotionEvent.ACTION_MOVE:
Rect r = new Rect();
view.getLocalVisibleRect(r);
if (!r.contains((int) motionEvent.getX(), (int) motionEvent.getY() + 3 * mShadowHeight) &&
!r.contains((int) motionEvent.getX(), (int) motionEvent.getY() - 3 * mShadowHeight)) {
updateBackground(unpressedDrawable);
this.setPadding(mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom + mShadowHeight);
}
break;
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
updateBackground(unpressedDrawable);
this.setPadding(mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom + mShadowHeight);
break;
}
return false;
}
public int getVisiblePercent(View v) {
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (v.getLocalVisibleRect(scrollBounds)) {
// Any portion of the imageView, even a single pixel, is within the visible window
} else {
// NONE of the imageView is within the visible window
return -1;
}
if (v.isShown()) {
Rect r = new Rect();
v.getGlobalVisibleRect(r);
double sVisible = r.width() * r.height();
double sTotal = v.getWidth() * v.getHeight();
MyLg.e(TAG, "sVisible " + sVisible + " sTotal" + sTotal);
return (int) (100 * sVisible / sTotal) - 20;
} else {
return -1;
}
}
public int getVisiblePercent(View v) {
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (v.getLocalVisibleRect(scrollBounds)) {
// Any portion of the imageView, even a single pixel, is within the visible window
} else {
// NONE of the imageView is within the visible window
return -1;
}
if (v.isShown()) {
Rect r = new Rect();
v.getGlobalVisibleRect(r);
double sVisible = r.width() * r.height();
double sTotal = v.getWidth() * v.getHeight();
MyLg.e(TAG, "sVisible " + sVisible + " sTotal" + sTotal);
return (int) (100 * sVisible / sTotal) - 20;
} else {
return -1;
}
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
updateBackground(pressedDrawable);
this.setPadding(mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom);
break;
case MotionEvent.ACTION_MOVE:
Rect r = new Rect();
view.getLocalVisibleRect(r);
if (!r.contains((int) motionEvent.getX(), (int) motionEvent.getY() + 3 * mShadowHeight) &&
!r.contains((int) motionEvent.getX(), (int) motionEvent.getY() - 3 * mShadowHeight)) {
updateBackground(unpressedDrawable);
this.setPadding(mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom + mShadowHeight);
}
break;
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
updateBackground(unpressedDrawable);
this.setPadding(mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom + mShadowHeight);
break;
}
return false;
}
private boolean isCircleImageViewVisible() {
for (int i = 0; i < swipeRefreshLayout.getChildCount(); i++) {
View child = swipeRefreshLayout.getChildAt(i);
if (child instanceof ImageView) {
child.getLocalVisibleRect(visibleRect);
if (visibleRect.bottom > 0)
return true;
}
}
return false;
}
public static float getViewVisiblePercent(View view) {
if (view == null) {
return 0f;
}
float height = view.getHeight();
Rect rect = new Rect();
if (!view.getLocalVisibleRect(rect)) {
return 0f;
}
float visibleHeight = rect.bottom - rect.top;
Log.d(TAG, "getViewVisiblePercent: emm " + visibleHeight);
return visibleHeight / height;
}
/**
* Scrolls to the top of the given View in the given ScrollView.
*
* @param scrollView
* @param view
*/
public static void scrollToView(final ScrollView scrollView, final View view) {
/*
The delayed focus has been added so that TalkBack reads the proper view's description that
we want focus on. For example in case of {@link RegistrationEditText} we want accessibility
focus on TIL when an error is displayed instead of the EditText within it, which can only
be achieved through this delay.
*/
view.postDelayed(new Runnable() {
@Override
public void run() {
view.requestFocus();
view.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
}
}, ACCESSIBILITY_FOCUS_DELAY_MS);
// Determine if scroll needs to happen
final Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (!view.getLocalVisibleRect(scrollBounds)) {
new Handler().post(new Runnable() {
@Override
public void run() {
scrollView.smoothScrollTo(0, view.getTop());
}
});
}
}
/**
* View 自身是否可见
*
* @return DecorView 时返回 true
* View 宽、高、透明度 有一个 < 0 时,或 getLocalVisibleRect 为 false 时;返回 false 。
* View getVisibility 不可见,且有 Animation getFillAfter 为 false 时;返回 false 。
* View 无 Animation 时 getVisibility 不可见时返回 false 。
*/
@RequiresApi(api = 11)
private static boolean isViewSelfVisible(View mView) {
if (mView == null) {
return false;
}
if (mView.getWidth() <= 0 || mView.getHeight() <= 0 || mView.getAlpha() <= 0.0f || !mView.getLocalVisibleRect(new Rect())) {
return false;
}
return (mView.getVisibility() != VISIBLE && mView.getAnimation() != null && mView.getAnimation().getFillAfter()) || mView.getVisibility() == VISIBLE;
}
public static View.OnTouchListener getHighlightTouchListener(final int color) {
return new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
Drawable drawable = ((ImageView) view).getDrawable();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
LightingColorFilter lighten = new LightingColorFilter(color, color);
drawable.setColorFilter(lighten);
break;
case MotionEvent.ACTION_UP:
drawable.clearColorFilter();
break;
case MotionEvent.ACTION_MOVE:
Rect rect = new Rect();
view.getLocalVisibleRect(rect);
if (!rect.contains((int) event.getX(), (int) event.getY())) {
drawable.clearColorFilter();
}
break;
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
drawable.clearColorFilter();
break;
}
return false;
}
};
}
/**
* @return Visible rect in screen coordinates for the given View.
*/
private Rect getScreenVisibleRect(View view) {
view.getLocalVisibleRect(mScreenVisibleRect);
view.getLocationOnScreen(mScreenVisiblePoint);
mScreenVisibleRect.offset(mScreenVisiblePoint[0], mScreenVisiblePoint[1]);
return mScreenVisibleRect;
}
/**
* When this method is called, the implementation should provide a visibility percents in range 0 - 100 %
* @param view the view which visibility percent should be calculated.
* Note: visibility doesn't have to depend on the visibility of a full view.
* It might be calculated by calculating the visibility of any inner View
*
* @param item
* @return percents of visibility
*/
public static int getVisibilityPercents(View view, ListItem item) {
final Rect currentViewRect = new Rect();
int percents = 100;
int height = (view == null || view.getVisibility() != View.VISIBLE) ? 0 : view.getHeight();
if (height == 0) {
return 0;
}
if(view.getLocalVisibleRect(currentViewRect)) {
if (viewIsPartiallyHiddenTop(currentViewRect)) {
// view is partially hidden behind the top edge
percents = (height - currentViewRect.top) * 100 / height;
} else if (viewIsPartiallyHiddenBottom(currentViewRect, height)) {
percents = currentViewRect.bottom * 100 / height;
}
// only ListItem's visibility could be 100 percent
if (item == null && percents == 100) {
percents--;
}
return percents;
}
return 0;
}
static View getTheVisibileView(List<View> viewList) {
Rect scrollBounds = new Rect();
int listSize = viewList.size();
for (int i = 0; i < listSize; ++i) {
View view = viewList.get(i);
if (view != null) {
view.getHitRect(scrollBounds);
if (view.getLocalVisibleRect(scrollBounds)) {
return view;
}
}
}
return null;
}
/**
* @param child
* @return
*/
public boolean isChildVisible(View child) {
if (child == null) {
return false;
}
Rect scrollBounds = new Rect();
getHitRect(scrollBounds);
return child.getLocalVisibleRect(scrollBounds);
}
@Override
public void getOutline(View view, Outline outline) {
final Rect clipPath = new Rect();
view.getLocalVisibleRect(clipPath);
outline.setRoundRect(clipPath, mCornerRadius);
}
static boolean isShownWithinConfinesOfVisibleScreen(View view) {
Rect scrollBounds = new Rect();
view.getHitRect(scrollBounds);
return view.getLocalVisibleRect(scrollBounds);
}
public boolean isViewVisible() {
View view = mAwareChild.getHostView();
return view != null && view.getLocalVisibleRect(mVisibleRect);
}
private Bitmap loadBitmap(View backgroundView, View targetView) {
Rect backgroundBounds = new Rect();
backgroundView.getHitRect(backgroundBounds);
if (!targetView.getLocalVisibleRect(backgroundBounds)) {
// NONE of the imageView is within the visible window
return null;
}
Bitmap blurredBitmap = captureView(backgroundView);
//capture only the area covered by our target view
int[] loc = new int[2];
int[] bgLoc = new int[2];
backgroundView.getLocationInWindow(bgLoc);
targetView.getLocationInWindow(loc);
int height = targetView.getHeight();
int y = loc[1];
if (bgLoc[1] >= loc[1]) {
//view is going off the screen at the top
height -= (bgLoc[1] - loc[1]);
if (y < 0)
y = 0;
}
if (y + height > blurredBitmap.getHeight()) {
height = blurredBitmap.getHeight() - y;
Log.d("TAG", "Height = " + height);
if (height <= 0) {
//below the screen
return null;
}
}
Matrix matrix = new Matrix();
//half the size of the cropped bitmap
//to increase performance, it will also
//increase the blur effect.
matrix.setScale(0.5f, 0.5f);
Bitmap bitmap = Bitmap.createBitmap(blurredBitmap,
(int) targetView.getX(),
y,
targetView.getMeasuredWidth(),
height,
matrix,
true);
return bitmap;
//If handling rounded corners yourself.
//Create rounded corners on the Bitmap
//keep in mind that our bitmap is half
//the size of the original view, setting
//it as the background will stretch it out
//so you will need to use a smaller value
//for the rounded corners than you would normally
//to achieve the correct look.
//ImageHelper.roundCorners(
//bitmap,
//getResources().getDimensionPixelOffset(R.dimen.rounded_corner),
//false);
}