闲谈自定义控件源码-view 坐标

来源:互联网 发布:数学分析教材推荐知乎 编辑:程序博客网 时间:2024/05/22 05:17
自定义view中 尤其是Ondraw和做动画的时候,基本上都在和坐标打交道
    一、2个概念的区分 density densitydpi 
     区分两个概念 
     首先可以看一个类 DisplayMetrics Display:显示 展示的意思 Metrics:度量。可见这是一个关于屏幕度量的类
      density和densityDpi是该类的两个成员变量
      
  1. /**
  2. * The logical density of the display. This is a scaling factor for the
  3. * Density Independent Pixel unit, where one DIP is one pixel on an
  4. * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
  5. * providing the baseline of the system's display. Thus on a 160dpi screen
  6. * this density value will be 1; on a 120 dpi screen it would be .75; etc.
  7. *
  8. * <p>This value does not exactly follow the real screen size (as given by
  9. * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
  10. * the overall UI in steps based on gross changes in the display dpi. For
  11. * example, a 240x320 screen will have a density of 1 even if its width is
  12. * 1.8", 1.3", etc. However, if the screen resolution is increased to
  13. * 320x480 but the screen size remained 1.5"x2" then the density would be
  14. * increased (probably to 1.5).
  15. *
  16. * @see #DENSITY_DEFAULT
  17. */
  18. public float density;
  19. /**
  20. * The screen density expressed as dots-per-inch. May be either
  21. * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
  22. */
  23. public int densityDpi;

根据注释可以看出 densityDpi:dots-per-inc 即:每英寸像素数量  即在屏幕上y轴2.54cm长度所包含的像素数
                               density:densityDpi/160 ---dp 为px*density  由此看见在任何分辨率手机上一英寸的dp长度是固定的。

二、View的定位
    view是根据left top right bottom来定位位置的。
    可以参考网上的一篇博客,讲解的很详细:http://blog.csdn.net/yanbober/article/details/50419117/
0 0