问题
So, I'm scaling down bitmap files so that I can display images without going over the memory size. I referred to the android tutorial here.
What I found out was that not only is this scaling down my image so the size won't be too big but it is also cutting a portion of the image off. Or so it seems. I only want the image to take up a part of the screen. How can I scale down the bitmap and display the whole image?
If you could provide any help, ideas, or suggestions I'll be very thankful! And if I seem to be making a simple mistake, please point it out to me. Thanks!
Here's the code:
String fileName = data.getStringExtra("file");
image.setImageBitmap(decodeSampledBitmapFromFile(fileName, screenWidth, screenHeight / 2));
public static Bitmap decodeSampledBitmapFromFile(String file, int reqWidth, int reqHeight){
//first decode with inJustDecodeBounds = true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, options);
//calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
//decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
System.gc();
return BitmapFactory.decodeFile(file, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight){
//raw height and width of image
final int imageHeight = options.outHeight;
final int imageWidth = options.outWidth;
int inSampleSize = 1;
if (imageHeight > reqHeight || imageWidth > reqWidth){
if (imageWidth > imageHeight){
inSampleSize = Math.round((float) imageHeight / (float) reqHeight);
}else{
inSampleSize = Math.round((float) imageWidth / (float) reqWidth);
}//end of second if statement
}//end of first if statement
return inSampleSize;
}//end of calculateInSampleSize
public void getDisplaySize(){
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
//get the screen dimensions
try{
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
buttonHeight = view.getHeight();
screenLength = (int) (screenHeight / 5);
imageWidth = (int) (screenWidth / 4);
}catch (NoSuchMethodError e){
//for older versions of android
screenWidth = display.getWidth();
screenHeight = display.getHeight();
buttonHeight = view.getHeight();
screenLength = (int) (screenHeight / 5);
imageWidth = (int) (screenWidth / 4);
}//end of try/catch block
}//end of getDisplaySize() method
来源:https://stackoverflow.com/questions/13203957/scaling-down-bitmap-causes-portion-of-image-not-visible