參考該文章理論知識加程式碼
但是呢,該篇博文裡個人覺得程式碼封裝的不是很好,於是自己封裝了下,使用XStream生成xml。上面那篇文章裡沒有對橫豎屏進行適配,程式碼裡完善了這一點,對橫豎屏進行了適配。
在開始碼程式碼前,貼一張圖,結合前面那篇文章的理論知識一起看。
然後呢看最終適配的效果,這裡以320*480為基準,螢幕上放一個TextView,寬度為x160,高度為y240,效果圖如下
然後呢,不要驚訝,你會發現裡面的兩個pad並沒有適配,其實呢,我也母雞呀,但是我開了一個模擬器,啟動了一個pad,其實是適配了。於是就沒有然後了,有興趣的再研究下吧。
先封裝Screen類
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package cn.edu.zafu.model; /** * @author lizhangqu * @description * @date */ public class Screen { private int width; private int height; public Screen(int width, int height) { this.width = width; this.height = height; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public String getDir(boolean portrait) { // landscape是橫向,portrait是縱向 if (portrait) { return String.format("values-%dx%d", height, width); } else { return String.format("values-land-%dx%d", height, width); } } @Override public String toString() { return "Screen [width=" + width + ", height=" + height + "]"; } } |
其次是Rescource類,使用註解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package cn.edu.zafu.model; import java.util.ArrayList; import java.util.List; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamImplicit; /** * @author lizhangqu * @description * @date */ @XStreamAlias("resources") public class Resource { @XStreamImplicit(itemFieldName = "dimen") List<Dimen> dimens = new ArrayList<Dimen>(); public Resource() { } public Resource(List<Dimen> dimens) { this.dimens = dimens; } public List<Dimen> getDimens() { return dimens; } public void setDimens(List<Dimen> dimens) { this.dimens = dimens; } @Override public String toString() { return "Resource [dimens=" + dimens + "]"; } } |
Dimen類,依然使用註解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package cn.edu.zafu.model; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAsAttribute; import com.thoughtworks.xstream.annotations.XStreamConverter; import com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter; /** * @author lizhangqu * @description * @date */ @XStreamAlias("dimen") @XStreamConverter(value = ToAttributedValueConverter.class, strings = { "value" }) public class Dimen { @XStreamAlias("name") @XStreamAsAttribute() private String name; private String value; public Dimen() { } public Dimen(String name, String value) { this.name = name; this.value = value; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public String toString() { return "Dimen [name=" + name + ", value=" + value + "]"; } } |
繼承XStream類,預設的只會輸出xml體,我們加入xml頭
1 |
<?xml version="1.0" encoding="utf-8"?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
package cn.edu.zafu.util; import java.io.OutputStream; import java.io.Writer; import com.thoughtworks.xstream.XStream; /** * @author lizhangqu * @description * @date */ public class XmlDeclarationXStream extends XStream { private String xmlDeclaration; private String version; private String ecoding; public XmlDeclarationXStream() { this("1.0", "utf-8"); } public XmlDeclarationXStream(String version, String ecoding) { this.version = version; this.ecoding = ecoding; buildDeclaration(); } private void buildDeclaration() { // generate xmlDeclaration StringBuffer buffer = new StringBuffer(); xmlDeclaration = buffer.append("<?xml version=\"").append(this.version) .append("\" encoding=\"").append(this.ecoding).append("\"?>") .append("\n").toString(); buffer = null; } public String getDeclaration() { return xmlDeclaration; } @Override public void toXML(Object arg0, OutputStream arg1) { try { String dec = this.getDeclaration(); byte[] bytesOfDec = dec.getBytes(this.ecoding); arg1.write(bytesOfDec); } catch (Exception e) { e.printStackTrace(); } super.toXML(arg0, arg1); } @Override public void toXML(Object arg0, Writer arg1) { try { arg1.write(getDeclaration()); } catch (Exception e) { e.printStackTrace(); } super.toXML(arg0, arg1); } } |
最後是生成資原始檔的工具類,見註釋
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
package cn.edu.zafu.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.text.DecimalFormat; import java.util.List; import cn.edu.zafu.model.Dimen; import cn.edu.zafu.model.Resource; import cn.edu.zafu.model.Screen; import com.thoughtworks.xstream.XStream; /** * * @author lizhangqu * @description a tool to generate what we want * @date */ public class Generator { // default base width private static final int DEFAULT_BASE_WIDTH = 320; // default base height private static final int DEFAULT_BASE_HEIGHT = 480; // decimal formator private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat( "#.##"); // a xstream,XmlDeclarationXStream extends XStream private static final XStream XSTREAM = new XmlDeclarationXStream(); //res dir private File resFile=null; // base width private int baseWidth; // base height private int beseHeight; /** * using default width and height to generate default width is 320,default * height is 480 */ public Generator() { this(DEFAULT_BASE_WIDTH, DEFAULT_BASE_HEIGHT); } /** * using baseWidth and baseHeight to generator * * @param baseWidth * @param beseHeight */ public Generator(int baseWidth, int beseHeight) { this.baseWidth = baseWidth; this.beseHeight = beseHeight; XSTREAM.autodetectAnnotations(true); this.resFile = new File("./res"); if (!resFile.exists()) { resFile.mkdir(); } } /** * using w and h to generate xml * * @param w * screen width * @param h * screen height * @return generated xml */ public String generateXml(Screen screen) { Resource resource = new Resource(); List<Dimen> dimens = resource.getDimens(); float cellWidth = screen.getWidth() * 1.0f / baseWidth; float cellHeight = screen.getHeight() * 1.0f / beseHeight; String result = null; Dimen dimen = null; for (int i = 1; i <= baseWidth; i++) { result = DECIMAL_FORMAT.format(i * cellWidth); dimen = new Dimen(); dimen.setName(String.format("x%d", i)); dimen.setValue(String.format("%spx", result)); dimens.add(dimen); } for (int i = 1; i <= beseHeight; i++) { result = DECIMAL_FORMAT.format(i * cellHeight); dimen = new Dimen(); dimen.setName(String.format("y%d", i)); dimen.setValue(String.format("%spx", result)); dimens.add(dimen); } String xml = XSTREAM.toXML(resource); return xml; } public String generateXmlLandscape(Screen screen) { Screen s=new Screen(screen.getHeight(),screen.getWidth()); return generateXml(s); } /** * write xmlContent to a File * * @param screen * @param xmlContent */ public void write2File(String valuesPath, String xmlContent) { File fileDir = new File(this.resFile + File.separator + valuesPath); fileDir.mkdir(); File file = new File(fileDir.getAbsolutePath(), "dimens.xml"); BufferedWriter bw = null; FileOutputStream fos = null; try { fos = new FileOutputStream(file); bw = new BufferedWriter(new OutputStreamWriter(fos)); bw.write(xmlContent); } catch (IOException e) { e.printStackTrace(); } finally { try { if (bw != null) { bw.close(); bw = null; } if (fos != null) { fos.close(); fos = null; } } catch (IOException e) { e.printStackTrace(); } } } } |
主函式,生成常見解析度的資原始檔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
package cn.edu.zafu; import java.util.ArrayList; import java.util.List; import cn.edu.zafu.model.Screen; import cn.edu.zafu.util.Generator; /** * @author lizhangqu * @description * @date */ public class Main { public static void main(String[] args) { new Main().bulid(); } public void bulid() { Generator generator = new Generator(320, 480); List<Screen> screens = new ArrayList<Screen>(); Screen screen = null; screen = new Screen(320, 400); screens.add(screen); screen = new Screen(320, 480); screens.add(screen); screen = new Screen(480, 800); screens.add(screen); screen = new Screen(480, 854); screens.add(screen); screen = new Screen(540, 960); screens.add(screen); screen = new Screen(600, 1024); screens.add(screen); screen = new Screen(720, 1184); screens.add(screen); screen = new Screen(720, 1196); screens.add(screen); screen = new Screen(720, 1280); screens.add(screen); screen = new Screen(768, 1024); screens.add(screen); screen = new Screen(768, 1280); screens.add(screen); screen = new Screen(800, 1280); screens.add(screen); screen = new Screen(1080, 1812); screens.add(screen); screen = new Screen(1080, 1920); screens.add(screen); screen = new Screen(1200, 1920); screens.add(screen); screen = new Screen(1440, 2560); screens.add(screen); screen = new Screen(2048, 1536); screens.add(screen); screen = new Screen(2560, 1600); screens.add(screen); int size = screens.size(); for (int i = 0; i < size; i++) { String portrait = generator.generateXml(screens.get(i)); generator.write2File(screens.get(i).getDir(true), portrait); String landscape = generator.generateXmlLandscape(screens.get(i)); generator.write2File(screens.get(i).getDir(false), landscape); System.out.println("create file "+i+" success"); } System.out.println("success"); } } |
原始碼下載
github下載:AndroidScreenAdapter
csdn下載:AndroidScreenAdapter