blob: 4c18b06fe8439f917d493b41b448ff7700259e28 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package cgeo.geocaching.utils;
import cgeo.geocaching.cgeoapplication;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.Display;
import android.view.WindowManager;
public class ImageHelper {
// Do not let this class be instantiated, this is a utility class.
private ImageHelper() {
}
/**
* Scales a bitmap to the given bounds
*
* @param image
* The bitmap to scale
* @return BitmapDrawable The scaled image
*/
public static BitmapDrawable scaleBitmapToFitDisplay(final Bitmap image) {
final cgeoapplication app = cgeoapplication.getInstance();
final Display display = ((WindowManager) app.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
final int maxWidth = display.getWidth() - 25;
final int maxHeight = display.getHeight() - 25;
Bitmap result = image;
int width = image.getWidth();
int height = image.getHeight();
if (width > maxWidth || height > maxHeight) {
final double ratio = Math.min((double) maxHeight / (double) height, (double) maxWidth / (double) width);
width = (int) Math.ceil(width * ratio);
height = (int) Math.ceil(height * ratio);
result = Bitmap.createScaledBitmap(image, width, height, true);
}
final BitmapDrawable resultDrawable = new BitmapDrawable(app.getResources(), result);
resultDrawable.setBounds(new Rect(0, 0, width, height));
return resultDrawable;
}
}
|