導航:首頁 > 操作系統 > Androidview刷新機制

Androidview刷新機制

發布時間:2023-02-24 03:54:05

android中的SurfaceView詳解

View 和 SurfaceView 的區別:

1 . View適用於主動更新的情況,而SurfaceView則適用於被動更新的情況,比如頻繁刷新界面。

2 . View在主線程中對頁面進行刷新,而SurfaceView則開啟一個子線程來對頁面進行刷新。

3 . View在繪圖時沒有實現雙緩沖機制,SurfaceView在底層機制中就實現了雙緩沖機制。

這摘錄了一段網上對於雙緩沖技術的介紹

1 . 創建SurfaceView

2 . 初始化SurfaceView

3 . 使用SurfaceView

這一步又可以分為3步來完成:

(1) 通過 lockCanvas() 方法獲得Canvas對象

(2) 在子線程中使用Canvas對象進行繪制

(3) 使用 unlockCanvasAndPost() 方法將畫布內容進行提交

注意: lockCanvas() 方法獲得的Canvas對象仍然是上次繪制的對象,由於我們是不斷進行繪制,但是每次得到的Canvas對象都是第一次創建的Canvas對象。

我們在xml文件中的使用和自定義View是相同的,使用全路徑名稱即可:

1 . 繪制正弦曲線

2 . 手寫板(隨手指繪制軌跡)

上面還有一個細節,在繪制的時候,我們並沒有讓線程一直運行,而是讓它休眠一會,從而節約系統資源,一般建議判斷的閾值為50-100之間即可保證用戶體驗同時節約系統資源。

Ⅱ Android怎麼強制刷新view

關鍵的一句話就是:
在Android的布局體系中,父View負責刷新、布局顯示子View;而當子View需要刷新時,則是通知父View來完成。
步驟就是:
1、調用子View的invalidate()
2、跳轉到上一層的invalidateChild函數中區
3、在一次調用invalidateChildInParent的函數一次層層刷新
4、具體的刷新後續操作,我就不清楚了,調用invalidate最終在代碼上就在invalidateChild終止了的,所以表示有點點不清晰,求各位大牛介紹一下吧。。。。。?在此謝過了。。
讓我來閱讀源代碼:
首先在View類中:
/**
* Invalidate the whole view. If the view is visible, {@link #onDraw} will
* be called at some point in the future. This must be called from a
* UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
*/
public void invalidate() {
if (ViewDebug.TRACE_HIERARCHY) {
ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
}
if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS)) {
mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
final ViewParent p = mParent; //獲得父類View的對象
final AttachInfo ai = mAttachInfo;//獲得匹配
if (p != null && ai != null) {
final Rect r = ai.mTmpInvalRect;
r.set(0, 0, mRight - mLeft, mBottom - mTop);//設置本View的尺寸,其實就是大小沒有設置位置
// Don't call invalidate -- we don't want to internally scroll
// our own bounds
p.invalidateChild(this, r); //調用父類的刷新函數
}
}
}
下面我們來到Viewgroup對象:

