Android之应用坐标系统全面详解

来自:http://blog.csdn.net/yanbober/article/details/50419117

1 背景

去年有很多人私信告诉我让说说自定义控件,其实通观网络上的很多博客都在讲各种自定义控件,但是大多数都是授之以鱼,却很少有较为系统性授之于渔的文章,同时由于自己也迟迟没有时间规划这一系列文章,最近想将这一系列文章重新提起来,所以就来先总结一下自定义控件的一个核心知识点——坐标系。

很多人可能不屑一顾Android的坐标系,但是如果你想彻底学会自定义控件,我想说了解Android各种坐标系及一些API的坐标含义绝对算一个小而不可忽视的技能;所谓Android自定义View那几大主要onXXX()方法的重写实质其实大多数都是在处理坐标逻辑运算,所以我们就先来就题重谈一下Android坐标系。

【工匠若水 http://blog.csdn.net/yanbober 未经允许严禁转载,请尊重作者劳动成果。私信联系我】

2 Android坐标系

说到Android坐标系其实就是一个三维坐标,Z轴向上,X轴向右,Y轴向下。这三维坐标的点处理就能构成Android丰富的界面或者动画等效果,所以Android坐标系在整个Android界面中算是盖楼房的尺寸草图,下面我们就来看看这些相关的概念。

2-1 Android屏幕区域划分

我们先看一副图来了解一下Android屏幕的区域划分(关于这个东西的深入探讨你可以看下《Android应用setContentView与LayoutInflater加载解析机制源码分析 》一文,那儿给出了部分原理的解释),如下:


通过上图我们可以很直观的看到Android对于屏幕的划分定义。下面我们就给出这些区域里常用区域的一些坐标或者度量方式。如下:

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取屏幕区域的宽高等尺寸获取</span>
DisplayMetrics metrics = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> widthPixels = metrics.widthPixels;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> heightPixels = metrics.heightPixels;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//应用程序App区域宽高等尺寸获取</span>
Rect rect = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取状态栏高度</span>
Rect rect= <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> statusBarHeight = rectangle.top;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//View布局区域宽高等尺寸获取</span>
Rect rect = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Rect();  
getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rect);  </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

特别注意:上面这些方法最好在Activity的onWindowFocusChanged ()方法或者之后调运,因为只有这时候才是真正的显示OK,不懂的可以看我之前关于setContentView相关的博客。

2-2 Android View绝对相对坐标系

上面我们分析了Android屏幕的划分,可以发现我们平时开发的重点其实都在关注View布局区域,那么下面我们就来细说一下View区域相关的各种坐标系。先看下面这幅图:


通过上图我们可以很直观的给出View一些坐标相关的方法解释,不过必须要明确的是上面这些方法必须要在layout之后才有效,如下:

View的静态坐标方法 解释
getLeft() 返回View自身左边到父布局左边的距离
getTop() 返回View自身顶边到父布局顶边的距离
getRight() 返回View自身右边到父布局左边的距离
getBottom() 返回View自身底边到父布局顶边的距离
getX() 返回值为getLeft()+getTranslationX(),当setTranslationX()时getLeft()不变,getX()变。
getY() 返回值为getTop()+getTranslationY(),当setTranslationY()时getTop()不变,getY()变。


同时也可以看见上图中给出了手指触摸屏幕时MotionEvent提供的一些方法解释,如下:

MotionEvent坐标方法 解释
getX() 当前触摸事件距离当前View左边的距离
getY() 当前触摸事件距离当前View顶边的距离
getRawX() 当前触摸事件距离整个屏幕左边的距离
getRawY() 当前触摸事件距离整个屏幕顶边的距离


上面就解释了你在很多代码中看见各种getXXX方法进行数学逻辑运算判断的含义。不过上面只是说了一些相对静止的Android坐标点关系,下面我们来看看几个和上面方法紧密相关的View方法。如下:

View宽高方法 解释
getWidth() layout后有效,返回值是mRight-mLeft,一般会参考measure的宽度(measure可能没用),但不是必须的。
getHeight() layout后有效,返回值是mBottom-mTop,一般会参考measure的高度(measure可能没用),但不是必须的。
getMeasuredWidth() 返回measure过程得到的mMeasuredWidth值,供layout参考,或许没用。
getMeasuredHeight() 返回measure过程得到的mMeasuredHeight值,供layout参考,或许没用。


