zoukankan      html  css  js  c++  java
  • Android dp,sp,pi,dpi关系讲解以及简单适配

    ▲ 基础概念 :

    dp:最常用的长、宽、margin、padding等的单位
    sp:字体的单位,和dp差不多,区别是如果字体使用的sp为单位,那如果你手机字体调大了,那你app的字体会随之变大,如果用dp则不会变化。
    px:像素
    dpi:dots per inch,即每英寸上有多少个像素,也就是屏幕密度,具体计算下面写说
    density:density=dpi/160
    dp和px转换

    px=dp*(dpi/160)

    计算dpi、density

    以5.5寸1920 * 1080的手机为例:

    5.5寸是指手机对角线的长度,分辨率是1920 * 1080,也就是知道长像素是1920,宽像素是1080,那么对角线多长呢,其实就是求斜边的长度: 1920^2+1080^2=2202^2

    所以 dpi=2202/5.5=400

    height和width即为长宽的像素,平方和即为对角线的像素个数,size即我们常说的5寸手机、4寸手机中的5和4,即对角线的长度

    所以,一样是5寸的手机,分辨率越高,dpi越高。分辨率相同,屏幕对角线英寸数越小,dpi越高

    而dp也叫dip,是device independent pixels。设备不依赖像素的一个单位。在不同的像素密度的设备上会自动适配,比如: 
    在320x480分辨率,像素密度为160,1dp=1px 
    在480x800分辨率,像素密度为240,1dp=1.5px 
    计算公式:px = dp * (dpi/160)

    一般我们android,布局都是用dp,字体用sp来布局的,这样至少适配了分辨率

    但是,android手机尺寸还有千奇百怪的,所以还要适配手机的屏幕 

    建立多个dimens,这个建立方式有两种,我就是被这两种弄晕了

    1.

    这种是以分辨率为单位的,可以达到适配效果,在xml中要以像素为单位,不要以dp(我自己认为的,不然感觉设计没用,因为dp不会因为像素改变)

    2.

    这种是以宽度的dp区分的,也可以,达到效果,这个肯定以dp为单位

    然后就可以自己去网上搜一遍自动生成的文件了

    放一篇自己搜的

    package com.tengying.tengying.utils;
     
    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;
     
    /**
     * Created by A on 2017/10/31.
     */
     
    public class DimenTool {
     
        public static void gen() {
     
            File file = new File("./app/src/main/res/values/dimens.xml");
            BufferedReader reader = null;
            StringBuilder sw480 = new StringBuilder();
            StringBuilder sw600 = new StringBuilder();
            StringBuilder sw720 = new StringBuilder();
            StringBuilder sw800 = new StringBuilder();
            StringBuilder sw820 = new StringBuilder();
            StringBuilder sw900 = 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));
     
                        sw480.append(start).append((int) Math.round(num * 0.53)).append(end).append(" ");
                        sw600.append(start).append((int) Math.round(num * 0.67)).append(end).append(" ");
                        sw720.append(start).append((int) Math.round(num * 0.8)).append(end).append(" ");
                        sw800.append(start).append((int) Math.round(num * 0.88)).append(end).append(" ");
                        sw820.append(start).append((int) Math.round(num * 0.91)).append(end).append(" ");
                        sw900.append(start).append((int) Math.round(num * 1)).append(end).append(" ");
     
                    } else {
                        sw480.append(tempString).append(" ");
                        sw600.append(tempString).append(" ");
                        sw720.append(tempString).append(" ");
                        sw800.append(tempString).append(" ");
                        sw820.append(tempString).append(" ");
                        sw900.append(tempString).append(" ");
                    }
                    line++;
                }
                reader.close();
     
                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 sw820file = "./app/src/main/res/values-sw820dp-land/dimens.xml";
                String sw900file = "./app/src/main/res/values-sw900dp-land/dimens.xml";
                writeFile(sw480file, sw480.toString());
                writeFile(sw600file, sw600.toString());
                writeFile(sw720file, sw720.toString());
                writeFile(sw800file, sw800.toString());
                writeFile(sw820file, sw820.toString());
                writeFile(sw900file, sw900.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();
        }
    }
     

  • 相关阅读:
    详解应对平台高并发的分布式调度框架TBSchedule
    利用httpd配置虚拟目录创建下载站点
    在centos7下用http搭建配置svn服务
    centos7/rhel7下配置PXE+Kickstart自动安装linux系统
    centos7破解安装confluence5.9.11
    centos7破解安装jira6.3.6(含Agile)
    mysql5.6做单向主从复制Replication
    解决忘记mysql中的root用户密码问题
    centos7安装mysql5.6(rpm包安装)
    centos7/RHEL7最小化系统安装gnome图形界面
  • 原文地址:https://www.cnblogs.com/zixia/p/11342376.html
Copyright © 2011-2022 走看看