Я хотел бы масштабировать a Bitmap
до зависящей от времени ширины и высоты, где поддерживается соотношение сторон, и Bitmap
заполняет всю ширину и центрирует изображение по вертикали, либо обрезая избыток, либо заполняя пробел с 0 альфа-пикселями.
В настоящее время я перерисовываю растровое изображение, создавая Bitmap
всех 0 альфа-пикселей и рисуя изображение Bitmap
поверх него, масштабируя его до указанной указанной ширины и сохраняя соотношение сторон, однако оно заканчивается потерять/закрутить пиксельные данные.
Вот как я это делаю:
Bitmap background = Bitmap.createBitmap((int)width, (int)height, Config.ARGB_8888);
float originalWidth = originalImage.getWidth(), originalHeight = originalImage.getHeight();
Canvas canvas = new Canvas(background);
float scale = width/originalWidth;
float xTranslation = 0.0f, yTranslation = (height - originalHeight * scale)/2.0f;
Matrix transformation = new Matrix();
transformation.postTranslate(xTranslation, yTranslation);
transformation.preScale(scale, scale);
canvas.drawBitmap(originalImage, transformation, null);
return background;
Есть ли там библиотека или какой-то лучший код, который может сделать это лучше? Я хотел бы, чтобы изображение выглядело максимально четким, но я знал, что моя функция не даст отличного результата.
Я знаю, что у меня могло бы быть хорошее изображение, используя масштабирование по целому числу, вместо масштабирования по платам, но мне нужно, чтобы ширина была заполнена на 100%.
Кроме того, я знаю о возможностях ImageView
Gravity.CENTER_CROP
, однако также использует целочисленное масштабирование, поэтому он не разрешает ширину изображения, если это не так.