本文讲解主要涉及的知识点:
1.线程控制
2.画图类
3.心形函数
大家先看图片:
因为前一段时间在写画图类,刚好有一个线程控制画图闪烁的,我就想说我能不能做一个心形闪烁的,出来的效果就如图,先贴再讲解代码:
里面设置两个类,一个是我们的activity类,这个类用来显示示图,然后建一个继承SurfaceView的类,我们在这里面画图。先贴两个累的代码:
主类名:GameMainActivity,画图类类名:Love.
2
3 import android . app . Activity;
4 import android . os . Bundle;
5
6 public class GameMainActivity extends Activity {
7 /* * Called when the activity is first created. */
8
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 }
画图类:
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的了,关于画更好看的心形还有一个函数,大家可以看下:
有兴趣的童鞋可以设置再做一下.
关于这个代码就这么多,所以就不放附件代码了,把apk放上,之前就打算写这一篇博客,没想到在七夕这天写了。有兴趣的童鞋可以把apk下载下来给女友看哦…
本文转自HDDevTeam 51CTO博客,原文链接:http://blog.51cto.com/hddev/632632,如需转载请自行联系原作者