Example usage for android.widget ImageView getWidth

List of usage examples for android.widget ImageView getWidth

Introduction

In this page you can find the example usage for android.widget ImageView getWidth.

Prototype

@ViewDebug.ExportedProperty(category = "layout")
public final int getWidth() 

Source Link

Document

Return the width of your view.

Usage

From source file:Main.java

public static int[] mapBitmapCoordinatesFromImageView(int posX, int posY, ImageView imageView) {
    Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();

    int ivW = imageView.getWidth();
    int ivH = imageView.getHeight();
    int bW = bitmap.getWidth();
    int bH = bitmap.getHeight();

    int newX = posX * bW / ivW;
    int newH = posY * bH / ivH;

    return new int[] { newX, newH };
}

From source file:Main.java

public static Point calcDecodeSizeHint(ImageView imageView) {
    Point p = new Point();
    LayoutParams params = imageView.getLayoutParams();
    p.x = (params != null) ? params.width : imageView.getWidth();
    p.y = (params != null) ? params.height : imageView.getHeight();
    return p;/*www  .  j av a2 s.c o m*/
}

From source file:Main.java

public static void setImageViewWidthBitmap(String imgPath, ImageView imageView) throws IOException {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   w  w w .j a  v  a 2s .  c  om*/
    BitmapFactory.decodeFile(imgPath, options);
    options.inSampleSize = Math.min(options.outWidth / imageView.getWidth(),
            options.outHeight / imageView.getWidth());
    options.inJustDecodeBounds = false;
    options.inPurgeable = true;
    options.inInputShareable = true;

    FileInputStream fis = new FileInputStream(imgPath);
    imageView.setImageBitmap(BitmapFactory.decodeFileDescriptor(fis.getFD(), null, options));
}

From source file:Main.java

/**
 * {@inheritDoc}/*from w w w .j a va  2  s  . co  m*/
 * <p/>
 * Width is defined by target {@link ImageView view} parameters,
 * configuration parameters or device display dimensions.<br />
 * Size computing algorithm:<br />
 * 1) Get the actual drawn <b>getWidth()</b> of the View. If view haven't
 * drawn yet then go to step #2.<br />
 * 2) Get <b>layout_width</b>. If it hasn't exact value then go to step #3.<br />
 * 3) Get <b>maxWidth</b>.
 */
public static int getImageViewWidth(ImageView imageView) {
    if (imageView != null) {
        final ViewGroup.LayoutParams params = imageView.getLayoutParams();
        int width = 0;
        if (params != null && params.width != ViewGroup.LayoutParams.WRAP_CONTENT) {
            width = imageView.getWidth(); // Get actual image width
        }
        if (width <= 0 && params != null) {
            width = params.width; // Get layout width parameter
        }
        if (width <= 0) {
            width = getImageViewFieldValue(imageView, "mMaxWidth");
        }
        return width;
    }
    return DEFAULT_WIDTH;
}

From source file:Main.java

public static Rect getBitmapRectFromImageView(ImageView imageView) {
    Drawable drawable = imageView.getDrawable();
    Bitmap bitmap = null;// w  ww . ja va 2 s.  com
    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;
    }
}

From source file:com.android.contacts.editor.EditorUiUtils.java

/** Binds the full resolution image at the given Uri to the provided ImageView. */
static void loadPhoto(ContactPhotoManager contactPhotoManager, ImageView imageView, Uri photoUri) {
    final DefaultImageProvider fallbackToPreviousImage = new DefaultImageProvider() {
        @Override//from   ww  w  .j a va 2s.  c o m
        public void applyDefaultImage(ImageView view, int extent, boolean darkTheme,
                DefaultImageRequest defaultImageRequest) {
            // Before we finish setting the full sized image, don't change the current
            // image that is set in any way.
        }
    };
    contactPhotoManager.loadPhoto(imageView, photoUri, imageView.getWidth(), /* darkTheme =*/ false,
            /* isCircular =*/ false, /* defaultImageRequest =*/ null, fallbackToPreviousImage);
}

From source file:android.support.v7.testutils.TestUtilsMatchers.java

/**
 * Returns a matcher that matches <code>ImageView</code>s which have drawable flat-filled
 * with the specific color.// w  w w.j a v a 2 s .c o m
 */
public static Matcher drawable(@ColorInt final int color) {
    return new BoundedMatcher<View, ImageView>(ImageView.class) {
        private String failedComparisonDescription;

        @Override
        public void describeTo(final Description description) {
            description.appendText("with drawable of color: ");

            description.appendText(failedComparisonDescription);
        }

        @Override
        public boolean matchesSafely(final ImageView view) {
            Drawable drawable = view.getDrawable();
            if (drawable == null) {
                return false;
            }

            // One option is to check if we have a ColorDrawable and then call getColor
            // but that API is v11+. Instead, we call our helper method that checks whether
            // all pixels in a Drawable are of the same specified color.
            try {
                TestUtils.assertAllPixelsOfColor("", drawable, view.getWidth(), view.getHeight(), true, color,
                        0, true);
                // If we are here, the color comparison has passed.
                failedComparisonDescription = null;
                return true;
            } catch (Throwable t) {
                // If we are here, the color comparison has failed.
                failedComparisonDescription = t.getMessage();
                return false;
            }
        }
    };
}

From source file:com.tz.explore.util.ImageLoader.java

public void loadImage(ImageView imageView, String path) {
    Bitmap bitmap = cache.get(path);//from w  w  w.j  ava2  s .  c  om
    if (bitmap == null) {
        Messenger messenger = new Messenger(imageView, path, imageView.getWidth(), this);
        TaskPoster poster = new TaskPoster(messenger);
        poster.execute();
    } else {
        imageView.setImageBitmap(bitmap);
    }
}

From source file:my.home.lehome.asynctask.LoadProfileHeaderBgAsyncTask.java

@Override
protected void onPreExecute() {
    ImageView imageView = mImageView.get();
    if (imageView != null) {
        imageView.setImageURI(null);/*from  ww w . j  a v  a2 s.  c om*/
        this.mWidth = imageView.getWidth();
        this.mHeight = imageView.getHeight();
        this.mScaleType = imageView.getScaleType();
    }
    ProgressBar progressBar = mProgressBar.get();
    if (progressBar != null) {
        progressBar.setVisibility(View.VISIBLE);
    }
}

From source file:com.umeng.comm.ui.imagepicker.adapters.ImagePagerAdapter.java

/**
 * ?imagewrap_content,match_parent250</br>
 * //w  ww  . j a v  a 2  s.c o  m
 * @param imageView
 * @return
 */
private Point getSize(ImageView imageView) {
    Point size = new Point();
    if (imageView.getWidth() > 0) {
        size.x = imageView.getWidth();
        size.y = imageView.getHeight();
    } else {
        size.x = size.y = 250;
    }
    return size;
}