在invalidate中,調用父View的invalidateChild,這是一個從第向上回溯的過程,每一層的父View都將自己的顯示區域與傳入的刷新Rect做交集。
/**
* Don't call or override this method. It is used for the implementation of
* the view hierarchy.
*/
public final void invalidateChild(View child, final Rect dirty) {
if (ViewDebug.TRACE_HIERARCHY) {
ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD);
}
ViewParent parent = this;
final AttachInfo attachInfo = mAttachInfo;
if (attachInfo != null) {
final int[] location = attachInfo.mInvalidateChildLocation;
// 刷新子View的位置
location[CHILD_LEFT_INDEX] = child.mLeft;
location[CHILD_TOP_INDEX] = child.mTop;
// If the child is drawing an animation, we want to this flag onto
// ourselves and the parent to make sure the invalidate request goes
// through
final boolean drawAnimation = (child.mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION;
// Check whether the child that requests the invalidate is fully opaque
final boolean isOpaque = child.isOpaque() && !drawAnimation &&
child.getAnimation() != null;
// Mark the child as dirty, using the appropriate flag
// Make sure we do not set both flags at the same time
final int opaqueFlag = isOpaque ? DIRTY_OPAQUE : DIRTY;
do {
View view = null;
if (parent instanceof View) {
view = (View) parent;
}
if (drawAnimation) {
if (view != null) {
view.mPrivateFlags |= DRAW_ANIMATION;
} else if (parent instanceof ViewRoot) {
((ViewRoot) parent).mIsAnimating = true;
}
}
// If the parent is dirty opaque or not dirty, mark it dirty with the opaque
// flag coming from the child that initiated the invalidate
if (view != null && (view.mPrivateFlags & DIRTY_MASK) != DIRTY) {
view.mPrivateFlags = (view.mPrivateFlags & ~DIRTY_MASK) | opaqueFlag;
}
parent = parent.invalidateChildInParent(location, dirty);
} while (parent != null);
}
/**
* Don't call or override this method. It is used for the implementation of
* the view hierarchy.
*
* This implementation returns null if this ViewGroup does not have a parent,
* if this ViewGroup is already fully invalidated or if the dirty rectangle
* does not intersect with this ViewGroup's bounds.
*/
public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
if (ViewDebug.TRACE_HIERARCHY) {
ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD_IN_PARENT);
}
if ((mPrivateFlags & DRAWN) == DRAWN) {
if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
FLAG_OPTIMIZE_INVALIDATE) {
// 由父類的的位置,偏移刷新區域
dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX,
location[CHILD_TOP_INDEX] - mScrollY);
final int left = mLeft;
final int top = mTop;
if (dirty.intersect(0, 0, mRight - left, mBottom - top) ||
(mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
mPrivateFlags &= ~DRAWING_CACHE_VALID;
location[CHILD_LEFT_INDEX] = left;
location[CHILD_TOP_INDEX] = top;
return mParent;
}
} else {
mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
location[CHILD_LEFT_INDEX] = mLeft;
location[CHILD_TOP_INDEX] = mTop;
dirty.set(0, 0, mRight - location[CHILD_LEFT_INDEX],
mBottom - location[CHILD_TOP_INDEX]);
return mParent;
}
}
return null;
}
另外:
Invalidate()方法不能放在線程中,所以需要把Invalidate()方法放在Handler中。在MyThread中只需要在規定時間內發送一個Message給handler,當Handler接收到消息就調用Invalidate()方法。
postInvalidate()方法就可以放在線程中做處理,就不需要Handler。

而上面的新線程MyThre可以放在OnCreate()中開始,也可以放在OnStart()中開始。
Invalidate()方法和postInvalidate()都可以在主線程中調用而刷新視圖。
Invalidate()方法在SDK中是這樣描述的:Invalidate the whole view. If the view is visible, onDraw(Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate(). 當Invalidate()被調用的時候,View的OnDraw()就會被調用,Invalidate()必須是在UI線程中被調用,如果在新線程中更新視圖的就調用postInvalidate()。
簡言之,如果確定是在main thread中調用調用話, 使用 invaludate()

否則要調用 postInvalidate()

另外,橫豎屏切換使用重新構造 activity的。所以一定會重新刷新view 。

Ⅲ Android怎麼強制刷新view

步驟就是:
1、調用子View的invalidate()
2、跳轉到上一層的invalidateChild函數中區
3、在一次調用invalidateChildInParent的函數一次層層刷新

讓我來閱讀源代碼:
首先在View類中:
/**
* Invalidate the whole view. If the view is visible, {@link #onDraw} will
* be called at some point in the future. This must be called from a
* UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
*/
public void invalidate() {
if (ViewDebug.TRACE_HIERARCHY) {
ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
}
if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS)) {
mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
final ViewParent p = mParent; //獲得父類View的對象
final AttachInfo ai = mAttachInfo;//獲得匹配
if (p != null && ai != null) {
final Rect r = ai.mTmpInvalRect;
r.set(0, 0, mRight - mLeft, mBottom - mTop);//設置本View的尺寸,其實就是大小沒有設置位置
// Don't call invalidate -- we don't want to internally scroll
// our own bounds
p.invalidateChild(this, r); //調用父類的刷新函數
}
}
}
下面我們來到Viewgroup對象:

