Q: Scale images to completely fit the screen 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);


A: 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,

After,

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