我正在制作新应用,它基于文字和小图片。但我希望这些文本看起来不错,并且用我的字体格式化。所以我在photoshop中写了它并提取为png。但是我在一个活动和布局中有大约15个这样的图像。他们有800x3980的分辨率。当我将其重新制作成较低的分辨率时,它并不清晰,而且不可读,但是当我制作高分辨率图像时,应用程序崩溃了,因为内存充足。
那么有人有任何建议让它变得更小但更敏锐吗?
答案 0 :(得分:0)
private static Integer DEFAULT_WIDTH = {Your value};
private static Integer DEFAULT_HEIGHT = {Your value};
private static Integer DEFAULT_LARGE_WIDTH = {Your value};
private static Integer DEFAULT_LARGE_HEIGHT = {Your value};
public static Bitmap reduceImageQuality(String path, Integer width,
Integer height, Context context) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
if (width == null || height == null) {
if (InputOutput.isLargeScreen(context)) {
options.inSampleSize = calculateInSampleSize(options,
DEFAULT_LARGE_WIDTH, DEFAULT_LARGE_HEIGHT);
} else {
options.inSampleSize = calculateInSampleSize(options,
DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
} else {
options.inSampleSize = calculateInSampleSize(options, width, height);
}
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
return bitmap;
}
public static int calculateInSampleSize(BitmapFactory.Options options,
Integer reqWidth, Integer 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;
}
public static boolean isLargeScreen(Context context) {
int screenSize = context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK;
if (screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE
|| screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
return true;
}
return false;
}
您可以使用inSampleSize选项来降低质量。但800x3980的分辨率太高了。是不是可以调整它并将其放在可绘制文件夹中然后降低性能质量?我完全会推荐这个。
编辑:我添加了一种方法来检查屏幕是否很大。我初始化了图像的默认宽度和高度值。如果您的图像在任何地方都具有相同的witdh和height,那么您可以在reduceImageQuality方法中删除第2和第3个参数。您可以直接使用默认值。
然后将您的原始图像放在drawable-x drawable-xx中,并将调整后的图像放入其他图像