0

I need to downscale my images (+- 2500px one side) to show them on my devices in my app. But I've got one problem. If I downscale the images with my code (a bit further down) the images are displayed but the side is cut-off. The height of the image is perfect with my code. I'm downscaling my images with this code:

static public Bitmap scaleToFill(Bitmap b, int width, int height) {
    float factorH = height / (float) b.getWidth();
    float factorW = width / (float) b.getWidth();
    float factorToUse = (factorH > factorW) ? factorW : factorH;
    return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factorToUse), (int) (b.getHeight() * factorToUse), false);
}

The height and width I'm getting with this:

        WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

The image is viewed in a ImageView with ScaleType CENTER_CROP. I hope someone can help me with this! Thanks in advance!

Marc
  • 1,094
  • 3
  • 17
  • 38
  • Have you tried setting `CENTER_INSIDE` on the ImageView instead of `CENTER_CROP`? – Jave Aug 20 '13 at 08:22
  • Jave, that could work, but if the image needs to be made larger then it won't work anymore. – Marc Aug 20 '13 at 08:50
  • Why would it not work? The imageview does not change the size of the source file. The only thing `CENTER_INSIDE` does is making sure that the displayed image fits in the view, while keeping its proportions. – Jave Aug 20 '13 at 08:52
  • And if I dont scale it down its a outofmemory exception. So I do need to scale it down. – Marc Aug 20 '13 at 09:18
  • Yes, but if the image aspect ratio is not the same as the screen ratio, the sides will be cut of when you use `CENTER_CROP`, If you instead use `CENTER_INSIDE` (on your scaled image) the image will be placed so the entirety is visible. For a list of all possible scale types, please see http://developer.android.com/reference/android/widget/ImageView.ScaleType.html – Jave Aug 20 '13 at 09:38

2 Answers2

0

In layout file - inside image view

<ImageView
 android:scaleType="fitXY"
/>

try with thsi code.
abc
  • 74
  • 9
0

this question is much more general than you think. it has nothing to do with downscaling or android.

if you wish to keep the aspect ratio , you have to choose to either have empty space, or show only a part of the image.

the same problem occurs on TVs , where the aspect ratio of the screen is different from the one being sent to it.

there is also an experimental way to resize images (seam carving) but it doesn't always work well and depends on the image itself.

btw, the way you downscale is very inefficient, as it uses a lot of memory (first you decode the original bitmap, and then you create a new one that is a downscaled version of the original bitmap). you should try out either google's way or my way .

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • I looked at your way of doing it, it looks good. But what do I need to fill in at the sampleSize? Do I need to calculate it? If so how? – Marc Aug 20 '13 at 09:21
  • Just because the image needs to stay clear (not blurry) on even 10 inch devices. – Marc Aug 20 '13 at 09:27
  • sample size should be calculated . that's true. the number should tell the algorithm how much you need to downscale the bitmap. for example , if you set it to 2 , and the image is 1000x1000, the output would be 500x500 . you can find an algorithm to calculate the sample size here: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap . about the second post you've written, i don't understand ... do note that my method might be a bit slower than google's way, but you could make it work even with fractions instead of integers (not supported , but possible). – android developer Aug 20 '13 at 09:57
  • Okay, I understand this. But I don't know how big the screen is. The ImageView is stretched over the whole screen. And because the width is fill_parent I can't get the width to calculate my SampleSize, do you have any ideas? – Marc Aug 20 '13 at 11:43
  • yes, but this is going quite far from the question you've asked, so you can search about it using the Google search engine. in this case you can search "android get screen resolution", and find that you can use getResources().getDisplayMetrics() :) . btw, use match_parent as fill_parent is deprecated (even though both are exactly the same). – android developer Aug 20 '13 at 12:05