0

How can I fit any size image coming in from a http request into a ImageView with the size of 100x100.

    android:layout_width="100dp"
    android:layout_height="100dp"

I want to avoid loosing accuracy and I'd like to see the exact same image but in 100x100 size. I have tried with Bitmap.createScaledBitmap(bitmap, 100, 100, true); but failed.

What are the correct steps doing this?

powder366
  • 4,351
  • 7
  • 47
  • 79

2 Answers2

0

Take a look at this:
"This cannot be done in xml only. It is not possible to scale both the image and the ImageView so that image's one dimension would always be 100dp and the ImageView would have the same dimensions as the image.

This code scales Drawable of an ImageView to stay in a square like 100dp x 100dp with one dimension exactly 100dp and keeping the aspect ratio. Then the ImageView is resized to match the dimensions of the scaled image. The code is used in an activity. I tested it via button click handler.

private void scaleImage()
{
// Get the ImageView and its bitmap
ImageView view = (ImageView) findViewById(R.id.image_box);
Drawable drawing = view.getDrawable();
if (drawing == null) {
    return; // Checking for null & return, as suggested in comments
}
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();

// Get current dimensions AND the desired bounding box
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int bounding = dpToPx(250);
Log.i("Test", "original width = " + Integer.toString(width));
Log.i("Test", "original height = " + Integer.toString(height));
Log.i("Test", "bounding = " + Integer.toString(bounding));

// 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) bounding) / width;
float yScale = ((float) bounding) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
Log.i("Test", "xScale = " + Float.toString(xScale));
Log.i("Test", "yScale = " + Float.toString(yScale));
Log.i("Test", "scale = " + Float.toString(scale));

// 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);
width = scaledBitmap.getWidth(); // re-use
height = scaledBitmap.getHeight(); // re-use
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
Log.i("Test", "scaled width = " + Integer.toString(width));
Log.i("Test", "scaled height = " + Integer.toString(height));

// 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);

Log.i("Test", "done");
}

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

The xml code for the ImageView:

<ImageView a:id="@+id/image_box"
a:background="#ff0000"
a:src="@drawable/star"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_marginTop="20dp"
a:layout_gravity="center_horizontal"/>"

source

Community
  • 1
  • 1
Dyna
  • 2,304
  • 1
  • 14
  • 25
  • I updated the answer with a solution that I found here at stackoverflow. Hope that helps ;) * – Dyna Aug 29 '13 at 13:47
0

Check out this link : Fit image into ImageView, keep aspect ratio and then resize ImageView to image dimensions?

And ImageView that is a fixed size, regardless of image size

Try android:scaleType="fitXY" for larger images

Community
  • 1
  • 1
jeydee
  • 49
  • 3