Search code examples
javaandroidxmlimageviewcrop

scaleType = centerCrop dynamically


I have a gallery in my app. My photos are from the internet, so they are added dynamically. My gallery doesn't crop them to all be a perfect square. I would like to do this just to make my gallery look better. I tried this in my XML;

 ImageView
        android:id="@+id/phone"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="15dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"/>

But because I am adding the image dynamically and not from src in xml, this wont work. Anyone know how to do this another way? I cant find anything on how to do it when added dynamically. Thanks in advance.


Solution

  • If you need to scale the images dynamically you can use scaled bitmap method of bitmap to specifying size of your scaled down/up image programaticaly. Something like this:

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(unscaledBitmap, wantedWidth, wantedHeight, true);

    For scaling details you can follow below link:

    http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/