日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ViewTreeObserver

發布時間:2025/6/15 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ViewTreeObserver 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

api的概述:

A view tree observer is used to register listeners that can be notified of global changes in the view tree. Such global events include, but are not limited to, layout of the whole tree, beginning of the drawing pass, touch mode change.... A ViewTreeObserver should never be instantiated by applications as it is provided by the views hierarchy. Refer to View.getViewTreeObserver() for more information.

簡單來說,這是個view事件的觀察者。要注意的是它的初始化就是調用View.getViewTreeObserver()


包含下面6個事件:

interface ?ViewTreeObserver.OnDrawListener?

擋在一個視圖樹繪制時,所要調用的回調函數的接口類(level 16)

interface ? ? ? ? ?ViewTreeObserver.OnGlobalFocusChangeListener ??
當在一個視圖樹中的焦點狀態發生改變時,所要調用的回調函數的接口類

interface ? ? ? ? ?ViewTreeObserver.OnGlobalLayoutListener
當在一個視圖樹中全局布局發生改變或者視圖樹中的某個視圖的可視狀態發生改變時,所要調用的回調函數的接口類

interface ? ? ? ? ?ViewTreeObserver.OnPreDrawListener
當一個視圖樹將要繪制時,所要調用的回調函數的接口類

interface ? ? ? ? ?ViewTreeObserver.OnScrollChangedListener
當一個視圖樹中的一些組件發生滾動時,所要調用的回調函數的接口類

interface ? ? ? ? ?ViewTreeObserver.OnTouchModeChangeListener
當一個視圖樹的觸摸模式發生改變時,所要調用的回調函數的接口類


具體的實例,

1,在OnCreate()中獲取控件高度與寬度

[java] view plaincopy
  • ViewTreeObserver?observer?=?view.getViewTreeObserver();????
  • observer?.addOnGlobalLayoutListener(new?OnGlobalLayoutListener()?{????
  • ???????????@Override????
  • ???????????public?void?onGlobalLayout()?{????
  • ??????????????view.getViewTreeObserver().removeGlobalOnLayoutListener(this);????
  • ????final?int?w?=?view.getMeasuredWidth();??
  • ????????????final?int?h?=?view.getMeasuredHeight();??
  • ???????????}????
  • ???????});???


  • 2,啟動幀動畫

    使用ViewTreeObserver.OnPreDrawListener listener:當一個視圖樹將要繪制時產生事件,可以添加一個其事件處理函數:onPreDraw

    [html] view plaincopy
  • OnPreDrawListener?opdl=new?OnPreDrawListener(){??
  • ????????@Override??
  • ????????public?boolean?onPreDraw()?{??
  • ????????????animDraw.start();??
  • ????????????return?true;??
  • ????????}??
  • ????};??
  • ???
  • //onCreate方法中??
  • imageV.getViewTreeObserver().addOnPreDrawListener(opdl);??

  • view也有onfocusChangedListener,是不是有些重復呢,這些回調肯定和view中的對應的回調有關聯的,只不過這里的是有Global這個關鍵字,范圍更大,其他的用處深入android源碼應該可以找到挺多的,待補充吧。

    總結

    以上是生活随笔為你收集整理的ViewTreeObserver的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。