首页 > 程序开发 > 移动开发 > Android >

Android线程实例讲解

2011-10-29

本文只是一个关于Android线程之间通信的例子,高手请自动略过。例子主要实现如下功能:进度条,音乐播放进度显示,图片的移动。游戏部分源码:LoadBar.java1. package com.dance.mrljdx;2.3. import android....

本文只是一个关于Android线程之间通信的例子,高手请自动略过。例子主要实现如下功能:进度条,音乐播放进度显示,图片的移动。

游戏部分源码:

LoadBar.java

1. package com.dance.mrljdx;

2.

3. import android.app.Activity;

4. import android.content.Intent;

5. import android.os.Bundle;

6. import android.os.Handler;

7. import android.os.Message;

8. import android.util.DisplayMetrics;

9. import android.util.Log;

10. import android.widget.ProgressBar;

11. import android.widget.TextView;

12.

13. public class LoadBar extends Activity {

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

15. TextView txview=null,txload=null;

16. ProgressBar pBar=null;

17. protected static final int stop=1;

18. protected static final int start=2;

19. int intCounter=1;

20. @Override

21. public void onCreate(Bundle savedInstanceState) {

22. super.onCreate(savedInstanceState);

23. setContentView(R.layout.loadbar);

24. //DisplayMetrics 获取屏幕信息

25. DisplayMetrics dm=new DisplayMetrics();

26. getWindowManager().getDefaultDisplay().getMetrics(dm);

27. int screenwith=dm.widthPixels;

28. int screenheigh=dm.heightPixels;

29. txview=(TextView)findViewById(R.id.textwh);

30. txload=(TextView)findViewById(R.id.loadtext);

31. pBar=(ProgressBar)findViewById(R.id.pb);

32. txview.setText("屏幕宽度:"+screenwith+"\n屏幕长度:"+screenheigh); //通过线程来改变ProgressBar的值

33.

34. new Thread(new Runnable() {

35. public void run()

36. {

37. for (int i = 0; i <=pBar.getMax(); i++)

38. {

39. try

40. {

41. Thread.sleep(100);

42. iintCounter=i+1;

43. if (i == pBar.getMax())

44. {

45. Message m = new Message();

46. m.what=LoadBar.stop; //what是一个整形值

47. LoadBar.this.myMessageHandler.sendMessage(m);

48. //将处理信息发送给myMessageHandler处理

49. break;

50. }

51. else

52. {

53. Message m = new Message();

54. m.what=LoadBar.start;

55. LoadBar.this.myMessageHandler.sendMessage(m);

56. }

57. }

58. catch (Exception e)

59. {

60. e.printStackTrace();

61. }

62. }

63. }

64. }).start();

65. }

66. //重构Handler方法

67. Handler myMessageHandler = new Handler()

68. {

69. // @Override

70. public void handleMessage(Message msg)

71. {

72. switch (msg.what)

73. {

74. //ProgressBar已经是对大值

75. case LoadBar.stop:

76. //设置Intent,跳转到其他Activity

77. Intent mIntent=new Intent();

78. mIntent.setClass(LoadBar.this, Menu.class);

79. startActivity(mIntent);

80. Log.d("Lua", "Main==class");

81. Thread.currentThread().interrupt();

82. finish();

83. break;

84. case LoadBar.start:

85. if (!Thread.currentThread().isInterrupted())

86. {

87. // 改变ProgressBar的当前值

88. pBar.setProgress(intCounter);

89. // 设置标题栏中前景的一个进度条进度值

90. Log.d("Lua", "LoadBar线程");

91. txload.setText(" 加载中"+intCounter+"%");

92. }

93. break;

94. }

95. super.handleMessage(msg);

96. }

97. };

98. }

Game.java

1. package com.dance.mrljdx;

2.

3. import android.app.Activity;

4. import android.media.MediaPlayer;

5. import android.os.Bundle;

6. import android.os.Handler;

7. import android.os.Message;

8. import android.util.Log;

9. import android.view.KeyEvent;

10. import android.view.View;

11. import android.view.View.OnClickListener;

12. import android.view.animation.Animation;

13. import android.view.animation.AnimationUtils;

14. import android.widget.ImageButton;

15. import android.widget.ImageView;