上面解释了自定义View时各种获取宽高的一些含义,下面我们再来看看关于View获取屏幕中位置的一些方法,不过这些方法需要在Activity的onWindowFocusChanged ()方法之后才能使用。如下图:


下面我们就给出上面这幅图涉及的View的一些坐标方法的结果(结果采用使用方法返回的实际坐标,不依赖上面实际绝对坐标转换,上面绝对坐标只是为了说明例子中的位置而已),如下:

View的方法 上图View1结果 上图View2结果 结论描述
getLocalVisibleRect() (0, 0 - 410, 100) (0, 0 - 410, 470) 获取View自身可见的坐标区域,坐标以自己的左上角为原点(0,0),另一点为可见区域右下角相对自己(0,0)点的坐标,其实View2当前height为550,可见height为470。
getGlobalVisibleRect() (30, 100 - 440, 200) (30, 250 - 440, 720) 获取View在屏幕绝对坐标系中的可视区域,坐标以屏幕左上角为原点(0,0),另一个点为可见区域右下角相对屏幕原点(0,0)点的坐标。
getLocationOnScreen() (30, 100) (30, 250) 坐标是相对整个屏幕而言,Y坐标为View左上角到屏幕顶部的距离。
getLocationInWindow() (30, 100) (30, 250) 如果为普通Activity则Y坐标为View左上角到屏幕顶部(此时Window与屏幕一样大);如果为对话框式的Activity则Y坐标为当前Dialog模式Activity的标题栏顶部到View左上角的距离。


到此常用的相关View的静态坐标获取处理的方法和含义都已经叙述完了,下面我们看看动态的一些解释(所谓动静只是我个人称呼而已)。

2-3 Android View动画相关坐标系

其实在我们使用动画时,尤其是补间动画时,你会发现其中涉及很多坐标参数,一会儿为相对的,一会儿为绝对的,你可能会各种蒙圈。那么不妨看下《Android应用开发之所有动画使用详解 》这篇博客,这里面详细介绍了关于Android动画相关的坐标系统,这里不再累赘叙述。

2-4 Android View滑动相关坐标系

关于View提供的与坐标息息相关的另一组常用的重要方法就是滚动或者滑动相关的,下面我们给出相关的解释(特别注意:View的scrollTo()和scrollBy()是用于滑动View中的内容,而不是改变View的位置;改变View在屏幕中的位置可以使用offsetLeftAndRight()和offsetTopAndBottom()方法,他会导致getLeft()等值改变。),如下:

View的滑动方法 效果及描述
offsetLeftAndRight(int offset) 水平方向挪动View,offset为正则x轴正向移动,移动的是整个View,getLeft()会变的,自定义View很有用
offsetTopAndBottom(int offset) 垂直方向挪动View,offset为正则y轴正向移动,移动的是整个View,getTop()会变的,自定义View很有用
scrollTo(int x, int y)View中内容(不是整个View)滑动到相应的位置,参考坐标原点为ParentView左上角,x,y为正则向xy轴反方向移动,反之同理。
scrollBy(int x, int y) 在scrollTo()的基础上继续滑动xy。
setScrollX(int value) 实质为scrollTo(),只是只改变Y轴滑动。
setScrollY(int value) 实质为scrollTo(),只是只改变X轴滑动。
getScrollX()/getScrollY() 获取当前滑动位置偏移量。


关于Android View的scrollBy()和scrollTo()参数传递正数却向坐标系负方向移动的特性可能很多人都有疑惑,甚至是死记结论,这里我们简单给出产生这种特性的真实原因—-源码分析,如下:

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">scrollTo</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> x, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> y) {<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (mScrollX != x || mScrollY != y) {<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> oldX = mScrollX;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> oldY = mScrollY;mScrollX = x;mScrollY = y;invalidateParentCaches();onScrollChanged(mScrollX, mScrollY, oldX, oldY);<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!awakenScrollBars()) {postInvalidateOnAnimation();}}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>

View的该方法注释里明确说明了调运他会触发onScrollChanged()和invalidated()方法,那我们就将矛头转向invalidated()方法触发的draw()过程,draw()过程中最终其实会触发下面的invalidate()方法,如下:

<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">invalidate</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> l, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> t, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> r, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> b) {<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> scrollX = mScrollX;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> scrollY = mScrollY;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//scroller时为何参数和坐标反向的真实原因</span>invalidateInternal(l - scrollX, t - scrollY, r - scrollX, b - scrollY, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>);
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

