展会信息港展会大全

Android中自定义View的invalidate,Handler和postInvalidate
来源:互联网   发布日期:2016-01-14 09:50:59   浏览:2339次  

导读:[代码]Activity01.java001packagecom.yarin.android.Examples_05_01;002003importandroid.app.Activity;004importandroid.os.Bundle;005importandroid.os.Handler;006importandroid.os.Message;007importandroid......

[代码] Activity01.java

001

package com.yarin.android.Examples_05_01;

002

003

import android.app.Activity;

004

import android.os.Bundle;

005

import android.os.Handler;

006

import android.os.Message;

007

import android.view.KeyEvent;

008

import android.view.MotionEvent;

009

010

public class Activity01 extends Activity

011

{

012

private static final int REFRESH= 0x000001;

013

014

/** 声明GameView类对象 */

015

private GameViewmGameView = null;

016

017

/** Called when the activity is first created. */

018

@Override

019

public void onCreate(Bundle savedInstanceState)

020

{

021

super.onCreate(savedInstanceState);

022

023

/** 实例化GameView对象 */

024

this.mGameView = new GameView(this);

025

026

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

027

setContentView(mGameView);

028

029

// 开启线程

030

new Thread(new GameThread()).start();

031

}

032

033

// Handler myHandler = new Handler()

034

// {

035

////接收到消息后处理

036

//public void handleMessage(Message msg)

037

//{ switch (msg.what)

038

//{

039

//case Activity01.REFRESH:

040

//mGameView.invalidate();

041

//break;}

042

//super.handleMessage(msg);

043

//}

044

// };

045

//

046

// class GameThread implements Runnable

047

// { public void run()

048

//{while (!Thread.currentThread().isInterrupted())

049

//{ Message message = new Message();

050

//message.what = Activity01.REFRESH;

051

////发送消息

052

//Activity01.this.myHandler.sendMessage(message);

053

//try

054

//{Thread.sleep(100);

055

//}

056

//catch (InterruptedException e)

057

//{Thread.currentThread().interrupt();

058

//}

059

//}

060

//}

061

// }

062

// **

063

//* 当然可以将GameThread类这样写

064

//* 同样可以更新界面,并且不在需要

065

//* Handler在接受消息

066

class GameThread implements Runnable

067

{

068

public void run()

069

{

070

while (!Thread.currentThread().isInterrupted())

071

{

072

try

073

{

074

Thread.sleep(100);

075

}

076

catch (InterruptedException e)

077

{

078

Thread.currentThread().interrupt();

079

}

080

//使用postInvalidate可以直接在线程中更新界面

081

mGameView.postInvalidate();

082

//如果用invalidate()则会出错

083

//mGameView.invalidate();

084

//FATAL EXCEPTION: Thread-8

085

//android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

086

//at android.view.ViewRoot.checkThread(ViewRoot.java:2802)

087

//at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)

088

//at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)

089

//at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)

090

//at android.view.View.invalidate(View.java:5139)

091

//at com.yarin.android.Examples_05_01.Activity01$GameThread.run(Activity01.java:81)

092

//at java.lang.Thread.run(Thread.java:1096)

093

//查看View源文件

094

//public void postInvalidate(int left, int top, int right, int bottom) {

095

//postInvalidateDelayed(0, left, top, right, bottom);

096

//}

097

//

098

//public void postInvalidateDelayed(long delayMilliseconds) {

099

//// We try only with the AttachInfo because there's no point in invalidating

100

//// if we are not attached to our window

101

//if (mAttachInfo != null) {

102

//Message msg = Message.obtain();

103

//msg.what = AttachInfo.INVALIDATE_MSG;

104

//msg.obj = this;

105

//mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);

106

//}

107

108

}

109

}

110

}

111

112

113

//详细事件处理见第三章

114

//当然这些事件也可以写在GameView中

115

//触笔事件

116

public boolean onTouchEvent(MotionEvent event)

117

{

118

return true;

119

}

120

121

//按键按下事件

122

public boolean onKeyDown(int keyCode, KeyEvent event)

123

{

124

return true;

125

}

126

127

//按键弹起事件

128

public boolean onKeyUp(int keyCode, KeyEvent event)

129

{

130

switch (keyCode)

131

{

132

//上方向键

133

case KeyEvent.KEYCODE_DPAD_UP:

134

mGameView.y-=3;

135

break;

136

//下方向键

137

case KeyEvent.KEYCODE_DPAD_DOWN:

138

mGameView.y+=3;

139

break;

140

}

141

return false;

142

}

143

144

public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)

145

{

146

return true;

147

}

148

}

赞助本站

人工智能实验室

相关热词: 自定义View

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港