0

I want to load bitmap efficiently, because frequently on 4.0+ android i get OutOfMemoryError. So i tried to use sample code from android developers page resource "Loading bitmaps efficiently".

However my problem is to get height and width of ImageView, because dont have any constant values to define dimensions.

Code with required fields from developer page.

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight)

Here is my layout:

<RelativeLayout android:layout_width="match_parent"
                android:layout_weight="3"
                android:layout_height="0px">

   <ImageView
    android:id="@+id/img1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitStart"
    android:src="@drawable/image" />

I tried to use image.getHeight() but that didnt work.

kort.es
  • 479
  • 2
  • 7
  • 26
  • Should search first ;) [link](http://stackoverflow.com/questions/4142090/how-do-you-to-retrieve-dimensions-of-a-view-getheight-and-getwidth-always-r/4406090#4406090) – Calvin Park Mar 19 '13 at 22:04
  • I have no objections with searching first, but that link isn't going to solve the problem. You have an `ImageView` which you're setting width and height to `wrap_content` meaning "fit the view to the size of the image" but then you're trying to resize the image to the view... Circular logic; it won't work. You need to pick dimensions and size the image to it – kabuko Mar 19 '13 at 22:12

2 Answers2

0

This is because 'image' is not an ImageView object. Here is how to programmatically declare an ImageView in your case :

`ImageView image = (ImageView) findViewById(R.id.img1);

Moreover, this might help you: How to retrieve the dimensions of a view?

Community
  • 1
  • 1
Romain Pellerin
  • 2,470
  • 3
  • 27
  • 36
0

You can use a ViewTreeObserver and wait for one of the callbacks, and then grab the size of the ImageView.

See more here: When Can I First Measure a View?

Community
  • 1
  • 1
DDRBoxman
  • 311
  • 2
  • 10