Summer Solstice Welcome reprint, Please also keep this statement, thank you
In the past, when doing small things, feel as long as the adaptation of their own mobile phone can be, found that really dug a lot of holes for themselves. It's a painful thing, but it's really interesting to have a good look at it today.
First, understand several concepts first.
1. Screen size
屏幕尺寸指屏幕的对角线的长度,单位是英寸 : 1 英寸 = 2.54 mm 常见的尺寸有: 2.4 、2.8、3.5 、3.7,4.2、5.0等等
2. Screen resolution
屏幕分辨率是指在横纵向上的像素点数,单位是px ,1px = 1个像素点
3. Screen pixel density
Screen pixel density refers to the number of pixels per inch, dpi (also called DP), screen pixel density and screen size and screen resolution, in a single change, the smaller screen size, the higher the resolution, the greater the pixel density, conversely, the smaller.
4, DP, dip, DPI, SP, px
Dip and DP are a means of screen density. What is DPI? It can be understood that if there are 160 pixels in 1 inches, then the pixel density of this screen is 160dpi. In this case, the DP and PX conversions are very simple, as shown in:
For example, you are on a 480x320 mobile phone cloth, a look at the overbearing cool, but your unit is used PX, a 800x480 or higher resolution of the mobile phone, is staggered. Therefore, we generally do not recommend using PX to do the unit.
5, MDPI, hdpi, xdpi, xxdpi
There is a ldpi, but now basically no one with such a horrible resolution, so you can not have to control it. With eclipse we often see such defined drawable folders in the Res folder, so what exactly do they mean? Google's official designation is differentiated according to the following criteria:
In the development of the time, we also need to correspond to different folders into different images:
If it is not particularly important for scaling, we can put it in the Drawable folder, such as. 9 graphs, etc.
6. Support Pingbian Screen Size
Back to our point, what we're going to talk about today is the adaptive screen.
-Use Wrap_content, match_parent, weight
In order to ensure the flexibility of the layout and adapt to a variety of screens, we generally recommend using wrap_content content wrapping, and match_parent filling the entire container, so that is not an absolute layout, but can be adapted to the size of the screen, but we layout so much, It is not possible to use these two attributes for each, so the limitations are great.
As for the weight weight of this property, easy to use is good, but generally in the linear layout, but the linear layout has an awkward place is that if you have a lot of layout, then its roots will be very long, this for the UI optimization is very unfavorable, and Google generally recommends that our layout layer not more than 10 layers, so, If it is relatively complex, it is recommended to use the Reativelayout relative layout. Therefore, the limitations are also very large.
-use DP and Percent
At this point, you might say that using DP and SP AH, DP is density, you have different resolution, with DP can also be very well adapted. Find two pictures on the internet. The effect of running on two different models with the same code is as follows:
It's embarrassing, all using DP, why do you still have this problem? In fact, because of the variety of screens, not all screens are the same DP length, such as Nexus One belongs to hdpi, the screen width is 320DP, and the Nexus 5 belongs to xxhdpi, the screen width is 360DP. So, even if you use DP, there is a problem. For example, you wrote a textview in the "DP", the font size of the SP, under two different devices, the size of the obvious difference.
At this time, you are not very desperate, feel the feeling of having no love. Resolution is different, so can not use PX, screen width is not the same, so you can not use DP, but also let people live. Don't worry, there's still a way out. Refer to the two gods of the Great God:
Hon Yang's Great god: http://blog.csdn.net/lmj623565791/article/details/45460089
One leaf floating boat: http://blog.csdn.net/jdsjlzx/article/details/45891551
You will be the one who basically looks at them. I'm just writing about my understanding.
Of course, after Android 5.0, Android also supports the percentage. That's a good news for sure. We can see, why we are on the computer browsing the web, the size of the control, always according to the size of our screen varies, that is, they use a percentage of the effect ah, the percentage is the proportion of the entire screen, this is easy to understand it. But its limitations are in here, need 5.0 system, I tried it myself, the effect is really good, but put to 4.3 of the system, the chaos. Therefore, you can properly understand. If you want to know more about it, please refer to the works of the Great God: http://blog.csdn.net/lmj623565791/article/details/46695347
好了,言归正传,我们的方案很简单,就是在项目中添加你的分辨率的不同文件夹,如:
The Java code that automatically generates the resolution package is as follows:
ImportJava.io.File;ImportJava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.PrintWriter;/** * Created by Zhy on 15/5/3. * * Public class generatevaluefiles { Private intBasew;Private intBaseh;PrivateString Dirstr ="./res";Private Final StaticString wtemplate =" <dimen name=\" x{0}\ ">{1}px</dimen>\n ";Private Final StaticString htemplate =" <dimen name=\" y{0}\ ">{1}px</dimen>\n ";/** * {0}-height * * Private Final StaticString value_template ="Values-{0}x{1}";Private Static FinalString support_dimesion ="320,480;480,800;480,854;540,960;600,1024;720,1184;720,1196;720,1280;768,1024;800,1280;1080,1812;1080,1920 ; 1440,2560; ";PrivateString supportstr = support_dimesion; Public Generatevaluefiles(intBaseX,intBasey, String supportstr) { This. Basew = BaseX; This. Baseh = Basey;if(! This. Supportstr.contains (BaseX +","+ Basey)) { This. supportstr + = BaseX +","+ Basey +";"; } This. supportstr + = ValidateInput (SUPPORTSTR); System.out.println (SUPPORTSTR); File dir =NewFile (DIRSTR);if(!dir.exists ()) {Dir.mkdir (); } System.out.println (Dir.getabsolutefile ()); }/** * @param supportstr * W,H_...W,H; * @return * * PrivateStringValidateInput(String supportstr) {StringBuffer SB =NewStringBuffer (); string[] Vals = Supportstr.split ("_");intW =-1;intH =-1; String[] WH; for(String val:vals) {Try{if(val = =NULL|| Val.trim (). Length () = =0)Continue; WH = Val.split (","); W = Integer.parseint (wh[0]); h = integer.parseint (wh[1]); }Catch(Exception e) {System.out.println ("Skip Invalidate params:w,h ="+ val);Continue; } sb.append (W +","+ H +";"); }returnSb.tostring (); } Public void Generate() {string[] Vals = Supportstr.split (";"); for(String val:vals) {string[] wh = val.split (","); Generatexmlfile (Integer.parseint (wh[0]), Integer.parseint (wh[1])); } }Private void Generatexmlfile(intWinth) {StringBuffer sbforwidth =NewStringBuffer (); Sbforwidth.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\n "); Sbforwidth.append ("<resources>");floatCELLW = w *1.0F/basew; System.out.println ("width:"+ W +","+ Basew +","+ CELLW); for(inti =1; i < Basew; i++) {Sbforwidth.append (Wtemplate.replace (' {0} ', i +""). Replace (' {1} ', Change (CELLW * i) +"")); } sbforwidth.append (Wtemplate.replace (' {0} ', Basew +""). Replace (' {1} ', W +"")); Sbforwidth.append ("</resources>"); StringBuffer sbforheight =NewStringBuffer (); Sbforheight.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\n "); Sbforheight.append ("<resources>");floatCELLH = h *1.0F/baseh; System.out.println ("Height:"+ H +","+ Baseh +","+ CELLH); for(inti =1; i < Baseh; i++) {Sbforheight.append (Htemplate.replace (' {0} ', i +""). Replace (' {1} ', Change (CELLH * i) +"")); } sbforheight.append (Htemplate.replace (' {0} ', Baseh +""). Replace (' {1} ', H +"")); Sbforheight.append ("</resources>"); File Filedir =NewFile (dirstr + file.separator + value_template.replace (' {0} ', H +"")//. replace (' {1} ', W +"")); Filedir.mkdir (); File Layxfile =NewFile (Filedir.getabsolutepath (),"Lay_x.xml"); File Layyfile =NewFile (Filedir.getabsolutepath (),"Lay_y.xml");Try{PrintWriter PW =NewPrintWriter (NewFileOutputStream (Layxfile)); Pw.print (Sbforwidth.tostring ()); Pw.close (); PW =NewPrintWriter (NewFileOutputStream (Layyfile)); Pw.print (Sbforheight.tostring ()); Pw.close (); }Catch(FileNotFoundException e) {E.printstacktrace (); } } Public Static float Change(floatA) {inttemp = (int) (A * -);returnTemp/ -F } Public Static void Main(string[] args) {intBasew = the;intBaseh = -; String addition ="";Try{if(Args.length >=3) {Basew = Integer.parseint (args[0]); Baseh = Integer.parseint (args[1]); addition = args[2]; }Else if(Args.length >=2) {Basew = Integer.parseint (args[0]); Baseh = Integer.parseint (args[1]); }Else if(Args.length >=1) {addition = args[0]; } }Catch(NumberFormatException e) {System.err. println ("Right input params:java-jar xxx.jar width height w,h_w,h_..._w,h;"); E.printstacktrace (); System.exit (-1); }NewGeneratevaluefiles (Basew, Baseh, addition). Generate (); }}
The only place to be modified is the benchmark, such as the one above which is based on 480 x 320. , we can change to what we need. Of course, you can also generate a jar package, and then, under the command line, generate different code:
First, package the process into a jar package and enter:
Java-jar Xx.jar Width Height width,height_width,height
What do you mean, the first two is the meaning of the benchmark. The next one is the package you want to extend the extra. Like what
I want a 1280x720 benchmark, and then an extra 1366x768 package. It can be written like this:
Java-jar Test.jar 1280x720 1366,768
The following is also to be extended, where you can choose different depending on the difference. 、
Of course you can also direct you to the benchmark package:
But there are limitations, see many screens on the Internet, if the mobile phone with a virtual key, this scheme is not suitable, can only follow the virtual key to re-fit, of course, here is still suitable for a lot of memory. Here are the commonly used two benchmark 480x320, 800x480, and 1280x720 benchmark res package to everyone, of course, I suggest you do it yourself. CSDN accessories too sore, with 360 good:
160 base Https://yunpan.cn/cPFiutXhwhdpB access password 1BC7
240 Base Https://yunpan.cn/cPFiFXihU7ytz access password 29DF
320 base Https://yunpan.cn/cPFixBsSQqrrX access password 91da
If it disappears, please contact me.
7. The difference between Android TV and mobile phone
Here we will talk about the difference between Android TV and mobile phone. On the phone, we can use a 480x320 benchmark to design the screen size, because the phone is 5.0 inches dead, so the benchmark is still very good. But Android TV is not the same, are flat-type, but also a larger LCD screen. So, our benchmark is to change. To use 1280x720 as a benchmark, of course, this is only a personal understanding. There are errors and also welcome points.
Advanced--Screen adaptive