├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── modules │ └── DpsGenerate.iml ├── runConfigurations.xml ├── vcs.xml └── workspace.xml ├── README.md ├── build.gradle ├── generate.iml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── local.properties ├── src └── main │ └── java │ └── cn │ └── georgeyang │ └── DpTransUtil.java └── test.iml /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /build 3 | /.idea 4 | /.gradle 5 | # Intellij 6 | *.iml 7 | # Generated files 8 | bin/ 9 | gen/ 10 | out/ -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 17 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 36 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/modules/DpsGenerate.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 21 | 22 | 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 | 140 | 141 | 146 | 151 | 171 | 320 | 325 | 488 | 491 | 492 | 493 | 500 | 501 | 502 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 543 | 544 | 545 | 546 | 549 | 550 | 553 | 554 | 555 | 556 | 559 | 560 | 563 | 564 | 567 | 568 | 571 | 572 | 575 | 576 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 607 | 608 | 609 | 610 | 613 | 614 | 615 | 616 | 629 | 630 | 637 | 638 | 639 | 640 | 658 | 665 | 666 | 674 | 675 | 680 | 681 | 683 | 684 | 685 | 687 | 688 | 689 | 690 | 1491874410365 691 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 732 | 735 | 736 | 737 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DpsGenerate 2 | 安卓最佳屏幕适配解决方案 3 | 4 | # 使用方法: 5 | 1. android Stdio-File-New-new module,选择本项目目录 6 | 2. 修改DpTransUtil.java 中的适配参数 7 | 8 | ``` 9 | 参数说明: 10 | 11 | 生成常用dp值 12 | generate(路径, UI设计图的宽度, 最小兼容的屏幕宽度DP值, 最大兼容的屏幕宽度DP值, 最小到最大DP的增量) 13 | generate(路径, UI设计图的宽度, 指定兼容的DP值) 14 | 15 | 增加指定dP值(默认1%-100%,如果不满足需要可以用该方法) 16 | appendPercent(路径, 指定百分比) 17 | 18 | 增加指定的X宽度对应dp值(默认1-Design宽度,如果不满足需要可以用该方法) 19 | appendXValue(path, 指定X值,支持负值) 20 | 21 | 22 | ``` 23 | 24 | 3. 开始执行 25 | 26 | 两种方法可以运行 27 | 28 | ``` 29 | 1:传统的javac方法执行DpTransUtil 30 | 2:进入DpsGenerate目录,执行gradle runJava 31 | ``` 32 | 33 | # 使用适配 34 | 以UI效果图720*1280为例,实际上,高度是可以忽略的,因为像素点是正方形的,有些手机720宽度,但不是1280高度 35 | 36 | 布局宽高安卓ui标注写@dimen/x?? 37 | 38 | 如果按照一定宽高比例,布局写@dimen/p?? ,p100代表沾满屏幕宽度 39 | 40 | 文字大小使用@dimen/f?? f??的实际单位是sp,手机字体是默认的普通大小时和dp没差别,但手机字体改成偏大或偏小,就起效果了 41 | 42 | # 联系我 43 | 44 | georgeyang1024@gmail.com 45 | 46 | 47 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | dependencies { 4 | compile fileTree(dir: 'libs', include: ['*.jar']) 5 | } 6 | 7 | tasks.withType(JavaCompile) { 8 | options.encoding = "UTF-8" 9 | } 10 | 11 | task runJava(type: JavaExec, dependsOn: 'classes') { 12 | main = 'cn.georgeyang.DpTransUtil' 13 | classpath = sourceSets.main.runtimeClasspath 14 | } 15 | 16 | defaultTasks 'runJava' -------------------------------------------------------------------------------- /generate.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | ## Project-wide Gradle settings. 2 | # 3 | # For more details on how to configure your build environment visit 4 | # http://www.gradle.org/docs/current/userguide/build_environment.html 5 | # 6 | # Specifies the JVM arguments used for the daemon process. 7 | # The setting is particularly useful for tweaking memory settings. 8 | # Default value: -Xmx1024m -XX:MaxPermSize=256m 9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 10 | # 11 | # When configured, Gradle will run in incubating parallel mode. 12 | # This option should only be used with decoupled projects. More details, visit 13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 14 | # org.gradle.parallel=true 15 | #Tue Apr 11 09:33:33 CST 2017 16 | systemProp.http.proxyHost=127.0.0.1 17 | systemProp.http.proxyPort=1080 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/georgeyang1024/DpsGenerate/00c16090e32ab811217eb9d5453431cfa33a5701/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Apr 11 09:33:39 CST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /local.properties: -------------------------------------------------------------------------------- 1 | ## This file is automatically generated by Android Studio. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must *NOT* be checked into Version Control Systems, 5 | # as it contains information specific to your local configuration. 6 | # 7 | # Location of the SDK. This is only used by Gradle. 8 | # For customization when using a Version Control System, please read the 9 | # header note. 10 | #Tue Apr 11 09:33:30 CST 2017 11 | sdk.dir=/Users/touch_ping/Application/android-sdk-macosx 12 | -------------------------------------------------------------------------------- /src/main/java/cn/georgeyang/DpTransUtil.java: -------------------------------------------------------------------------------- 1 | package cn.georgeyang; 2 | 3 | import java.io.*; 4 | import java.util.regex.Matcher; 5 | import java.util.regex.Pattern; 6 | 7 | /** 8 | * 480*800,160dpi的手机纵向,宽度是320dp 9 | * values-w400dp是指,宽度至少400dp 10 | * 如果将400设置成设计时的宽度,那么就会有w???/w400,将比例乘进去得到400dp的一套尺寸 11 | * 生xml,从最小320开始生成,增量20,最大nexus6p的520,开始生成10个xml 12 | * Created by yangsp on 2016/10/24. 13 | */ 14 | 15 | public class DpTransUtil { 16 | private final static String TEMPLATE = " {1}dp\n"; 17 | private final static String TEMPLATEF = " {1}sp\n"; 18 | private final static String TEMPLATEP = " {1}dp\n"; 19 | private final static String TEMPLATE_DIR = "values-w{0}dp"; 20 | private final static String FILENAME = "dps.xml"; 21 | private final static String ENDCHAR = ""; 22 | 23 | 24 | /** 25 | * 批量生成xml 26 | * 27 | * @param dir 28 | * @param designWidth 29 | * @param minWidthDp 30 | * @param maxWidthDp 31 | * @param increase 32 | * @return 33 | */ 34 | public static boolean generate(String dir, int designWidth, int minWidthDp, int maxWidthDp, int increase) { 35 | boolean ret = true; 36 | for (int i = minWidthDp; i <= maxWidthDp; i += increase) { 37 | ret = generate(dir, designWidth, i); 38 | } 39 | return ret; 40 | } 41 | 42 | /** 43 | * 生成指定像素密度的xml 44 | * 45 | * @param dir 46 | * @param designWidth 47 | * @param tagWidthDp 48 | * @return 49 | */ 50 | public static boolean generate(String dir, int designWidth, int tagWidthDp) { 51 | File fileDir = new File(dir + File.separator 52 | + TEMPLATE_DIR.replace("{0}", tagWidthDp + "")); 53 | fileDir.mkdirs(); 54 | 55 | float proportion = tagWidthDp * 1f / designWidth; 56 | 57 | File xmlFile = new File(fileDir.getAbsolutePath(), FILENAME); 58 | 59 | System.out.println("try generate XmlFile:" + tagWidthDp + ">>" + xmlFile.getAbsolutePath()); 60 | return generateXmlFile(xmlFile, designWidth, proportion); 61 | } 62 | 63 | /** 64 | * 追加百分比,如果1%-100%未满足需要,出现200%的情况,用此方法 65 | * 66 | * @return 67 | */ 68 | public static boolean appendPercent(String dir, float percent) { 69 | try { 70 | File currFile = new File(dir); 71 | if (currFile.isDirectory()) { 72 | File[] files = currFile.listFiles(); 73 | for (File file : files) { 74 | appendPercent(file.getAbsolutePath(), percent); 75 | } 76 | } else if (FILENAME.equalsIgnoreCase(currFile.getName())) { 77 | System.out.println("try append Percent:" + currFile.getAbsolutePath()); 78 | String content = readFile(currFile); 79 | Pattern pattern = Pattern.compile("(\\d+.\\d+)dp.*>"); 80 | Matcher matcher = pattern.matcher(content); 81 | if (matcher.find()) { 82 | String value = matcher.group(1); 83 | float proportion = Float.valueOf(value) / 100; 84 | float realWidth = ((int) (percent * proportion * 100)) / 100; 85 | String append = TEMPLATEP.replace("{0}", String.valueOf(percent).replace(".", "_")).replace("{1}", realWidth + ""); 86 | 87 | RandomAccessFile raf = new RandomAccessFile(currFile, "rw"); 88 | raf.seek(currFile.length() - ENDCHAR.length()); 89 | raf.writeBytes(append); 90 | raf.writeBytes(ENDCHAR); 91 | raf.close(); 92 | } 93 | } 94 | return true; 95 | } catch (Exception e) { 96 | e.printStackTrace(); 97 | } 98 | return false; 99 | } 100 | 101 | /** 102 | * 追加像素值,如果1-design宽度未满足需要,出现很高的高度需要显示,用此方法 103 | * 104 | * @return 105 | */ 106 | public static boolean appendXValue(String dir, int px) { 107 | try { 108 | File currFile = new File(dir); 109 | if (currFile.isDirectory()) { 110 | File[] files = currFile.listFiles(); 111 | for (File file : files) { 112 | appendXValue(file.getAbsolutePath(), px); 113 | } 114 | } else if (FILENAME.equalsIgnoreCase(currFile.getName())) { 115 | System.out.println("try append XValue:" + currFile.getAbsolutePath()); 116 | String content = readFile(currFile); 117 | Pattern pattern = Pattern.compile("(\\d+.\\d+)dp.*>"); 118 | Matcher matcher = pattern.matcher(content); 119 | if (matcher.find()) { 120 | String key = matcher.group(1); 121 | String value = matcher.group(2); 122 | float proportion = Float.valueOf(value) / Float.valueOf(key); 123 | float realWidth = ((int) (px * proportion * 100)) / 100; 124 | String append = TEMPLATE.replace("{0}", px + "").replace("{1}", realWidth + ""); 125 | append = append.replace("x-","x_"); 126 | 127 | RandomAccessFile raf = new RandomAccessFile(currFile, "rw"); 128 | raf.seek(currFile.length() - ENDCHAR.length()); 129 | raf.writeBytes(append); 130 | raf.writeBytes(ENDCHAR); 131 | raf.close(); 132 | } 133 | } 134 | return true; 135 | } catch (Exception e) { 136 | e.printStackTrace(); 137 | } 138 | return false; 139 | } 140 | 141 | /** 142 | * 生成xml 143 | * @param xmlFile 144 | * @param maxWidth 145 | * @param proportion 比例 146 | * @return 147 | */ 148 | private static boolean generateXmlFile(File xmlFile, int maxWidth, float proportion) { 149 | StringBuffer output = new StringBuffer(); 150 | output.append("\n"); 151 | for (int i = 1; i <= 100; i++) { 152 | float realWidth = (maxWidth * proportion * i) / 100f; 153 | realWidth = ((int) realWidth * 100) / 100f; 154 | output.append(TEMPLATEP.replace("{0}", i + "").replace("{1}", realWidth + "")); 155 | } 156 | for (int i = 1; i <= maxWidth; i++) { 157 | float realWidth = (int) ((i * proportion) * 100) / 100f; 158 | output.append(TEMPLATE.replace("{0}", i + "").replace("{1}", realWidth + "")); 159 | output.append(TEMPLATEF.replace("{0}", i + "").replace("{1}", realWidth + "")); 160 | } 161 | output.append(""); 162 | 163 | return writeToFile(xmlFile, output.toString()); 164 | } 165 | 166 | private static String readFile(File file) { 167 | String content = ""; 168 | try { 169 | Long fileLens = file.length(); 170 | byte[] bytes = new byte[fileLens.intValue()]; 171 | FileInputStream fs = new FileInputStream(file); 172 | fs.read(bytes); 173 | fs.close(); 174 | content = new String(bytes, "UTF-8"); 175 | } catch (Exception e) { 176 | e.printStackTrace(); 177 | } 178 | return content; 179 | } 180 | 181 | 182 | private static boolean writeToFile(File file, String content) { 183 | try { 184 | PrintWriter pw = new PrintWriter(new FileOutputStream(file)); 185 | pw.print(content); 186 | pw.close(); 187 | return true; 188 | } catch (Exception e) { 189 | e.printStackTrace(); 190 | } 191 | return false; 192 | } 193 | 194 | 195 | public static void main(String[] args) { 196 | String path = "./res"; 197 | System.out.println("生成结果 0:" + generate(path, 720, 320, 520, 10)); 198 | System.out.println("生成结果 1:" + generate(path, 720, 120)); 199 | System.out.println("生成结果 2:" + appendPercent(path, 200)); 200 | System.out.println("生成结果 3:" + appendXValue(path, 800)); 201 | System.out.println("生成结果 4:" + appendXValue(path, 1003)); 202 | System.out.println("生成结果 4:" + appendXValue(path, 778)); 203 | System.out.println("生成结果 5:" + appendXValue(path, 755)); 204 | System.out.println("生成结果 6:" + appendXValue(path, -80)); 205 | } 206 | 207 | 208 | 209 | /** 210 | * 传统算法,如果720的设计,2px=1sp(备用方法) 211 | * @param dir 212 | * @return 213 | */ 214 | public static boolean generateSps(String dir) { 215 | File fileDir = new File(dir + File.separator + "values"); 216 | fileDir.mkdirs(); 217 | File spFile = new File(fileDir.getAbsolutePath(), "sps.xml"); 218 | StringBuffer output = new StringBuffer(); 219 | output.append("\n"); 220 | for (int i = 1; i <= 720; i++) { 221 | String string = String.format(" %ssp\n",new Object[]{i,(i*10.0/20) + ""}); 222 | output.append(string); 223 | } 224 | output.append(""); 225 | 226 | return writeToFile(spFile, output.toString()); 227 | } 228 | } 229 | -------------------------------------------------------------------------------- /test.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | --------------------------------------------------------------------------------