Android 动态生成不同分辨率的values文件

public class Main {

   static final int width_size  = 750;  // 设定尺寸 640x1136  750x1334
   static final int height_size = 1334; // 设定尺寸 640x1136  750x1334

   public static void main(String[] args) {
      String pathname = "";

      // 屏幕 宽(x),(y) 像素
      int[] list_x = {320, 480, 480, 540, 600,  768,  640,  735,  720,  720,  720,  800,  1080, 750,  1080, 1440, 3220};
      int[] list_y = {480, 800, 854, 960, 1024, 1024, 1136, 1152, 1184, 1196, 1280, 1280, 1812, 1334, 1920, 2560, 4500};

      int length = list_x.length;
      String[] list_name = new String[length];
      for(int i = 0; i < length; i++) {
         list_name[i] = "values-" + list_y[i] + "x" + list_x[i];
      }

      int size = list_name.length;
      for(int i = 0; i < size; i++) {
         File directory = new File(""); // 设定为当前文件夹

         try {
            String absolutePath = directory.getAbsolutePath();
            pathname = absolutePath + "/asset/" + list_name[i] + "/";
            File file = new File(pathname);
            if(!file.exists()) {
               file.mkdirs();
            }
         } catch(Exception e) {
            e.printStackTrace();
         }


         float level_x = (float) list_x[i] / width_size;  // 宽 单位值
         float level_y = (float) list_y[i] / height_size; // 高 单位值

         toCreateWidth(pathname, level_x);
         toCreateHeight(pathname, level_y);
      }

   }

   /**
    * 生成屏幕宽度文件
    *
    * @param pathname
    * @param level_y
    */
   private static void toCreateWidth(String pathname, float level_y) {

      StringBuilder builder = new StringBuilder();
      builder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
        "<resources>");
      for(int i = 1; i <= width_size; i++) {
         float real_y = level_y * i;
         builder.append("<dimen name=\"x" + i + "\">" + String.format("%.2f", real_y) + "px</dimen>");
      }
      builder.append("</resources>");

      XMLUtils.writeFile(pathname, builder.toString(), "lay_x");
   }

   /**
    * 生成屏幕高度文件
    *
    * @param pathname
    * @param level_y
    */
   private static void toCreateHeight(String pathname, float level_y) {

      StringBuilder builder = new StringBuilder();
      builder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
        "<resources>");
      for(int i = 1; i <= height_size; i++) {
         float real_x = level_y * i;
         builder.append("<dimen name=\"y" + i + "\">" + String.format("%.2f", real_x) + "px</dimen>");
      }
      builder.append("</resources>");

      XMLUtils.writeFile(pathname, builder.toString(), "lay_y");
   }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值