Ⅰ 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来装载的,刷新数据的方法有以下:
调用adapter.notifyDataSetChanged(); 刷新控件,但是要覆盖PagerAdapter的getItemPosition方法,并返回return POSITION_NONE;
利用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。
每当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(); } } } }