0

I work out the width and height of my fragment and scale its image to a specific percentage of that fragment. This works for images that need to be scaled up to meet that size but larger images seem to ignore the scales (i think they shrink a bit but not to corretc size).

I get my imahes via http asyncTask call then on onPostexecute set the imageView control src and scale the imageView. Work for smaller images, not larger ones.

The larger image is 10kb, smaller is 1kb.

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
        if (result != null) {
            int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, 35, getContext().getResources().getDisplayMetrics());
            int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, 35, getContext().getResources().getDisplayMetrics());

            bmImage.setMinimumWidth(width);
            bmImage.setMinimumHeight(height);
            bmImage.setMaxWidth(width);
            bmImage.setMaxHeight(height);

        }

I see the dimensions being calc'c correctly and afterwards set correctly in the imageView )(minimum and maxheight) but the mDrawable attr is big so perhaps this is an indicator of worng attr being set?

Fearghal
  • 10,569
  • 17
  • 55
  • 97
  • You need to use a custom ImageView. Are your images is in rectangular shape? – Ajeet Apr 02 '15 at 09:13
  • yes, they are pngs with transparent backgrounds to make it look like they are diff shapes. Why would it ignor setMaxHeight and not SetMinimumHeight? – Fearghal Apr 02 '15 at 09:45
  • Neat solution pasted, worked a treat and its wrapped in a class to boot :) – Fearghal Apr 02 '15 at 10:12

1 Answers1

0

https://argillander.wordpress.com/2011/11/24/scale-image-into-imageview-then-resize-imageview-to-match-the-image/

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);
}
Fearghal
  • 10,569
  • 17
  • 55
  • 97