在invalidate中,調用父View的invalidateChild,這是一個從第向上回溯的過程,每一層的父View都將自己的顯示區域與傳入的刷新Rect做交集。
/**
* Don't call or override this method. It is used for the implementation of
* the view hierarchy.
*/
public final void invalidateChild(View child, final Rect dirty) {
if (ViewDebug.TRACE_HIERARCHY) {
ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD);
}
ViewParent parent = this;
final AttachInfo attachInfo = mAttachInfo;
if (attachInfo != null) {
final int[] location = attachInfo.mInvalidateChildLocation;
// 刷新子View的位置
location[CHILD_LEFT_INDEX] = child.mLeft;
location[CHILD_TOP_INDEX] = child.mTop;
// If the child is drawing an animation, we want to this flag onto
// ourselves and the parent to make sure the invalidate request goes
// through
final boolean drawAnimation = (child.mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION;
// Check whether the child that requests the invalidate is fully opaque
final boolean isOpaque = child.isOpaque() && !drawAnimation &&
child.getAnimation() != null;
// Mark the child as dirty, using the appropriate flag
// Make sure we do not set both flags at the same time
final int opaqueFlag = isOpaque ? DIRTY_OPAQUE : DIRTY;
do {
View view = null;
if (parent instanceof View) {
view = (View) parent;
}
if (drawAnimation) {
if (view != null) {
view.mPrivateFlags |= DRAW_ANIMATION;
} else if (parent instanceof ViewRoot) {
((ViewRoot) parent).mIsAnimating = true;
}
}
// If the parent is dirty opaque or not dirty, mark it dirty with the opaque
// flag coming from the child that initiated the invalidate
if (view != null && (view.mPrivateFlags & DIRTY_MASK) != DIRTY) {
view.mPrivateFlags = (view.mPrivateFlags & ~DIRTY_MASK) | opaqueFlag;
}
parent = parent.invalidateChildInParent(location, dirty);
} while (parent != null);
}
/**
* Don't call or override this method. It is used for the implementation of
* the view hierarchy.
*
* This implementation returns null if this ViewGroup does not have a parent,
* if this ViewGroup is already fully invalidated or if the dirty rectangle
* does not intersect with this ViewGroup's bounds.
*/
public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
if (ViewDebug.TRACE_HIERARCHY) {
ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD_IN_PARENT);
}
if ((mPrivateFlags & DRAWN) == DRAWN) {
if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
FLAG_OPTIMIZE_INVALIDATE) {
// 由父類的的位置,偏移刷新區域
dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX,
location[CHILD_TOP_INDEX] - mScrollY);
final int left = mLeft;
final int top = mTop;
if (dirty.intersect(0, 0, mRight - left, mBottom - top) ||
(mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
mPrivateFlags &= ~DRAWING_CACHE_VALID;
location[CHILD_LEFT_INDEX] = left;
location[CHILD_TOP_INDEX] = top;
return mParent;
}
} else {
mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
location[CHILD_LEFT_INDEX] = mLeft;
location[CHILD_TOP_INDEX] = mTop;
dirty.set(0, 0, mRight - location[CHILD_LEFT_INDEX],
mBottom - location[CHILD_TOP_INDEX]);
return mParent;
}
}
return null;
}
另外:
Invalidate()方法不能放在線程中,所以需要把Invalidate()方法放在Handler中。在MyThread中只需要在規定時間內發送一個Message給handler,當Handler接收到消息就調用Invalidate()方法。
postInvalidate()方法就可以放在線程中做處理,就不需要Handler。