核心就在这里,相信不用我解释大家也知道咋回事了,自行脑补。

scrollTo()和scrollBy()方法特别注意:如果你给一个ViewGroup调用scrollTo()方法滚动的是ViewGroup里面的内容,如果想滚动一个ViewGroup则再给他嵌套一个外层,滚动外层即可。

【工匠若水 http://blog.csdn.net/yanbober 未经允许严禁转载,请尊重作者劳动成果。私信联系我】

3 总结

可以发现,上面只是说明了一些View里常用的与坐标相关的概念,关于自定义控件了解学习这些坐标概念只是一个基础,也是一个后续内容的铺垫,所以有必要先完全吃透此部分内容才能继续拓展学习新的东东。

View中还有一些其他与坐标获取相关的方法,但是一般都比较不常用,所以用到时可以现查API或者Debug看现象进行学习即可,这里篇幅和时间有限就不一一道来了。




本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/294398.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

震撼!豆瓣评分9.3,这部纪录片带你感受生命之重!

全世界只有3.14 % 的人关注了爆炸吧知识达尔文的进化论为后人研究生命起源开启了明窗&#xff0c;“物竞天择&#xff0c;适者生存”的法则在各种生物演变上得到了印证&#xff0c;自然环境也是新物种产生、旧物种灭亡的关键因素。因此&#xff0c;生物进化是自然界的必然趋势&…

QAction添加事件响应

为菜单Help下的About添加响应 about对应actionAbout 点击QAction会发出triggered()信号&#xff0c;所以&#xff0c;我们要做的是声明一个slot&#xff0c;然后connect这个信号。 头文件中 public slots: void showAboutMsg(); 构造函数中 connect(actionAbout,SIGNAL(trigger…

WPF学习笔记(二):初学者避坑实录

使用 KeyBinding 实现文本框回车提交文本框的回车提交是一个很常见的需求&#xff1a;在一个复杂的筛选页面上&#xff0c;用户希望在输入框输入文字后直接回车即可触发查询&#xff0c;而不是非得点击一下搜索按钮。假设需要在用户输入回车时触发 TestCommand 命令&#xff0c…

Android - 文件读写操作 总结

在Android开发中&#xff0c;有两种处理资源文件的方式。其一&#xff0c;是将所有资源文件以及JNI程序放置于一个单独的资源包。使用到他们时&#xff0c;使用文件方式读取。或者直接使用C层代码读取。 其二&#xff0c;则是将资源文件加入到APK内部。使用各种不同的办法去得到…

Android 之View绘图原理总结

Android系统的视图结构的设计也采用了组合模式&#xff0c;即View作为所有图形的基类&#xff0c;Viewgroup对View继承扩展为视图容器类&#xff0c;由此就得到了视图部分的基本结构--树形结构 View定义了绘图的基本操作 基本操作由三个函数完成&#xff1a;measure()、layout(…

SharePoint2013开发环境搭建(完整版:图文并茂)

windows 8 系统下安装SharePoint 2013 开发环境 配置windows8 系统 12G内存包含虚拟机&#xff08;windows server2012 系统 1.5G AD服务器&#xff09;,&#xff08;windows server2012 系统 6G sharepoint服务器及数据库服务器&#xff09; 1.安装AD服务器&#xff08;虚拟机…

全程颅内高潮!数学史上最震撼的三个瞬间!从那一刻起,人类的命运就被改写了.......

全世界只有3.14 % 的人关注了爆炸吧知识运伟大之思者必行伟大之迷途如果可以穿越到过去&#xff0c;你最想成为下面的哪个人&#xff1f;1 公元前3世纪&#xff0c;希腊&#xff0c;亚历山大城。有一个年轻人&#xff0c;千里迢迢地从雅典来到了这座城市&#xff0c;满脸疲惫&a…

java 创建web项目_java – Eclipse:以编程方式创建动态Web项目

我尝试通过首先通过IProject创建java项目然后使用IFacetedProject将其转换为动态Web项目来创建动态Web项目,但是只创建了静态项目…这是我已经完成的代码…请帮助我这……谢谢.IWorkspaceRoot root ResourcesPlugin.getWorkspace().getRoot();IProject project root.getProje…

英文版opensuse 12.2安装中文输入法ibus

