4
votes

I have two goals I am trying to achieve.

  1. Scale a bitmap proportionately.
  2. Scale the same bitmap so that its height is equal to the device's height.

When attempting this, I find that on devices with less pixels, the scaling works fine, but on devices with more pixels, the scaling forces the bitmap to reach over the 4096x4096 pixel limit:

W/OpenGLRenderer(10630): Bitmap too large to be uploaded into a texture (4476x885, max=4096x4096)

As of now, I am using Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height) to scale my bitmaps:

float conversion = (float) view.getHeight() / (float) originalBitmap.getHeight();

Matrix mat = new Matrix();
mat.postScale(conversion,conversion);

Bitmap resizedBitmap =
     Bitmap.createBitmap(originalBitmap,0,0,originalBitmap.getWidth(),
     originalBitmap.getHeight(), mat, false);
imageView.setImageBitmap(resize);

The above code coupled with the following xml allows for the bitmap to scale the entire screen:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:scaleType="matrix"/>

If there are any other ways to proportionality scale a bitmap to the entire screen, I would greatly appreciate those suggestions.

1

1 Answers

4
votes

Use Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter); where dstWidth and dstHeight are the desired widths and heights, respectively. (The filter just tries to smooth edges if it is true, and doesn't if it is false, as seen here.) This does not require a Matrix parameter or a desired position, it just returns the scaled bitmap.

The following would probably work for you:

Bitmap resizedBitmap = 
     Bitmap.createScaledBitmap(originalBitmap, 
     originalBitmap.getWidth() * (view.getHeight() / originalBitmap.getHeight()),
     view.getHeight());
imageView.setImageBitmap(resizedBitmap);

You also probably only need this much of the XML now:

<ImageView
     android:id="@+id/imageView1"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />

That's because you don't need to center something that takes up the whole of its parent, and you aren't using a matrix to scale anymore.