Java Code Examples for android.widget.ImageView#getLocationOnScreen()

The following examples show how to use android.widget.ImageView#getLocationOnScreen() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
private void animateClose(ImageView avatar, Rect ori) {

        if (ori == null) {
            return;
        }

        int[] avatarLocation = new int[2];
        avatar.getLocationOnScreen(avatarLocation);

        final int transX = ori.left - avatarLocation[0];
        final int transY = ori.top - avatarLocation[1];

        final float scaleX = (float) ori.width() / (float) avatar.getWidth();
        final float scaleY = (float) ori.height() / (float) avatar.getHeight();

        avatar.animate().translationX(transX).translationY(transY).scaleY(scaleY).scaleX(scaleX).alpha(0.7f).rotationY(0f)
                .setDuration(300)
                .setListener(new MyAnimationListener(new Runnable() {
                    @Override
                    public void run() {
                        dismissAllowingStateLoss();
                    }
                }));
    }
 
Example 2
private void animateClose(ImageView avatar, Rect ori) {

        if (ori == null) {
            return;
        }

        int[] avatarLocation = new int[2];
        avatar.getLocationOnScreen(avatarLocation);

        final int transX = ori.left - avatarLocation[0];
        final int transY = ori.top - avatarLocation[1];

        final float scaleX = (float) ori.width() / (float) avatar.getWidth();
        final float scaleY = (float) ori.height() / (float) avatar.getHeight();

        avatar.animate().translationX(transX).translationY(transY).scaleY(scaleY).scaleX(scaleX).alpha(0.7f).rotationY(0f)
                .setDuration(300)
                .setListener(new MyAnimationListener(new Runnable() {
                    @Override
                    public void run() {
                        dismissAllowingStateLoss();
                    }
                }));
    }
 
Example 3
Source Project: FragmentRigger   File: ImageViewUtil.java    License: MIT License 4 votes vote down vote up
/**
 * Returns the bitmap position inside an imageView.
 *
 * @param imageView source ImageView
 *
 * @return 0: left, 1: top, 2: width, 3: height
 */
public static int[] getDisplayedImageLocation(ImageView imageView) {
  int[] ret = new int[4];

  if (imageView == null || imageView.getDrawable() == null)
    return ret;

  // Get image dimensions
  // Get image matrix values and place them in an array
  float[] f = new float[9];
  imageView.getImageMatrix().getValues(f);

  // Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY)
  final float scaleX = f[Matrix.MSCALE_X];
  final float scaleY = f[Matrix.MSCALE_Y];

  // Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight)
  final Drawable d = imageView.getDrawable();
  final int origW = d.getIntrinsicWidth();
  final int origH = d.getIntrinsicHeight();

  // Calculate the actual dimensions
  final int actW = Math.round(origW * scaleX);
  final int actH = Math.round(origH * scaleY);

  ret[2] = actW;
  ret[3] = actH;

  // Get image position
  // We assume that the image is centered into ImageView
  int imgViewW = imageView.getWidth();
  int imgViewH = imageView.getHeight();

  int[] imgViewScreenLoc = new int[2];
  imageView.getLocationOnScreen(imgViewScreenLoc);

  // get the actual image location inside its image view
  int left = imgViewScreenLoc[0] + (imgViewW - actW) / 2;
  int top = imgViewScreenLoc[1] + (imgViewH - actH) / 2;

  ret[0] = left;
  ret[1] = top;

  return ret;
}
 
