Почему изображение обрезается сверху?
Код работает, но изображения обрезаются сверху. Я перепробовал все, но до сих пор не могу понять.
Может кто-нибудь взглянуть на это? Заранее спасибо.
Код:
public class ScalingUtilities {
public static Bitmap decodeResource(Resources res, int resId, int ReqWidth, int ReqHeight, ScalingLogic scalingLogic) {
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, ReqWidth,
ReqHeight, scalingLogic);
Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resId, options);
return unscaledBitmap;
}
public static Bitmap decode_imagePath_String(String image_path, int ReqWidth, int ReqHeight, ScalingLogic scalingLogic) {
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image_path, options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, ReqWidth,
ReqHeight, scalingLogic);
Bitmap unscaledBitmap = BitmapFactory.decodeFile(image_path, options);
return unscaledBitmap;
}
public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int ReqWidth, int ReqHeight,
ScalingLogic scalingLogic) {
Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
ReqWidth, ReqHeight, scalingLogic);
Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
ReqWidth, ReqHeight, scalingLogic);
Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(),
Config.RGB_565);
Canvas canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
public static enum ScalingLogic {
CROP, FIT
}
public static int calculateSampleSize(int srcWidth, int srcHeight, int ReqWidth, int ReqHeight,
ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
final float srcAspect = (float)srcWidth / (float)srcHeight;
final float dstAspect = (float)ReqWidth / (float)ReqHeight;
if (srcAspect > dstAspect) {
return srcWidth / ReqWidth;
} else {
return srcHeight / ReqHeight;
}
} else {
final float srcAspect = (float)srcWidth / (float)srcHeight;
final float dstAspect = (float)ReqWidth / (float)ReqHeight;
if (srcAspect > dstAspect) {
return srcHeight / ReqHeight;
} else {
return srcWidth / ReqWidth;
}
}
}
public static Rect calculateSrcRect(int srcWidth, int srcHeight, int ReqWidth, int ReqHeight,
ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.CROP) {
final float srcAspect = (float)srcWidth / (float)srcHeight;
final float dstAspect = (float)ReqWidth / (float)ReqHeight;
if (srcAspect > dstAspect) {
final int srcRectWidth = (int)(srcHeight * dstAspect);
final int srcRectLeft = (srcWidth - srcRectWidth) / 2;
return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, srcHeight);
} else {
final int srcRectHeight = (int)(srcWidth / dstAspect);
final int scrRectTop = (int)(srcHeight - srcRectHeight) / 2;
return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight);
}
} else {
return new Rect(0, 0, srcWidth, srcHeight);
}
}
public static Rect calculateDstRect(int srcWidth, int srcHeight, int ReqWidth, int ReqHeight,
ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
final float srcAspect = (float)srcWidth / (float)srcHeight;
final float dstAspect = (float)ReqWidth / (float)ReqHeight;
if (srcAspect > dstAspect) {
return new Rect(0, 0, ReqWidth, (int)(ReqWidth / srcAspect));
} else {
return new Rect(0, 0, (int)(ReqHeight * srcAspect), ReqHeight);
}
} else {
return new Rect(0, 0, ReqWidth, ReqHeight);
}
}
}
Я очень ценю это...
1 ответ
В CalculateSize, вы возвращаете int. В зависимости от соотношения вашего изображения это возвращаемое значение вполне может быть 0, а options.inSampleSize будет 0, поэтому масштабирование не будет.
Затем декодирование будет декодироваться с использованием переданных вами значений размера, поэтому произойдет обрезка. Я думаю, что вам нужно использовать параметры плотности вместо. Пожалуйста, посмотрите на этот пост