Android快速屏幕尺寸适配 快速生成 res/dimens.xml

这是java代码工具,直接点击运行,不是Application那个运行,是java运行,你懂得。

此代码是

package com.ljp.youku.utls;
import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.File;  
import java.io.FileReader;  
import java.io.FileWriter;  
import java.io.IOException;  
import java.io.PrintWriter;  

import com.ljp.youku.TestYoukuActivity;

import android.R;
import android.app.Activity;
import android.os.Environment;

/**
 * 根据values/dimens.xml, 自动计算比例并生成不同分辨率的dimens.xml
 * 注意用dp和sp,不要用dip,否则生成可能会出错;xml值不要有空格
 *
 *
 * 使用之前要好对应的文件夹  与  文件
 *
 * 使用配置工具类代码,直接运行即可,(如果提示 Invalid layout of preloaded class 错误 项目设置如下即可

    Project->Properties->Run/DebugSettings;
    
    选择你的工具类(DimenTool.java) 点击"Edit";
    打开表中的 "Classpath" 并移除“Remove”此 "Bootstrap Entries"所有的东西(AndroidLib);
    
    Apply everything and Run the class
    again.

)
 *
 * Created by dongxiang on 16/7/19.
 */  
public class DimenTool {  

    public static void gen() {  

        /**
         * Androidstudio: ./app/src/main/res/values/dimens.xml
         * Eclipse: ./res/values/dimens.xml
         *
         * /F:/work/MyWorkSpace/TestYouku/
         * */
        //获取当前Classes 的绝对路径
        String filePath=Class.class.getClass().getResource("/").getPath();
        System.out.println(filePath);
        
        System.out.println(System.getProperty("user.dir"));
        File file = new File("./res/values/dimens.xml");//这里如果找不到,使用绝对路径即可  
        BufferedReader reader = null;  
        //添加分辨率
        StringBuilder sw480 = new StringBuilder();  
        StringBuilder sw600 = new StringBuilder();  
        StringBuilder sw720 = new StringBuilder();  
        StringBuilder sw800 = new StringBuilder();  
        StringBuilder  w820 = new StringBuilder();  

        try {  
            System.out.println("生成不同分辨率:");  
            reader = new BufferedReader(new FileReader(file));  
            String tempString;  
            int line = 1;  
            // 一次读入一行,直到读入null为文件结束  

            while ((tempString = reader.readLine()) != null) {  

                if (tempString.contains("</dimen>")) {  
                    //tempString = tempString.replaceAll(" ", "");  
                    String start = tempString.substring(0, tempString.indexOf(">") + 1);  
                    String end = tempString.substring(tempString.lastIndexOf("<") - 2);  
                    int num = Integer.valueOf(tempString.substring(tempString.indexOf(">") + 1, tempString.indexOf("</dimen>") - 2));  

                    //主要核心就再这里了,如下3种分辨率分别缩小 0.6、0.75、0.9倍(根据实际情况自己随意DIY)
                    sw480.append(start).append((int) Math.round(num * 0.6)).append(end).append("\n");  
                    sw600.append(start).append((int) Math.round(num * 0.75)).append(end).append("\n");  
                    sw720.append(start).append((int) Math.round(num * 0.9)).append(end).append("\n");
                    sw800.append(tempString).append("\n");  
                    w820.append(tempString).append("\n");  

                } else {  
                    sw480.append(tempString).append("\n");  
                    sw600.append(tempString).append("\n");  
                    sw720.append(tempString).append("\n");  
                    sw800.append(tempString).append("\n");  
                    w820.append(tempString).append("\n");  
                }  
                line++;  
            }  
            reader.close();  
            System.out.println("<!--  sw480 -->");  
            System.out.println(sw480);  
            System.out.println("<!--  sw600 -->");  
            System.out.println(sw600);  

            System.out.println("<!--  sw720 -->");  
            System.out.println(sw720);  
            System.out.println("<!--  sw800 -->");  
            System.out.println(sw800);  
            System.out.println("<!--  sw820 -->");  
            System.out.println(w820);  

//           Eclipse使用这部分
            String sw480file = "./res/values-sw480dp-land/dimens.xml";
            String sw600file = "./res/values-sw600dp-land/dimens.xml";  
            String sw720file = "./res/values-sw720dp-land/dimens.xml";  
            String sw800file = "./res/values-sw800dp-land/dimens.xml";  
            String w820file  = "./res/values-w820dp/dimens.xml";  
            
//            Android Studio 使用 这部分
//            String sw480file = "./app/src/main/res/values-sw480dp-land/dimens.xml";
//            String sw600file = "./app/src/main/res/values-sw600dp-land/dimens.xml";  
//            String sw720file = "./app/src/main/res/values-sw720dp-land/dimens.xml";  
//            String sw800file = "./app/src/main/res/values-sw800dp-land/dimens.xml";  
//            String w820file = "./app/src/main/res/values-w820dp/dimens.xml";  
            writeFile(sw480file, sw480.toString());  
            writeFile(sw600file, sw600.toString());  
            writeFile(sw720file, sw720.toString());  
            writeFile(sw800file, sw800.toString());  
            writeFile(w820file, w820.toString());  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (reader != null) {  
                try {  
                    reader.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
        }  
    }  

    public static void writeFile(String file, String text) {  
        PrintWriter out = null;  
        try {  
            out = new PrintWriter(new BufferedWriter(new FileWriter(file)));  
            out.println(text);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }   
        out.close();  
    }  

    public static void main(String[] args) {  
        gen();  
    }  
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值