Example 4
@Test
public void testPinnedToolbarAndParallaxImage() throws Throwable {
  configureContent(
      R.layout.design_appbar_toolbar_collapse_with_image,
      R.string.design_appbar_collapsing_toolbar_with_image);

  final ImageView parallaxImageView = mCoordinatorLayout.findViewById(R.id.app_bar_image);

  // We have not set any padding on the ImageView, so ensure that none is set via
  // window insets handling
  assertEquals(0, parallaxImageView.getPaddingLeft());
  assertEquals(0, parallaxImageView.getPaddingTop());
  assertEquals(0, parallaxImageView.getPaddingRight());
  assertEquals(0, parallaxImageView.getPaddingBottom());

  CollapsingToolbarLayout.LayoutParams parallaxImageViewLp =
      (CollapsingToolbarLayout.LayoutParams) parallaxImageView.getLayoutParams();
  assertEquals(
      CollapsingToolbarLayout.LayoutParams.COLLAPSE_MODE_PARALLAX,
      parallaxImageViewLp.getCollapseMode());

  final float parallaxMultiplier = parallaxImageViewLp.getParallaxMultiplier();

  final int[] appbarOnScreenXY = new int[2];
  final int[] parallaxImageOnScreenXY = new int[2];
  final int toolbarHeight = mToolbar.getHeight();

  mAppBar.getLocationOnScreen(appbarOnScreenXY);
  parallaxImageView.getLocationOnScreen(parallaxImageOnScreenXY);

  final int originalAppbarTop = appbarOnScreenXY[1];
  final int originalAppbarBottom = appbarOnScreenXY[1] + mAppBar.getHeight();
  final int originalParallaxImageTop = parallaxImageOnScreenXY[1];
  final int centerX = appbarOnScreenXY[0] + mAppBar.getWidth() / 2;

  // Test that at the beginning our image is top-aligned with the app bar
  assertEquals(appbarOnScreenXY[1], parallaxImageOnScreenXY[1]);

  // Swipe up by the toolbar's height
  performVerticalSwipeUpGesture(
      R.id.coordinator_layout, centerX, originalAppbarBottom, toolbarHeight);

  // Test that the top edge of the image (in the screen coordinates) has "moved" by half
  // the amount that the top edge of the app bar (in the screen coordinates) has.
  mAppBar.getLocationOnScreen(appbarOnScreenXY);
  parallaxImageView.getLocationOnScreen(parallaxImageOnScreenXY);
  assertEquals(
      parallaxMultiplier * (appbarOnScreenXY[1] - originalAppbarTop),
      parallaxImageOnScreenXY[1] - originalParallaxImageTop,
      1);

  // Swipe up by another toolbar's height
  performVerticalSwipeUpGesture(
      R.id.coordinator_layout, centerX, originalAppbarBottom, toolbarHeight);

  // Test that the top edge of the image (in the screen coordinates) has "moved" by half
  // the amount that the top edge of the app bar (in the screen coordinates) has.
  mAppBar.getLocationOnScreen(appbarOnScreenXY);
  parallaxImageView.getLocationOnScreen(parallaxImageOnScreenXY);
  assertEquals(
      parallaxMultiplier * (appbarOnScreenXY[1] - originalAppbarTop),
      parallaxImageOnScreenXY[1] - originalParallaxImageTop,
      1);

  // Swipe down by a different value (150% of the toolbar's height) to test parallax going the
  // other way
  performVerticalSwipeDownGesture(
      R.id.coordinator_layout, centerX, originalAppbarBottom, 3 * toolbarHeight / 2);

  // Test that the top edge of the image (in the screen coordinates) has "moved" by half
  // the amount that the top edge of the app bar (in the screen coordinates) has.
  mAppBar.getLocationOnScreen(appbarOnScreenXY);
  parallaxImageView.getLocationOnScreen(parallaxImageOnScreenXY);
  assertEquals(
      parallaxMultiplier * (appbarOnScreenXY[1] - originalAppbarTop),
      parallaxImageOnScreenXY[1] - originalParallaxImageTop,
      1);
}
 
