开发适用于任何屏幕尺寸的图形应用程序需要一些思考。通常,我创建高分辨率图形,然后按比例缩放以适应屏幕。
我看到了一个建议,避免使用“真实像素”。我尝试使用密度和dp进行工作,但似乎比我使用的解决方案更加复杂。而且,我找不到比使用设备屏幕(真实像素)更好的方法来缩放我的图形。
我创建了这个类来缩放我的图像(基于真实像素)。这解决了我大部分的问题(仍然有一些设备具有不同的纵横比),并且似乎运行良好。
public class BitmapHelper {
// Scale and keep aspect ratio
static public Bitmap scaleToFitWidth(Bitmap b, int width) {
float factor = width / (float) b.getWidth();
return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), false);
}
// Scale and keep aspect ratio
static public Bitmap scaleToFitHeight(Bitmap b, int height) {
float factor = height / (float) b.getHeight();
return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factor), height, false);
}
// Scale and keep aspect ratio
static public Bitmap scaleToFill(Bitmap b, int width, int height) {
float factorH = height / (float) b.getWidth();
float factorW = width / (float) b.getWidth();
float factorToUse = (factorH > factorW) ? factorW : factorH;
return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factorToUse), (int) (b.getHeight() * factorToUse), false);
}
// Scale and dont keep aspect ratio
static public Bitmap strechToFill(Bitmap b, int width, int height) {
float factorH = height / (float) b.getHeight();
float factorW = width / (float) b.getWidth();
return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factorW), (int) (b.getHeight() * factorH), false);
}
}
我的问题是:
- 为什么建议避免使用“真实像素”?
- 缩放位图到屏幕的最佳实践是什么(欢迎提供好的教程文章)?
- 使用我目前的方法有哪些缺点,或者在使用这种方法时需要注意什么?
[编辑] 我忘了提到我通常会在我的应用程序中使用SurfaceView(如果有任何区别)。