1

I'm using the HorizontalScrollView and I want to put images inside it but images will be larger than the screen and makes users scroll horizontally while I'm using setScaleType(ImageView.ScaleType.CENTER_CROP) or other attributes. How can I have images completely fit the screen? I prefer to use XML attributes if it is possible.

activity_main.xml

<HorizontalScrollView
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"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/main"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal">

</LinearLayout>

MainActivity.java

ImageView image = new ImageView(this);
image.setImageResource(getResources().getIdentifier('file_name', "drawable", getPackageName()));
image.setAdjustViewBounds(true);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
image.setLayoutParams(layoutParams);
parent.addView(image);
Alex
  • 1,623
  • 1
  • 24
  • 48
  • Maybe this could help you if i understood correctly what is that you need: http://stackoverflow.com/a/18077714/1234624 – Bojan Kopanja Jun 04 '15 at 09:32
  • you can use `android:scaleType="fitXY"` but it will not maintain aspect ratio . – Spring Breaker Jun 04 '15 at 09:44
  • @SpringBreaker Thanks but I already said I tested all attributes including `fitXY` and they don't work as I want. All attributes scale images wider than the screen. – Alex Jun 04 '15 at 11:23

1 Answers1

1

You can use this xml property into your ImageView,

android:scaleType="fitXY"

Or use this dynamic code snippet to fit the ImageView accordingly the screen ratio,

private void scaleImage(ImageView view, int boundBoxInDp)
{
    // Get the ImageView and its bitmap
    Drawable drawing = view.getDrawable();
    Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();

    // Get current dimensions
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.
    float xScale = ((float) boundBoxInDp) / width;
    float yScale = ((float) boundBoxInDp) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;

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

    // Create a new bitmap and convert it to a format understood by the ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    width = scaledBitmap.getWidth();
    height = scaledBitmap.getHeight();

    // Apply the scaled bitmap
    view.setImageDrawable(result);

    // Now change ImageView's dimensions to match the scaled image
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);
}

private int dpToPx(int dp)
{
    float density = getApplicationContext().getResources().getDisplayMetrics().density;
    return Math.round((float)dp * density);
}

And sample code for testing:

ImageView image = new ImageView(this);
scaleImage(image , 250); // in dp

Here's the output,

Before,

enter image description here

After,

enter image description here

Source: Scale image into ImageView, then resize ImageView to match the image

Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50