View位置相關(guān)
1恢恼、Android的坐標(biāo)系定義為:屏幕的左上角為坐標(biāo)原點俩由,向右為x軸增大方向妆档,向下為y軸增大方向
getLeft():子View左上角距父View左側(cè)的距離
getTop():子View左上角距父View頂部的距離痹愚;
getRight():子View右下角距父View左側(cè)的距離
-
getBottom():子View右下角距父View頂部的距離
public final int getLeft() { return mLeft; } public final int getRight() { return mRight; } public final int getTop() { return mTop; } public final int getBottom() { return mBottom; } public final int getHeight() { return mBottom - mTop; } public final int getWidth() { return mRight - mLeft; }
對于mLeft,mRight器赞,mTop,mBottom同時也提供了對應(yīng)的set方法墓拜,一般我們不會調(diào)用港柜,因為會影響View的寬高,同時改變著4個值的方法還有setFrame()和setOpticalFrame()咳榜,用過View中l(wèi)ayout的源碼所知
boolean changed = isLayoutModeOptical(mParent) ?setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);
也就是說潘懊,系統(tǒng)對于這4個屬性的修改,僅僅限于在layout方法內(nèi)部贿衍,而layout方法我們都知道授舟,是View在onMeasure測量之后,對自身以及子控件進(jìn)行位置布局的贸辈。
總結(jié):
- 如果想改變這4個屬性的值可以調(diào)用set方法释树,也可以通過設(shè)置LayoutParams設(shè)置對應(yīng)的margin,因為父容器在布局的時候調(diào)用
layout(int l, int t, int r, int b)
時會加上margin的值- 如果想要改變View對于父容器的影響擎淤,那么影響的是mLeft奢啥、mTop、width嘴拢、height等屬于父容器的屬性(mLeft桩盲、mTop、width席吴、height等都是父容器的屬性赌结,而不屬于本View)
- 如果想要改變View自身的影響捞蛋,比如添加監(jiān)聽器,添加動畫等等柬姚,那么影響的是translationX拟杉、translationY等屬于自身的屬性
2、getX()與getY()方法獲取的是View左上角相對于父容器的坐標(biāo)量承;getTranslationX()搬设、getTranslationY()是View左上角相對于父容器的偏移量
getX() = getLeft() + getTranslationX()
getY() = getRight() + getTranslationY()
3、getLocationOnScreen撕捍、getLocationInWindow
- View.getLocationOnScreen(int[] position)獲取View相對于整個屏幕的坐標(biāo)
- View.getLocationInWindow(int[] position)獲取View相對于Window的坐標(biāo)
以上2種方法得到絕對位置的方法只在有彈出窗時會有區(qū)別拿穴。
4、MotionEvent相關(guān)
- getX()和getY()獲取到的是相對于當(dāng)前View左上角的坐標(biāo)
- getRawX()和getRawY()獲取的是相對于屏幕左上角的坐標(biāo)
代碼實例##
圖中textView的各項值
int left = textView.getLeft();//399
int top = textView.getTop();//739
int right = textView.getRight();//680
int bottom = textView.getBottom();//796
int width = textView.getWidth();//281
int height = textView.getHeight();//57
float x = textView.getX();//399
float y = textView.getY();//739
float translationX = textView.getTranslationX();//0
float translationY = textView.getTranslationY();//0
現(xiàn)在使用屬性動畫將TextView向右平移100忧风,
ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "TranslationX", 100f);
animator.setDuration(1000);
animator.start();
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
int left = textView.getLeft();//399
int top = textView.getTop();//739
int right = textView.getRight();//680
int bottom = textView.getBottom();//796
int width = textView.getWidth();//281
int height = textView.getHeight();//57
int measuredWidth = textView.getMeasuredWidth();//281
int measuredHeight = textView.getMeasuredHeight();//57
float x = textView.getX();//499
float y = textView.getY();//739
float translationX = textView.getTranslationX();//100
float translationY = textView.getTranslationY();//0
}
});
可以看出getTranslationX()由0變成100贞言;getX()的值由399變成499,增加了100阀蒂;其他的屬性均沒有變化
Android中顏色獲取
int color = 0xff303F9F;
int color = getResources().getColor(R.color.colorPrimaryDark);
int color = getColor(R.color.colorPrimaryDark);// (API 23及以上)
int color = Color.parseColor("#ff303F9F");
int color = Color.BLUE;
int color = Color.argb(127, 255, 0, 0);
textView.setTextColor(color);