点击左下角的小蜥蜴图标——>computer-->YAST-->Soft-->Soft Managerment-->搜索ibus 在右侧的一栏选择安装ibus&#xff0c;ibus-gtk&#xff0c;ibus-pingyin选中后单击Accpet&#xff0c;重启电脑即可转载于:https://blog.51cto.com/haoxy/1102808

YARP+AgileConfig 5分钟实现一个支持配置热更新的代理网关

YARP 是微软开源的一个反向代理项目&#xff0c;英文名叫 Yet Another Reverse Proxy 。所谓反向代理最有名的那就是 nginx 了&#xff0c;没错 YARP 也可以用来完成 nginx 的大部分功能&#xff0c;比如根据不一样的域名代理到不一样的后端服务上。既然它可以做反向代理&#…

Android之属性动画初步

Android动画系统包括View animation和Property animation&#xff0c;也就是视图动画和属性动画&#xff0c;属性动画有API限制&#xff0c;必须在API 11以上使用&#xff0c;不过有个开源项目NineOldAndroids&#xff0c;实现了对API 11之前版本的支持。   视图动画相对于属…

【IBatisNet Spring.Net】ORM与IOC 简单配置

1.修改WebConfig.cs配置文件 <configuration><configSections><sectionGroup name"spring"><section name"context" type"Spring.Context.Support.WebContextHandler, Spring.Web" /><section name"objects&quo…

Codeforces Round #323 (Div. 2) C.GCD Table

C. GCD TableThe GCD table G of size n  n for an array of positive integers a of length n is defined by formula Let us remind you that the greatest common divisor (GCD) of two positive integers x and y is the greatest integer that is divisor of both xand …

豆瓣9.8,它凭「少儿不宜」吊打所有美剧!脑洞大开必看神作!【内附资源】...

全世界只有3.14 % 的人关注了爆炸吧知识给最近剧荒的你安利一部脑洞大开、想象力天马行空的成人动画&#xff08;妥妥的神剧!&#xff09;——《Rick and Morty》Rick and Morty瑞克和莫蒂这部少儿不宜????的成人动画到底收获了多少好评呢&#xff1f;抛开拿到美国电视剧届…

java 继承与多态 习题_JAVA基础 第4章继承与多态_练习题_200910

Java基础第4章练习题大外软件学院第4章继承与多态一&#xff0e;选择题1. 编译和运行以下两文件结果是( D )。//文件P1.javapackage MyPackage;class P1{void afancymethod(){System.out.println("What a fancy method");}}//文件P2.javapackage YourPackage;import …

[设计模式原则]第五回:迪米特原则

1.引言 迪米特法则&#xff08;Law of Demeter&#xff09;又叫作最少知识原则&#xff08;LKP,Least Knowledge Principle&#xff09;&#xff0c;就是说一个对象应当对其他对象有尽可能少的了解&#xff0c;类与类之间的了解的越多&#xff0c;关系越密切&#xff0c;耦合度…

如何判断当前请求的API类型

前言上次&#xff0c;我们判断了《当前请求是否健康检查API》&#xff0c;避免其写入日志。但是&#xff0c;对于我们自己开发的API来说&#xff0c;最好也能来区分&#xff0c;比如调试用API&#xff0c;就不需要再写调用日志了。DisplayName方式直接判断路由地址的方式就不考…

Android之DrawText详解

如果你经常使用Canvas的draw***方法去绘制一些图像图形&#xff0c;绘制的坐标是从Canvas左上角开始计算的&#xff0c;如果想要把一个图像放到某个位置&#xff0c;直接drawBitmap传递图片左上角的坐标就行了。那drawText就不一样&#xff0c;如果你传递进去字符串&#xff0c…

Gruntjs: grunt-contrib-jst

预编译Underscore模板到JST文件&#xff08;Underscore&#xff1a;JS工具库) generate JavaScript template functions Gruntfile的配置实例&#xff1a; 1 module.exports function(grunt) {2 3 grunt.initConfig({4 jst: {5 bulid: {6 …

托马斯反驳牛顿被骂,普朗克颜值过高遭上帝捉弄,狄拉克却因爱情成话痨

全世界只有3.14 % 的人关注了爆炸吧知识今天&#xff0c;小编抑制不住自己&#xff0c;要给大家强烈推荐一个公众号“少年物理学家”&#xff01;少年物理学家是一个致力为学生家长和老师&#xff0c;提供丰富的物理小知识&#xff1a;物理学家、物理趣谈、科技与物理、万物背后…