16. import android.widget.SeekBar;

17. public class Game extends Activity {

18. MediaPlayer mediaPlayer;

19. ImageButton imgbt1,imgbt2,imgbt3,openbt;

20. OnClickListener listener1=null;

21. OnClickListener listener2=null;

22. OnClickListener listener3=null;

23. OnClickListener listener4=null;

24. OnClickListener openlistener=null;

25. SeekBar sBar;

26. Animation myAnimation;

27. ImageView img1;

28. private static final int start=1;

29. private static final int end=2;

30. // int position=mediaPlayer.getCurrentPosition();

31. int pb,i;

32. double current;

33. @Override

34. public void onCreate(Bundle savedInstanceState) {

35. super.onCreate(savedInstanceState);

36. setContentView(R.layout.game);

37. setListener();

38. imgbt1=(ImageButton)findViewById(R.id.btleft);

39. imgbt1.setOnClickListener(listener1);

40. imgbt2=(ImageButton)findViewById(R.id.btright);

41. imgbt2.setOnClickListener(listener2);

42. imgbt3=(ImageButton)findViewById(R.id.btup);

43. imgbt3.setOnClickListener(listener3);

44. // openbt=(ImageButton)findViewById(R.id.btopen);

45. sBar=(SeekBar)findViewById(R.id.seek);

46. pb=sBar.getProgress();

47. myAnimation= AnimationUtils.loadAnimation(this,R.anim.img);

48. img1=(ImageView) this.findViewById(R.id.img1);

49.

50. }

51. void setListener(){

52. listener1=new OnClickListener() {

53. public void onClick(View v) {

54. initSound();

55. }

56. };

57. listener2=new OnClickListener() {

58. public void onClick(View v) {

59. stopMusic();

60. }

61. };

62. listener3=new OnClickListener() {

63. public void onClick(View v) {

64. img1.setVisibility(View.INVISIBLE);

65. }

66. };

67. openlistener=new OnClickListener() {

68. public void onClick(View v) {

69. // TODO Auto-generated method stub

70. choosMusic();

71. }

72. };

73. }

74. //游戏音效

75. void initSound(){

76. mediaPlayer=MediaPlayer.create(this,R.raw.gf);

77. if(!mediaPlayer.isPlaying()){

78. mediaPlayer.start();//播放声音

79. img1.setVisibility(View.VISIBLE);

80. img1.startAnimation(myAnimation);

81. }

82. new Thread(new Runnable() {

83.

84. public void run() {

85. // TODO Auto-generated method stub

86.

87. while(current<1){

88. Message m=new Message();

89. if(mediaPlayer.isPlaying()){

90. current=(double)mediaPlayer.getCurrentPosition()/mediaPlayer.getDuration();

91. // Log.d("Lua", "==当前音乐是否播放="+mediaPlayer.isPlaying());

92. m.what=Game.start;

93. Game.this.musicHandler.sendMessage(m);

94. }else {

95. Log.d("Lua", "当前音乐是否播放="+mediaPlayer.isPlaying());

96. m.what=Game.end;

97. Game.this.musicHandler.sendMessage(m);

98. break;

99. }

100. }

101. }

102. }).start();

103. }

104. //停止播放音乐

105. void stopMusic(){

106. if(mediaPlayer.isPlaying()){

107. mediaPlayer.stop();//暂停声音

108. }

109. this.finish();

110. }

111. //选择播放的音乐

112. void choosMusic(){

113.

114. }

115. Handler musicHandler = new Handler(){

116. public void handleMessage(Message msg) {

117. switch(msg.what)

118. {

119. case Game.start:

120. if (!Thread.currentThread().isInterrupted())

121. {

122. int i=new Double(current*100).intValue();

123. Log.d("Lua", "==当期音乐播放位置="+i);

124. // 改变ProgressBar的当前值

125. sBar.setProgress(i+1);

126. }

127. break;

128. case Game.end:

129. Log.d("Lua", "当前音乐是否播放="+mediaPlayer.isPlaying());

130. Thread.currentThread().interrupt();

131. break;

132. }

133.

134. }

135. };

136.}

游戏截图:

\

\

本文出自 “追云似梦” 博客

相关文章
最新文章
热点推荐