`
mmdev
  • 浏览: 12915792 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

详解Android中自定义View的invalidate,Handler和postInvalidate

 
阅读更多

package com.yarin.android.Examples_05_01;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class Activity01 extends Activity
{
private static final intREFRESH= 0x000001;

/* 声明GameView类对象 */
private GameViewmGameView= null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

/* 实例化GameView对象 */
this.mGameView = new GameView(this);

// 设置显示为我们自定义的View(GameView)
setContentView(mGameView);

// 开启线程
new Thread(new GameThread()).start();
}

//HandlermyHandler= 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();
//}
//}
//}
//}
//**
// * 当然可以将GameThread类这样写
// * 同样可以更新界面,并且不在需要
// * Handler在接受消息
class GameThread implements Runnable
{
public void run()
{
while (!Thread.currentThread().isInterrupted())
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
//使用postInvalidate可以直接在线程中更新界面
mGameView.postInvalidate();
//如果用invalidate()则会出错
//mGameView.invalidate();
//FATAL EXCEPTION: Thread-8
// android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
// at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
// at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
// at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
// at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
// at android.view.View.invalidate(View.java:5139)
// at com.yarin.android.Examples_05_01.Activity01$GameThread.run(Activity01.java:81)
// at java.lang.Thread.run(Thread.java:1096)
// 查看View源文件
//public void postInvalidate(int left, int top, int right, int bottom) {
// postInvalidateDelayed(0, left, top, right, bottom);
// }
//
// public void postInvalidateDelayed(long delayMilliseconds) {
// // We try only with the AttachInfo because there's no point in invalidating
// // if we are not attached to our window
// if (mAttachInfo != null) {
// Message msg = Message.obtain();
// msg.what = AttachInfo.INVALIDATE_MSG;
// msg.obj = this;
// mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
// }

}
}
}


//详细事件处理见第三章
//当然这些事件也可以写在GameView中
//触笔事件
public boolean onTouchEvent(MotionEvent event)
{
return true;
}

//按键按下事件
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return true;
}

//按键弹起事件
public boolean onKeyUp(int keyCode, KeyEvent event)
{
switch (keyCode)
{
//上方向键
case KeyEvent.KEYCODE_DPAD_UP:
mGameView.y-=3;
break;
//下方向键
case KeyEvent.KEYCODE_DPAD_DOWN:
mGameView.y+=3;
break;
}
return false;
}

public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
{
return true;
}
}

分享到:
评论

相关推荐

    android中Invalidate和postInvalidate的更新view区别

    Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用。 Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在...

    Android中invalidate()和postInvalidate() 的区别及使用方法

    Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用。本文给大家介绍Android中invalidate()和postInvalidate() 的区别及使用...

    Android中View绘制流程以及invalidate()

    Android中View控件的Demo,值得学习。实现画多个圆圈的效果。

    Android中View绘制流程以及invalidate()等相关方法分析

    Android中View绘制流程以及invalidate()等相关方法分析

    《Android自定义组件开发详解》

    6.4.2 读取来自style和theme中的属性 181 6.5 案例1:圆形ImageView组件 186 6.6 案例2:验证码组件CodeView 190 6.7 练习作业 202 第七章 自定义容器 204 7.1 概述 204 7.2 ViewGroup类 205 7.2.1 ViewGroup常用...

    Android自定义View控件实现刷新效果

    三种得到LinearInflater的方法 a. LayoutInflater inflater = getLayoutInflater(); b....(LayoutInflater)context....onDraw 方法绘图,invalidate刷新界面。 效果图: 点击一下换颜色 onDraw画完图后,给控件设置点

    Android 实现界面刷新的几种方法

    Android程序中可以使用的界面刷新方法有两种,分别是利用Handler和利用postInvalidate()来实现在线程中刷新界面。 利用Handler刷新界面 实例化一个Handler对象,并重写handleMessage方法调用invalidate()实现界面...

    【Android】自定义圆形进度条效果(有进度标识点)

    this.postInvalidate(); } /** * 进度标注点的动画 * * @param fromDegrees * @param toDegrees * @return */ private Animation pointRotationAnima(float fromDegrees, float toDegrees) { int ...

    Android自定义View的实现方法实例详解

    一、自绘控件 ...新建一个CounterView继承自View,代码如下所示: 可以看到,首先我们在CounterView的构造函数中...通过 Android视图状态及重绘流程分析,带你一步步深入了解View(三) 这篇文章的学习我们都已经知道,调用

    自定义view计数的实现

    一个简单的自定义view的实现。设计到paint,自定义属性,invalidate,canvas的使用

    浅谈Android invalidate 分析

    1. invalidate 和 postInvalidate 的关系 postInvalidate 是通过 Handler 切换回到主线程,然后在调用 invalidate 的,源码: public void postInvalidate() { postInvalidateDelayed(0); } public void ...

    Android中刷新界面的二种方法

    Android界面刷新方法有两种,分别是利用Handler和利用postInvalidate()来实现在线程中刷新界面。 利用Handler刷新界面 实例化一个Handler对象,并重写handleMessage方法调用invalidate()实现界面刷新;而在线程中...

    android-demo:自定义View

    android-demo自定义View一、自定义课表View ClassSchedule二、修改MPAndroidChart开源类库的柱状图使其左右滑动,详见BarChartActivity左右滑动的方法是让其先绘制,绘制完后对图表进行放大处理,添加以下代码mChart...

    android 快速翻书

    照比較正常的作法, 如果不是做Game之類的應該通常會新增thread處理工作後, 使用handler傳送msg去View#invalidate畫面, 或是在非UI thread呼叫View#postInvalidate. 所以如果需要比較高效的畫面更新就可以用看看...

    Android自定义控件RatingBar调整字体大小

    这是一个类似于RatingBar的控件,然而配置RatingBar的样式难以实现这样的效果,如选中的图案和上面的... 在onTouchEvent()方法中处理ACTION_DOWN和ACTION_MOVE事件,调用invalidate()方法引起View的重绘,以更新视图

    分析invalidate流程例子

    分析invalidate流程例子

    Android自定义进度条的圆角横向进度条实例详解

    1.本文将向你介绍自定义进度条的写法,比较简单,但还是有些知识点是需要注意的: invalidate()方法 ...invalidate()是用来刷新View的,必须是在UI线程中进行工作。比如在修改某个view的显示时, 调用invalida

Global site tag (gtag.js) - Google Analytics