MagicFingr
MagicFingr

Reputation: 479

Android: imageview using drawable read from assets being scaled to very small size

I'm trying to implement a image gallery, and I'm using a HorizontalScrollView with a LinearLayout, and put the images into the LinearLayout dynamically. And my images are read from subdirectories of assets folder. And I have different size of images.

My layout xml file is below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    tools:context="com.microsoft.stc.indoormonkey.view.ShopDetailActivity">

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="15dp"
        >

        <LinearLayout
            android:id="@+id/layout_infoContainer"
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:orientation="horizontal"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView1"
                android:src="@drawable/z_001"
                android:adjustViewBounds="true"
                android:layout_marginRight="2dp"
                />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView2"
                android:src="@drawable/z_002"
                android:adjustViewBounds="true"
                android:layout_marginRight="2dp"
                />
        </LinearLayout>
    </HorizontalScrollView>
</RelativeLayout>

the two static ImageViews is what I used for test to see what parameters I need to get a proper view. And I copied the two test image files into res folder to access in the xml.

But if I set the layout parameters programmatically like:

private ImageView newImageView(Drawable drawable) {
    ImageView imageView = new ImageView(this);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
    layoutParams.setMargins(0, 0, 2, 0);
    imageView.setLayoutParams(layoutParams);

    imageView.setAdjustViewBounds(true);
    imageView.setImageDrawable(drawable);
    return imageView;
}

The image is somewhat being scaled to a very small size like this. You can see the statically added images display properly but the others not.

The drawable file I used in the newImageView function is read from assets folder. And actually if I change the drawable file draw by:

imageView.setImageDrawable(getResources().getDrawable(R.drawable.z_001));

It shows correctly. So I think the problem must be something related to my drawable file.

I'm read and set images in the onCreate function of an activity, the read code is:

AssetManager assetManager = getResources().getAssets();
try {
    InputStream inputStream = assetManager.open(fileName);
    drawable = Drawable.createFromStream(inputStream, null);
} catch (IOException e) {
    e.printStackTrace();
    Log.e("ERROR", "Invalid drawable file path!");
}

Any advice will be helpful, thanks!

Upvotes: 0

Views: 595

Answers (1)

SGal
SGal

Reputation: 1082

I cannot run the code ATM so its not a one-shot answer but one of these three should do the trick.

First, check if you have appropriate resources for the drawables placed in -ldpi, -mdpi, -hdpi and/or -xhdpi folders or is it in the common /drawables/. Framework might load from a different folder and not scvale it correctly if its in /drawables/.

Second approach would be trying to set android:scaleType for the ImageView. For image galleries its usualy android:scaleType="centerInside".

If all else fail just do it manually, you have all the control then:

Bitmap myImage= BitmapFactory.decodeResource(getResources(), R.drawable.z_001);

//apply some method to calculate scale factor for width based on resolved bitmap size and imageview size or screen size or whatever
float scaleX = calculateWidth(imageView, myImage);
//apply some method to calculate scale factor for height based on resolved bitmap size and imageview size or screen size or whatever 
float scaleY = calculateHeight(imageView, myImage);

// Create a matrix for the scaling and add the scaling data
Matrix matrix = new Matrix();
matrix.postScale(scaleX, scaleY);

Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, myImage.getWidth(), myImage.getHeight(), matrix, true);

BitmapDrawable result = new BitmapDrawable(scaledBitmap);
imageView.setImageDrawable(result);

// If you wish to resize imageView to match scaled drawable 
int width = scaledBitmap.getWidth();
int height = scaledBitmap.getHeight();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams(); 
params.width = width;
params.height = height;
imageView.setLayoutParams(params);

Hope this helps. Cheers!

Upvotes: 1

Related Questions