Android自定义View-invalidate()
最近要用一个自定义的View来实现坐标的功能,但碰到问题,数据更新之后,无论在我main里面还是view里面的处理方法中都无法刷新界面
后面看了下面的东西搞定问题
原文:http://blog.csdn.net/veryitman/article/details/6695516
【Android自定义View-invalidate()】
View编程(2): invalidate()再探博客中,说了加载View的onDraw()方法的时机以及invalidate()方法的作用。
事实上,远远没有您想象的那么简单。为了写好这篇博客,还是拿例子说事吧。
[java]view plain copy print ?
- package mark.zhang;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.RectF;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- public class ViewDrawTestActivity extends Activity {
- // 用于测试
- static int times = 1;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- MyView mView = new MyView(this);
- mView.invalidate();
- //setContentView(mView);
- }
- /**
- * 内部类,继承View
- *
- * @author mark
- */
- class MyView extends View {
- MyView(Context context) {
- super(context);
- }
- Paint vPaint = new Paint(); // 绘制样式物件
- int i = 0; // 弧形角度
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- Log.d("mark", "this run onDraw() " + (times++) + " times!");
- // 设定绘图样式
- vPaint.setColor(0xff00ffff); // 画笔颜色
- vPaint.setAntiAlias(true); // 反锯齿
- vPaint.setStyle(Paint.Style.STROKE);
- // 绘制一个弧形
- canvas.drawArc(new RectF(60, 120, 260, 320), 0, i, true, vPaint);
- // 弧形角度
- if ((i += 10) > 360) {
- i = 0;
- }
- // 重绘, 再一次执行onDraw 程序
- // invalidate();
- }
- }
- }
[java]view plain copy print ?
- mView.invalidate();
呵呵,onDraw()方法并没有执行!那么是不是因为没有调用setContentVIew()方法呢?修改onCreate()方法:
[java]view plain copy print ?
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- MyView mView = new MyView(this);
- mView.invalidate();
- setContentView(mView);
- mView.invalidate();
- }
[html]view plain copy print ?
- D/mark(251): this run onDraw() 1 times!
分析一下,invalidate()方法的源码吧,在这里也许可以找到答案。
[java]view plain copy print ?
- /**
- * 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;
- final AttachInfo ai = mAttachInfo;
- if (p != null && ai != null) {
- final Rect r = ai.mTmpInvalRect;
- r.set(0, 0, mRight - mLeft, mBottom - mTop);
- // Don't call invalidate -- we don't want to internally scroll
- // our own bounds
- p.invalidateChild(this, r);
- }
- }
- }
通过千辛万苦的search,终于找到ViewParen的实现类ViewRoot:
[java]view plain copy print ?
- /**
- * The top of a view hierarchy, implementing the needed protocol between View
- * and the WindowManager.This is for the most part an internal implementation
- * detail of {@link WindowManagerImpl}.
- *
- * {@hide}
- */
- @SuppressWarnings({"EmptyCatchBlock"})
- public final class ViewRoot extends Handler implements ViewParent, View.AttachInfo.Callbacks { }
[java]view plain copy print ?
- public void invalidateChild(View child, Rect dirty) {
- checkThread();
- if (DEBUG_DRAW) Log.v(TAG, "Invalidate child: " + dirty);
- if (mCurScrollY != 0 || mTranslator != null) {
- mTempRect.set(dirty);
- dirty = mTempRect;
- if (mCurScrollY != 0) {
- dirty.offset(0, -mCurScrollY);
- }
- if (mTranslator != null) {
- mTranslator.translateRectInAppWindowToScreen(dirty);
- }
- if (mAttachInfo.mScalingRequired) {
- dirty.inset(-1, -1);
- }
- }
- mDirty.union(dirty);
- if (!mWillDrawSoon) {
- scheduleTraversals();
- }
- }
[java]view plain copy print ?
- if (!mWillDrawSoon) {
- scheduleTraversals();
- }
[java]view plain copy print ?
- public void scheduleTraversals() {
- if (!mTraversalScheduled) {
- mTraversalScheduled = true;
- sendEmptyMessage(DO_TRAVERSAL);
- }
- }
[java]view plain copy print ?
- public void handleMessage(Message msg) {
- switch (msg.what) {
- // 、、、
- case DO_TRAVERSAL:
- // 、、、
- performTraversals();
- }
- }
[java]view plain copy print ?
- mView.draw(canvas);
[java]view plain copy print ?
- if (!dirtyOpaque) onDraw(canvas);
调用关系,请看草图!
推荐阅读
- android第三方框架(五)ButterKnife
- Android中的AES加密-下
- 带有Hilt的Android上的依赖注入
- SpringBoot调用公共模块的自定义注解失效的解决
- python自定义封装带颜色的logging模块
- 列出所有自定义的function和view
- android|android studio中ndk的使用
- Android事件传递源码分析
- RxJava|RxJava 在Android项目中的使用(一)
- Android7.0|Android7.0 第三方应用无法访问私有库