自定义view中 尤其是Ondraw和做动画的时候,基本上都在和坐标打交道
一、2个概念的区分 density densitydpi
区分两个概念
首先可以看一个类 DisplayMetrics Display:显示 展示的意思 Metrics:度量。可见这是一个关于屏幕度量的类
density和densityDpi是该类的两个成员变量
/**
* The logical density of the display. This is a scaling factor for the
* Density Independent Pixel unit, where one DIP is one pixel on an
* approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
* providing the baseline of the system's display. Thus on a 160dpi screen
* this density value will be 1; on a 120 dpi screen it would be .75; etc.
*
* <p>This value does not exactly follow the real screen size (as given by
* {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
* the overall UI in steps based on gross changes in the display dpi. For
* example, a 240x320 screen will have a density of 1 even if its width is
* 1.8", 1.3", etc. However, if the screen resolution is increased to
* 320x480 but the screen size remained 1.5"x2" then the density would be
* increased (probably to 1.5).
*
* @see #DENSITY_DEFAULT
*/
public float density;
/**
* The screen density expressed as dots-per-inch. May be either
* {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
*/
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/