如何在Android

时间:2015-08-24 20:24:45

标签: android bitmap

我想创建尺寸与其所在屏幕尺寸相关的图像。基本上我有一个缩放位图50w * 50h。我希望它在小型设备上50w * 50h,在较大的设备上稍微大一点......等等,无论如何都要制作与其屏幕尺寸相关的缩放位图。我的目的是为不同的屏幕宽度制作一个dimen.xml个不同dp值的文件。但它似乎要求很高。有没有更简单的方法呢?我做了一些研究,但没有发现,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

可能是this

 public class BitmapScaler {
     // scale and keep aspect ratio
     public static Bitmap scaleToFitWidth(Bitmap b, int width) {
         float factor = width / (float) b.getWidth();
         return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), true);
     }


     // scale and keep aspect ratio
     public static Bitmap scaleToFitHeight(Bitmap b, int height) {
        float factor = height / (float) b.getHeight();
         return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factor), height, true);
     }


     // scale and keep aspect ratio
     public static 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), true);
      }


     // scale and don't keep aspect ratio
     public static 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), true);
     }
 }

this会对您有所帮助。

相关问题