View的坐標(biāo)參數(shù)
- top 左上角縱坐標(biāo) left左上角橫坐標(biāo) bottom右下角縱坐標(biāo) right右下角橫坐標(biāo)
- x、y是View左上角的坐標(biāo)
- translationX、translationY是View左上角相對(duì)于父容器的偏移量,默認(rèn)值都為0
- getX/getY 返回的是相對(duì)于當(dāng)前view左上角的x和y
- getRawX/getRawY 返回的是相對(duì)于手機(jī)屏幕左上角的x和y坐標(biāo)
關(guān)系
x=left+translationX;
y=top+translationY;
width = right-left;
height = bottom-top;
Left = getLeft()
View平移時(shí),top、left等是原始左上角位置信息,其值不會(huì)發(fā)生變化,此時(shí)發(fā)生改變的是x、y、translationX、translationY
View中的幾個(gè)重要方法
public final void measure(int widthMeasureSpec, int heightMeasureSpec)
- measure 過(guò)程決定了View的寬高,Measure完成后可以通過(guò)getMeasureWidth和getMeasureHeight方法獲取到view的測(cè)量后的寬高,在幾乎所有的情況下都會(huì)等于最終view的寬高
- measure()方法接收兩個(gè)參數(shù),widthMeasureSpec和heightMeasureSpec,這兩個(gè)值分別用于確定視圖的寬度和高度的規(guī)格和大小。
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
- layout 過(guò)程決定了View的四個(gè)頂點(diǎn)的坐標(biāo)和實(shí)際的View的寬高,完成以后可以通過(guò)getTop,getBottom,getLeft,getRight來(lái)獲取View的四個(gè)頂點(diǎn)位置,并通過(guò)getWidth,getHeight獲取View的最終寬高
-
protected void onDraw(Canvas canvas)
draw過(guò)程則決定了View的顯示,完成draw后view會(huì)顯示在屏幕上
- 繪制背景(background.draw(Canvas))
- 繪制自己
protected void onDraw(Canvas canvas)
onDraw繪制自己,新建一個(gè)paint 在canvas上繪制自己的圖形 - 繪制children (dispatchDraw)
dispatchDraw會(huì)遍歷調(diào)用所有子元素的draw方法 - 繪制裝飾(onDrawScrollBars)
-
public boolean isEnabled()
當(dāng)前視圖是否可用。
可以調(diào)用setEnable()方法來(lái)改變視圖的可用狀態(tài),傳入true表示可用,傳入false表示不可用。
它們之間最大的區(qū)別在于,不可用的視圖是無(wú)法響應(yīng)onTouch事件的。 -
public boolean isFocused()
當(dāng)前視圖是否獲得焦點(diǎn)
通常情況下有兩種方法可以讓視圖獲得焦點(diǎn),即通過(guò)鍵盤(pán)的上下左右鍵切換視圖,以及調(diào)用requestFocus()方法。而現(xiàn)在的Android手機(jī)幾乎都沒(méi)有鍵盤(pán)了,因此基本上只可以使用requestFocus()這個(gè)辦法來(lái)讓視圖獲得焦點(diǎn)了。
而requestFocus()方法也不能保證一定可以讓視圖獲得焦點(diǎn),它會(huì)有一個(gè)布爾值的返回值,如果返回true說(shuō)明獲得焦點(diǎn)成功,返回false說(shuō)明獲得焦點(diǎn)失敗。一般只有視圖在focusable和focusable in touch mode同時(shí)成立的情況下才能成功獲取焦點(diǎn),比如說(shuō)EditText。 -
public void offsetTopAndBottom(int offset)及 public void offsetLeftAndRight(int offset)
offsetTopAndBottom直接改變的是top, bottom, 相當(dāng)于在parent中上下平移View的位置;
offsetLeftAndRight直接改變的是left, right, 相當(dāng)于在parent中左右平移View的位置;
View的邊界直接發(fā)生了變化,又因?yàn)閂iew和他的子View的相對(duì)位置沒(méi)變,所以他的子View的邊界也跟著變化了。
獲取View的位置坐標(biāo)失敗問(wèn)題怎么處理
獲取到位置坐標(biāo)為0,可能有如下三種原因:
- view的寬高本身就是0
- View的visibility屬性為gone;
- 視圖還未繪制完成,當(dāng)然未繪制完成也表現(xiàn)在不同的方面,比如,activity所代表的界面還沒(méi)顯示出來(lái)沒(méi)有添加到WindowPhone的DecorView上;要獲取的view沒(méi)有被添加到DecorView上
主要針對(duì)視圖還未繪制完成,怎么獲取view位置坐標(biāo)信息
- 在View的事件回調(diào)里獲??;這時(shí)候該view已經(jīng)被顯示即被添加到DecorView上 如點(diǎn)擊事件 觸摸事件 焦點(diǎn)事件等
view1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// doing
}
});
- 在activity被顯示出來(lái)時(shí)即添加到了DecorView上時(shí)獲取寬和高如
onWindowFocusChanged()回調(diào)方法
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// doing
}
boolean measure;
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && !measure) {
measure = true;
int width = new TextView(this).getMeasuredWidth();
int height = new TextView(this).getMeasuredHeight();
}
}
- 在onResume方法最后開(kāi)線(xiàn)程300毫秒左右后獲取寬和高 因?yàn)閛nResume執(zhí)行完后300毫秒后 界面就顯示出來(lái)了
@Override
protected void onResume() {
super.onResume();
view1.postDelayed(new Runnable() {
@Override
public void run() {
// doing
}
}, 300);
}
- 在onCreate()或onResume()等方法中需要獲取寬高時(shí)使用
getViewTreeObserver().addOnGlobalLayoutListener()
來(lái)為view添加回調(diào)在回調(diào)里獲得寬度或者高度獲取完后讓view刪除該回調(diào)
view1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// doing
}
});
- view.measure(int widthMeasureSpec,int heightMeasureSpec)
手動(dòng)對(duì)view進(jìn)行measure來(lái)得到view的寬高