關於屏幕適配的一些看法

因爲分辨率不一樣,所以不能用px;因爲屏幕寬度不一樣,所以要小心的用dp,那麼我們可不可以用另外一種方法來統一單位,不管分辨率是多大,屏幕寬度用一個固定的值的單位來統計呢?

答案是:當然可以。

我們假設手機屏幕的寬度都是320某單位,那麼我們將一個屏幕寬度的總像素數平均分成320份,每一份對應具體的像素就可以了

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
 
public class MakeXml {
 
    private final static String rootPath = "C:\\Users\\Administrator\\Desktop\\layoutroot\\values-{0}x{1}\\";
 
    private final static float dw = 320f;
    private final static float dh = 480f;
 
    private final static String WTemplate = "[dimen name=\"x{0}\"]{1}px[/dimen]\n";
    private final static String HTemplate = "[dimen name=\"y{0}\"]{1}px[/dimen]\n";
 
    public static void main(String[] args) {
        makeString(320, 480);
        makeString(480,800);
        makeString(480, 854);
        makeString(540, 960);
        makeString(600, 1024);
        makeString(720, 1184);
        makeString(720, 1196);
        makeString(720, 1280);
        makeString(768, 1024);
        makeString(800, 1280);
        makeString(1080, 1812);
        makeString(1080, 1920);
        makeString(1440, 2560);
    }
 
    public static void makeString(int w, int h) {
 
        StringBuffer sb = new StringBuffer();
        sb.append("[?xml version=\"1.0\" encoding=\"utf-8\"?]\n");
        sb.append("[resources]");
        float cellw = w / dw;
        for (int i = 1; i < 320; i++) {
            sb.append(WTemplate.replace("{0}", i + "").replace("{1}",
                    change(cellw * i) + ""));
        }
        sb.append(WTemplate.replace("{0}", "320").replace("{1}", w + ""));
        sb.append("[/resources]");
 
        StringBuffer sb2 = new StringBuffer();
        sb2.append("[?xml version=\"1.0\" encoding=\"utf-8\"?]\n");
        sb2.append("[resources]");
        float cellh = h / dh;
        for (int i = 1; i < 480; i++) {
            sb2.append(HTemplate.replace("{0}", i + "").replace("{1}",
                    change(cellh * i) + ""));
        }
        sb2.append(HTemplate.replace("{0}", "480").replace("{1}", h + ""));
        sb2.append("[/resources]");
 
        String path = rootPath.replace("{0}", h + "").replace("{1}", w + "");
        File rootFile = new File(path);
        if (!rootFile.exists()) {
            rootFile.mkdirs();
        }
        File layxFile = new File(path + "lay_x.xml");
        File layyFile = new File(path + "lay_y.xml");
        try {
            PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
            pw.print(sb.toString());
            pw.close();
            pw = new PrintWriter(new FileOutputStream(layyFile));
            pw.print(sb2.toString());
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
 
    }
 
    public static float change(float a) {
        int temp = (int) (a * 100);
        return temp / 100f;
    }
}

  代碼應該很好懂,我們將一個屏幕寬度分爲320份,高度480份,然後按照實際像素對每一個單位進行復制,放在對應values-widthxheight文件夾下面的lax.xml和lay.xml裏面,這樣就可以統一所有你想要的分辨率的單位了,下面是生成的一個320*480分辨率的文件,因爲寬高分割之後總分數和像素數相同,所以x1就是1px,以此類推。

 

 

具體詳情:http://www.cocoachina.com/android/20151030/13971.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章