Example 5
static void showPopup(ImageView helpIcon, int titleStringId, Activity activity, final Dialog dialog, String helpString) {
    if (!activity.isFinishing()) {
        DialogHelpPopupWindowX popup = new DialogHelpPopupWindowX(titleStringId, activity, dialog, helpString);

        View contentView = popup.getContentView();
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = contentView.getMeasuredWidth();
        int popupHeight = contentView.getMeasuredHeight();
        /*if (PPApplication.logEnabled()) {
            PPApplication.logE("DialogHelpPopupWindowX.showPopup", "popupWidth=" + popupWidth);
            PPApplication.logE("DialogHelpPopupWindowX.showPopup", "popupHeight=" + popupHeight);
        }*/

        ViewGroup activityView = activity.findViewById(android.R.id.content);
        int activityHeight = activityView.getHeight();
        //int activityWidth = activityView.getWidth();
        //PPApplication.logE("DialogHelpPopupWindowX.showPopup","activityHeight="+activityHeight);

        //int[] activityLocation = new int[2];
        //_eventStatusView.getLocationOnScreen(location);
        //activityView.getLocationInWindow(activityLocation);

        int[] locationHelpIcon = new int[2];
        helpIcon.getLocationOnScreen(locationHelpIcon); // must be used this in dialogs.
        //helpIcon.getLocationInWindow(locationHelpIcon);
        /*if (PPApplication.logEnabled()) {
            PPApplication.logE("DialogHelpPopupWindowX.showPopup", "locationHelpIcon[0]=" + locationHelpIcon[0]);
            PPApplication.logE("DialogHelpPopupWindowX.showPopup", "locationHelpIcon[1]=" + locationHelpIcon[1]);
        }*/

        int x = 0;
        int y = 0;

        if (locationHelpIcon[0] + helpIcon.getWidth() - popupWidth < 0)
            x = -(locationHelpIcon[0] + helpIcon.getWidth() - popupWidth);

        if ((locationHelpIcon[1] + popupHeight) > activityHeight)
            y = -(locationHelpIcon[1] - (activityHeight - popupHeight));

        /*if (PPApplication.logEnabled()) {
            PPApplication.logE("DialogHelpPopupWindowX.showPopup", "x=" + x);
            PPApplication.logE("DialogHelpPopupWindowX.showPopup", "y=" + y);
        }*/

        popup.setClippingEnabled(false); // disabled for draw outside activity
        popup.showOnAnchor(helpIcon, VerticalPosition.ALIGN_TOP,
                HorizontalPosition.ALIGN_RIGHT, x, y, false);
    }
}
 
Example 6
public static Rect getBitmapRectFromImageView(ImageView imageView) {
    Drawable drawable = imageView.getDrawable();
    Bitmap bitmap = null;
    if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    }

    Rect rect = new Rect();
    boolean isVisible = imageView.getGlobalVisibleRect(rect);
    if (!isVisible) {
        int[] location = new int[2];
        imageView.getLocationOnScreen(location);

        rect.left = location[0];
        rect.top = location[1];
        rect.right = rect.left + imageView.getWidth();
        rect.bottom = rect.top + imageView.getHeight();
    }

    if (bitmap != null) {

        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();

        int imageViewWidth = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
        int imageviewHeight = imageView.getHeight() - imageView.getPaddingTop() - imageView.getPaddingBottom();

        float startScale;
        if ((float) imageViewWidth / bitmapWidth > (float) imageviewHeight / bitmapHeight) {
            // Extend start bounds horizontally
            startScale = (float) imageviewHeight / bitmapHeight;

        } else {
            startScale = (float) imageViewWidth / bitmapWidth;

        }

        bitmapHeight = (int) (bitmapHeight * startScale);
        bitmapWidth = (int) (bitmapWidth * startScale);

        int deltaX = (imageViewWidth - bitmapWidth) / 2;
        int deltaY = (imageviewHeight - bitmapHeight) / 2;

        rect.set(rect.left + deltaX, rect.top + deltaY, rect.right - deltaX, rect.bottom - deltaY);

        return rect;
    } else {
        return null;
    }

}
 
Example 7
public static Rect getBitmapRectFromImageView(ImageView imageView) {
    Drawable drawable = imageView.getDrawable();
    Bitmap bitmap = null;
    if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    }

    Rect rect = new Rect();
    boolean isVisible = imageView.getGlobalVisibleRect(rect);
    if (!isVisible) {
        int[] location = new int[2];
        imageView.getLocationOnScreen(location);

        rect.left = location[0];
        rect.top = location[1];
        rect.right = rect.left + imageView.getWidth();
        rect.bottom = rect.top + imageView.getHeight();
    }

    if (bitmap != null) {

        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();

        int imageViewWidth = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
        int imageviewHeight = imageView.getHeight() - imageView.getPaddingTop() - imageView.getPaddingBottom();

        float startScale;
        if ((float) imageViewWidth / bitmapWidth > (float) imageviewHeight / bitmapHeight) {
            // Extend start bounds horizontally
            startScale = (float) imageviewHeight / bitmapHeight;

        } else {
            startScale = (float) imageViewWidth / bitmapWidth;

        }

        bitmapHeight = (int) (bitmapHeight * startScale);
        bitmapWidth = (int) (bitmapWidth * startScale);

        int deltaX = (imageViewWidth - bitmapWidth) / 2;
        int deltaY = (imageviewHeight - bitmapHeight) / 2;

        rect.set(rect.left + deltaX, rect.top + deltaY, rect.right - deltaX, rect.bottom - deltaY);

        return rect;
    } else {
        return null;
    }

}