而上面的新線程MyThre可以放在OnCreate()中開始,也可以放在OnStart()中開始。
Invalidate()方法和postInvalidate()都可以在主線程中調用而刷新視圖。
Invalidate()方法在SDK中是這樣描述的:Invalidate the whole view. If the view is visible, onDraw(Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate(). 當Invalidate()被調用的時候,View的OnDraw()就會被調用,Invalidate()必須是在UI線程中被調用,如果在新線程中更新視圖的就調用postInvalidate()。
簡言之,如果確定是在main thread中調用調用話, 使用 invaludate()

否則要調用 postInvalidate()

另外,橫豎屏切換使用重新構造 activity的。所以一定會重新刷新view 。

親!希望能幫助到你哦!~~~

Ⅳ Android 怎麼實現支持所有View的通用的下拉刷新控制項

下拉刷新主要有兩種實現方式:
1. 在ListView中添加header和footer,監聽ListView的滑動事件,動態設置header/footer的高度,但是這種方式只適用於ListView,RecyclerView。
2. 第二種方式則是繼承ViewGroup或其子類,監聽事件,通過scroll或Layout的方式移動child。

Ⅳ Android UI中的View如何刷新

如果是線程thread中獲取到了新的數據,需要配合使用hanlder進行刷新.
如果是listView刷新數據,adapter
adapter.notifyDataSetInvalidated();
adapter.notifyDataSetChanged();

Ⅵ android viewpager 數據如何刷新 呢

ViewPager的數據是通過PageAdapter來裝載的,刷新數據的方法有以下:

  1. 調用adapter.notifyDataSetChanged(); 刷新控制項,但是要覆蓋PagerAdapter的getItemPosition方法,並返回return POSITION_NONE;

  2. 利用PagerAdapter的工作機制,就是PagerAdapter的執行順序,PagerAdapter作為ViewPager的適配器,無論ViewPager有多少頁,PagerAdapter在初始化時也只初始化開始的2個View,即調用2次instantiateItem方法。而接下來每當ViewPager滑動時,PagerAdapter都會調用destroyItem方法將距離該頁2個步幅以上的那個View銷毀,以此保證PagerAdapter最多隻管轄3個View,且當前View是3個中的中間一個,如果當前View缺少兩邊的View,那麼就instantiateItem,如里有超過2個步幅的就destroyItem。

  3. 每當Adapter調用instantiateItem時,運用View.setTag方法將該View標識。當需要更新這個View的數據時,通過調用ViewPager.findViewWithTag方法找到相應的View,然後更新View中的數據。

Ⅶ android View.invalidate 方法是怎樣做到局部刷新的

5767778開線程 用handle 進行刷新 想刷哪塊就刷那塊。Handler // 在onCreate()中開啟線程 new Thread(new GameThread()).start();、 // 實例化一個handler Handler myHandler = new Handler() { //接收到消息後處理 public void handleMessage(Message msg) { switch (msg.what) { case Activity01.REFRESH: mGameView.invalidate(); //刷新界面 break; } super.handleMessage(msg); } }; class GameThread implements Runnable { public void run() { while (!Thread.currentThread().isInterrupted()) { Message message = new Message(); message.what = Activity01.REFRESH; //發送消息 Activity01.this.myHandler.sendMessage(message); try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } }

閱讀全文

與Androidview刷新機制相關的資料

熱點內容
怎麼把安卓視頻傳到蘋果上面 瀏覽:79
手機拍鬼片用什麼app 瀏覽:640
爬山虎app是干什麼用的 瀏覽:505
有哪些寫給程序員的歌 瀏覽:49
成都市命令 瀏覽:993
建立系列文件夾 瀏覽:983
蘋果開機白屏帶文件夾問號 瀏覽:733
體驗服為什麼伺服器會關閉 瀏覽:41
酒店命令 瀏覽:750
中走絲線切割編程視頻 瀏覽:80
衣服壓縮袋手泵原理 瀏覽:714
通達信編程書籍 瀏覽:981
車用壓縮天然氣瓶閥 瀏覽:971
鞋的程序員 瀏覽:259
車的壓縮比是什麼意思 瀏覽:202
網站源碼怎麼傳到文件夾 瀏覽:914
海南壓縮機在哪裡 瀏覽:491
電腦文件夾清晰的文件結構 瀏覽:839
如何把蘋果手機的app轉到安卓 瀏覽:305
java同步並發 瀏覽:249