Reputation: 524
I have images that have been taken from different cameras, with diff resolutions and are of diff sizes.
Now i have to display these images within a image View. Now if i fix the size of the image View to 300dp by 300dp the image gets compressed and looks distorted.
At the same time if make it wrap to content the sizes differ.
Whats the best approach ?
Upvotes: 0
Views: 86
Reputation: 21066
Use one of the image loading libraries: Glide, Square Picasso, UIL or Ion (Ion is actually more of a general purpose async loading tool) to do the dirty work for you. All of them have methods that can fit you image into the needed size while loading.
Using vanilla code, you probably want to set either android:scaleType="centerCrop"
or android:scaleType="centerInside"
depending on whether you want the "touch from inside" or "fit all" behavior, respectively.
Upvotes: 1
Reputation: 6707
In your ImageView, change your width and height according to what you want and make your scale type to fit x (width) and y (height). This will stretch your image to your defined width and height:
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/imageView"
android:src="@drawable/my_image"
android:scaleType="fitXY" /> <!-- Add this -->
If you are doing this programmatically, do it like this:
imageView.setImageResource(R.drawable.my_image);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
Take a look at this, if you don't want to use fitXY
: http://etcodehome.blogspot.com/2011/05/android-imageview-scaletype-samples.html
You may need to use centerInside
in case you don't want your image to be stretched, and change your image's height and width according to what you want.
Upvotes: 0
Reputation: 183
Try this code give height and width according to your requirement.
BitmapFactory.Options bitopt = new BitmapFactory.Options();
bitopt.inJustDecodeBounds = true;
Bitmap bit = BitmapFactory.decodeFile(file, bitopt);
int h = (int) Math.ceil(bitopt.outHeight / (float) height);
int w = (int) Math.ceil(bitopt.outWidth / (float) width);
if (h > 1 || w > 1) {
if (h > w) {
bitopt.inSampleSize = h;
} else {
bitopt.inSampleSize = w;
}
}
bitopt.inJustDecodeBounds = false;
bit = BitmapFactory.decodeFile(file, bitopt);
return bit;
}
In XML set height width to imageview and set scaletype to fitXY
Upvotes: 0
Reputation: 915
This is what I usually use to resize bitmaps :
public Bitmap decodeSampledBitmap(File res, int reqWidth, int reqHeight) {
if (res != null) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
FileInputStream stream2 = new FileInputStream(res);
BitmapFactory.decodeStream(stream2, null, options);
stream2.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Calculate inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
o2.inJustDecodeBounds = false;
FileInputStream stream = null;
try {
stream = new FileInputStream(res);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, o2);
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
} else
return null;
}
public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
Upvotes: 0