I’m trying to take an image from the web and have it fill the entire width of the screen. This seems to be working up to doing my calculations which are always returning 0.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Integer width = metrics.widthPixels;
try {
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL("http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif").getContent());
float height =bitmap.getHeight() * (width/bitmap.getWidth());
bitmap = Bitmap.createScaledBitmap(bitmap, width, (int) height, true);
imageView.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
This code will crash the program if because of the line:
float height =bitmap.getHeight() * (width/bitmap.getWidth());
For some reason if the bitmap’s width is greater than the screen width it will always return 0 because the Float isn’t allowing for any decimal places.
An example would be:
Screen Size: 800px; Image Width: 500px; Image Height: 400px;
The new image should then have a width of 800px and height of 640px. Because the float isn’t working correctly instead of having 400 x 1.6 (i.e.800/500), the code is dropping the decimal and doing 400 x 1.
Any clues?
Cast the result of integer devision to float: