DigitalEye
DigitalEye

Reputation: 1565

Images of same dimensions are sized differently by ImageView

I have a frame layout encompassing an imageview that's supposed to update the image it's showing when the user presses the next button (think of a slideshow). All the images that I want to thusly put in this imageview are of the same dimensions. The initial image that's shown by the imageview at the start of the activity is in the res/drawable-hdpi directory, while the other images live in the assets directory. The problem is that the images that I swap into the imageview appear smaller than the first image although all these images are of the same size.

With regard to layout parameters, my frame layout's layout_width and layout_height are set to wrap_content and so is the imageview, I tried setting the scaleType parameter to every possible value and I still see the same results. So unless the images stored in res/drawable-hdpi and assets are somehow scaled differently this behavior shouldn't happen.

So, what's the best approach to preserve the dimensions of the imageview as the user cycles through these images?

Here is my XML for the frame layout & my imageview

<FrameLayout
  android:id="@+id/coverLayout"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true" >

  <ImageView
    android:id="@+id/coverImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="false"
    android:scaleType="fitCenter"
    android:src="@drawable/coverImage" />
</FrameLayout>

Here is my code that loads new images into my imageview

_coverImageView = (ImageView) findViewById(R.id.coverImageView);
...
...
if (loadNewImage) {
        AssetManager assetMgr = getAssets();
        InputStream stream;
        try {
            stream = assetMgr.open("covers/" + _coverImageName);
            Drawable coverDrawable = Drawable.createFromStream(stream, _coverImageName);
            _coverImageView.setImageDrawable(coverDrawable);
        }
        catch (IOException ex) {                        
            System.out.println(ex);
        }
}

Upvotes: 0

Views: 186

Answers (1)

kabuko
kabuko

Reputation: 36302

So unless the images stored in res/drawable-hdpi and assets are somehow scaled differently this behavior shouldn't happen.

This is exactly the issue. Android scales bitmaps based on several rules including the density (class) of the screen and which resource folder the bitmap was from. See this related question/answer. If you want the same behavior for both assets and resources, you can put the initial cover image in drawable-nodpi instead of drawable-hdpi.

Upvotes: 2

Related Questions