Android中利用畫圖類和線程畫出閃爍的心形,送給親愛的他(她) – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

本文講解主要涉及的知識點:

1.線程控制

2.畫圖類

3.心形函數

大傢先看圖片:

bluepinkgreen

redqingyellow

 

因為前一段時間在寫畫圖類,剛好有一個線程控制畫圖閃爍的,我就想說我能不能做一個心形閃爍的,出來的效果就如圖,先貼再講解代碼:

 

 

裡面設置兩個類,一個是我們的activity類,這個類用來顯示示圖,然後建一個繼承SurfaceView的類,我們在這裡面畫圖。先貼兩個累的代碼:

主類名:GameMainActivity,畫圖類類名:Love.

1  package com.cz.game.demo;

3  import android.app.Activity;

4  import android.os.Bundle;

6  public class GameMainActivity extends Activity {

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

9      private Love love;

10     @Override

11     public void onCreate(Bundle savedInstanceState) {

12         super.onCreate(savedInstanceState);

13         this.love = new Love(this);

14         setContentView(love);

15     }

16 }

 

畫圖類:

1   /**

2    * 

3    */

4   package com.cz.game.demo;

5  

6   import android.content.Context;

7   import android.graphics.Canvas;

8   import android.graphics.Color;

9   import android.graphics.Paint;

10  import android.graphics.RectF;

11  import android.graphics.Typeface;

12  import android.view.SurfaceHolder;

13  import android.view.SurfaceView;

14 

15  /**

16   * @author CZ

17   * 

18   */

19  public class Love extends SurfaceView implements SurfaceHolder.Callback,

20          Runnable {

21 

22      boolean mbloop = false;

23      SurfaceHolder mSurfaceHolder = null;

24      private Canvas canvas;

25      int miCount = 0;

26      int y = 50;

27 

28      /**

29       * @param context

30       */

31      public Love(Context context) {

32          super(context);

33          mSurfaceHolder = this.getHolder();

34          mSurfaceHolder.addCallback(this);

35          this.setFocusable(true);

36          this.setKeepScreenOn(true);

37          mbloop = true;

38      }

39 

40      /*

41       * (non-Javadoc)

42       * 

43       * @see

44       * android.view.SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder

45       * , int, int, int)

46       */

47      @Override

48      public void surfaceChanged(SurfaceHolder holder, int format, int width,

49              int height) {

50          // TODO Auto-generated method stub

51 

52      }

53 

54      /*

55       * (non-Javadoc)

56       * 

57       * @see

58       * android.view.SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder

59       * )

60       */

61      @Override

62      public void surfaceCreated(SurfaceHolder holder) {

63          // TODO Auto-generated method stub

64          new Thread(this).start();

65      }

66 

67      /*

68       * (non-Javadoc)

69       * 

70       * @seeandroid.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.

71       * SurfaceHolder)

72       */

73      @Override

74      public void surfaceDestroyed(SurfaceHolder holder) {

75          // TODO Auto-generated method stub

76          mbloop = false;

77      }

78 

79      /*

80       * (non-Javadoc)

81       * 

82       * @see java.lang.Runnable#run()

83       */

84      @Override

85      public void run() {

86          // TODO Auto-generated method stub

87          while (mbloop) {

88              try {

89                  Thread.sleep(200);

90              } catch (Exception e) {

91                  // TODO: handle exception

92              }

93              synchronized (mSurfaceHolder) {

94                  Draw();

95              }

96          }

97      }

98 

99      /**

100      * 

101      * Year:2011 Date:2011-7-27 Time:下午06:52:04 Author:CZ TODO

102      */

103     private void Draw() {

104         // TODO Auto-generated method stub

105         canvas = mSurfaceHolder.lockCanvas();

106         try {

107             if (mSurfaceHolder == null || canvas == null) {

108                 return;

109             }

110             if (miCount < 100) {

111                 miCount++;

112             } else {

113                 miCount = 0;

114             }

115             Paint paint = new Paint();

116             paint.setAntiAlias(true);

117             paint.setColor(Color.BLACK);

118             canvas.drawRect(0, 0, 320, 480, paint);

119             switch (miCount % 6) {

120             case 0:

121                 paint.setColor(Color.BLUE);

122                 break;

123             case 1:

124                 paint.setColor(Color.GREEN);

125                 break;

126             case 2:

127                 paint.setColor(Color.RED);

128                 break;

129             case 3:

130                 paint.setColor(Color.YELLOW);

131                 break;

132             case 4:

133                 paint.setColor(Color.argb(255, 255, 181, 216));

134                 break;

135             case 5:

136                 paint.setColor(Color.argb(255, 0, 255, 255));

137                 break;

138             default:

139                 paint.setColor(Color.WHITE);

140                 break;

141             }

142             int i, j;

143             double x, y, r;

144

145             for (i = 0; i <= 90; i++) {

146                 for (j = 0; j <= 90; j++) {

147                     r = Math.PI / 45 * i * (1 – Math.sin(Math.PI / 45 * j))

148                             * 20;

149                     x = r * Math.cos(Math.PI / 45 * j)

150                             * Math.sin(Math.PI / 45 * i) + 320 / 2;

151                     y = -r * Math.sin(Math.PI / 45 * j) + 400 / 4;

152                     canvas.drawPoint((float) x, (float) y, paint);

153                 }

154             }

155

156             paint.setTextSize(32);

157             paint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.ITALIC));

158

159             RectF rect = new RectF(60, 400, 260, 405);

160             canvas.drawRoundRect(rect, (float) 1.0, (float) 1.0, paint);

161             canvas.drawText("Loving You", 75, 400, paint);

162             mSurfaceHolder.unlockCanvasAndPost(canvas);

163         } catch (Exception e) {

164         }

