本文主要记录自定义view时所要涉及到的位置坐标等知识点的总结。
本文主要记录自定义view时所要涉及到的位置坐标等知识点的总结。
测试机型:三星S4 GTI9508
屏幕参数:分辨率:1080*1920,尺寸: 5.0寸
参考我以前总结的文章:android view学习系列(一)基础知识
1、屏幕区域划分
一个手机的屏幕区域一般分为:状态栏、标题栏、view区域等部分。
各部分区域大小计算
1、 获取屏幕区域的宽和高
| 12
 3
 4
 5
 6
 
 | 
2、应用程序App区域宽高等尺寸获取 | 
| 12
 3
 4
 5
 6
 
 | Rect rect = new Rect()getWindow().getDecorView().getWindowVisibleDisplayFrame(rect)
 int appWidth = rect.width()
 int appHeight = rect.height()
 Log.d(TAG, "应用程序App区域宽: " + appWidth)
 Log.d(TAG, "应用程序App区域高: " + appHeight)
 | 
3、获取状态栏高度
| 12
 3
 4
 
 | Rect rectTop = new Rect()getWindow().getDecorView().getWindowVisibleDisplayFrame(rectTop)
 int statusBarHeight = rectTop.top
 Log.d(TAG, "获取状态栏高度: " + statusBarHeight)
 
 | 
4、View布局区域宽高等尺寸获取
| 12
 3
 4
 5
 6
 
 | Rect rectView = new Rect()getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rectView)
 int viewWidth = rectView.width()
 int viewHeight = rectView.height()
 Log.d(TAG, "View布局区域宽: " + viewWidth)
 Log.d(TAG, "View布局区域高: " + viewHeight)
 | 
5、标题栏的高度
| 1
 | Log.d(TAG,"标题栏的高度等于-> 应用程序App区域高 - View布局区域高: "+(appHeight-viewHeight)); | 
建议:以上个参数的获取最好在onWindowFocusChanged ()方法里面获取。
2、View坐标体系
View的位置属性主要有它的四个定点来决定的,分别是top、left、right、bottom
对照此图我们可以得出此view的坐标为:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 
 | Left=getLeft()
 
 
 Top=getTop()
 
 
 Right=getRight()
 
 
 Bottom=getBottom()
 
 
 width=right-left
 height=bottom-top
 
 | 
注意 :从android3.0以上,view增加了几个额外的参数:x、y、translationX、translationY。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | x、y
 
 
 translationX、translationY,默认为0
 
 getX()=getLeft()+getTranslationX()
 getY()=getTop()+getTranslationY()
 
 
 
 view在平移过程中,getLeft()和getTop()代表原始左上角的位置信息,不会改变,此时改变的是getX()、getY()、getTranslationX()、getTranslationY()。
 
 | 
当我们触摸屏幕view区域时,会发生一系列事件MotionEvent,同时可以获得发生时的坐标信息,系统提供了两组方法:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | 
 getX()
 
 
 getY()
 
 
 getRawX()
 
 
 getRawY()
 
 | 
验证
新建application工程、新建一个类:viewTest,创建布局如下代码所示:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.lljjcoder.viewdemo.MainActivity">
 
 <com.lljjcoder.viewdemo.widget.ViewTest
 android:layout_width="200dp"
 android:layout_height="300dp"
 android:layout_marginLeft="10dp"
 android:layout_marginTop="10dp"
 android:background="@color/colorPrimary" />
 </RelativeLayout>
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 
 | * 作者:liji on 2016/6/24 10:46
 * 邮箱:lijiwork@sina.com
 */
 public class ViewTest extends View {
 private final String TAG = "自定义view"
 
 public ViewTest(Context context) {
 super(context)
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas)
 
 
 int left = getLeft()
 int top = getTop()
 int right = getRight()
 int bottom = getBottom()
 Log.d(TAG, "left/top/right/bottom: " + left + "," + top + "," + right + "," + bottom)
 
 
 int viewWidth = getWidth()
 int viewHeight = getHeight()
 Log.d(TAG, "view width=(right-left): " + (right - left))
 Log.d(TAG, "view width=(getWidth()): " + viewWidth)
 Log.d(TAG, "view height=(bottom-top): " + (bottom - top))
 Log.d(TAG, "view height=(getHeight()): " + (viewHeight))
 
 }
 
 
 | 
由于我的手机分辨率是1080*1920,物理尺寸是5.0寸,所以算下来的ppi为480PPI,所以是基准160PPI的3倍关系,从结果也能看出来,我们在xml布局文件中设定的:
| 12
 3
 4
 5
 6
 
 | <com.lljjcoder.viewdemo.widget.ViewTestandroid:layout_width="200dp"
 android:layout_height="300dp"
 android:layout_marginLeft="10dp"
 android:layout_marginTop="10dp"
 android:background="@color/colorPrimary" />
 
 | 
距离左边10dp,距离上面是10dp,我们打印出来的是30px,所以是正确的。同时宽设定为200dp,也就是600px,所以
| 1
 | left/top/right/bottom,依次是:30,30,630,930
 | 
非常正确!
原文链接:http://www.apkbus.com/blog-705730-61056.html
		
			
			打开App,阅读手记