Java Code Examples for android.view.View#getLocalVisibleRect()

The following examples show how to use android.view.View#getLocalVisibleRect() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PreferencesHelpDialog.java    From Androzic with GNU General Public License v3.0 6 votes vote down vote up
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());
			}
		});
	}
}
 
Example 2
Source File: LoadingButton.java    From LoadingButton with MIT License 6 votes vote down vote up
@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;
}
 
Example 3
Source File: FButton.java    From android-flat-button with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 4
Source File: PropertyAnimation06.java    From cogitolearning-examples with MIT License 6 votes vote down vote up
@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();
  }
  
}
 
Example 5
Source File: FButton.java    From Trivia-Knowledge with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 6
Source File: PlaceDetailActivity.java    From Expert-Android-Programming with MIT License 6 votes vote down vote up
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;
        }
    }
 
Example 7
Source File: CollectionDetailActivity.java    From Expert-Android-Programming with MIT License 6 votes vote down vote up
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;
        }
    }
 
Example 8
Source File: RLScrollView.java    From Roid-Library with Apache License 2.0 5 votes vote down vote up
/**
 * @param child
 * @return
 */
public boolean isChildVisible(View child) {
    if (child == null) {
        return false;
    }
    Rect scrollBounds = new Rect();
    getHitRect(scrollBounds);
    return child.getLocalVisibleRect(scrollBounds);
}
 
Example 9
Source File: Utils.java    From MaterialViewPager with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: VisibilityPercentsCalculator.java    From VideoListPlayer with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: AppMenuDragHelper.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * @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;
}
 
Example 12
Source File: NotificationHelper.java    From NotificationPeekPort with Apache License 2.0 5 votes vote down vote up
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;
        }
    };
}
 
Example 13
Source File: FrgUtil.java    From sa-sdk-android with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 14
Source File: RegisterActivity.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
            }
        });
    }
}
 
Example 15
Source File: JZUtils.java    From JZVideoDemo with MIT License 5 votes vote down vote up
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;
}
 
Example 16
Source File: SwipeRefreshRecyclerView.java    From SimpleAdapterDemo with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 17
Source File: AppearanceHelper.java    From weex-uikit with MIT License 4 votes vote down vote up
public boolean isViewVisible() {
  View view = mAwareChild.getHostView();
  return view != null && view.getLocalVisibleRect(mVisibleRect);

}
 
Example 18
Source File: CardViewActivity.java    From FrostyBackgroundTestApp with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 19
Source File: BaseTestDashboardFragment.java    From bitmask_android with GNU General Public License v3.0 4 votes vote down vote up
static boolean isShownWithinConfinesOfVisibleScreen(View view) {
    Rect scrollBounds = new Rect();
    view.getHitRect(scrollBounds);
    return view.getLocalVisibleRect(scrollBounds);
}
 
Example 20
Source File: RoundRectFrameLayout.java    From EnhancedScreenshotNotification with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void getOutline(View view, Outline outline) {
    final Rect clipPath = new Rect();
    view.getLocalVisibleRect(clipPath);
    outline.setRoundRect(clipPath, mCornerRadius);
}