165

166     }

167

168 }

169

 

關於這個程序要講解的幾點:

1. 畫圖的時候你可以繼承View,也可以繼承SurfaceView,這兩者的區別在於:surfaceView是在一個新起的單獨線程中可以重新繪制畫面而View必須在UI的主線程中更新畫面。SurfaceView可以控制表面的格式,比如大小,顯示在屏幕中的位置,最關鍵是的提供瞭SurfaceHolder類,使用getHolder方法獲取,還有涉及的surfaceCreated(SurfaceHolder holder),surfaceDestroyed(SurfaceHolder holder),surfaceChanged(SurfaceHolder holder, int format, int width, int height)方法,而在SurfaceHolder.Callback 接口回調中可以通過重寫來改變這些方法

2.程序其實很簡單, 既然生命瞭Runnable接口,就有相對應的Run方法,在surfaceCreate()的時候開啟線程,線程每隔200ms就刷新一次,這樣我們看到的效果就是閃爍的,每200毫秒 畫一次圖,根據經過的間隔時間來設置畫筆的顏色,然後通過循環描點,畫出心形,然後設置字體大小,畫字和字下面的橫線。

3.關於心形函數,是從一個例子中看來得,關於x和y的得到,

x = r * Math.cos(Math.PI / 45 * j)  * Math.sin(Math.PI / 45 * i) + 320 / 2;  y = -r * Math.sin(Math.PI / 45 * j) + 400 / 4;

320是屏幕的寬度,本來豎屏我設置的是480,可是下面得寫字,就設置為400的瞭,關於畫更好看的心形還有一個函數,大傢可以看下:

5801107438_52486a79a2

 

有興趣的童鞋可以設置再做一下.

關於這個代碼就這麼多,所以就不放附件代碼瞭,把apk放上,之前就打算寫這一篇博客,沒想到在七夕這天寫瞭。有興趣的童鞋可以把apk下載下來給女友看哦…

本文出自 “HDDevTeam” 博客

發佈留言