Android中利用画图类和线程画出闪烁的心形,送给亲爱的他(她)
本文讲解主要涉及的知识点:
1.线程控制
2.画图类
3.心形函数
大家先看图片:
因为前一段时间在写画图类,刚好有一个线程控制画图闪烁的,我就想说我能不能做一个心形闪烁的,出来的效果就如图,先贴再讲解代码:
里面设置两个类,一个是我们的activity类,这个类用来显示示图,然后建一个继承Su易做图ceView的类,我们在这里面画图。先贴两个累的代码:
主类名:GameMainActivity,画图类类名:Love.
1 package com.cz.game.demo;
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 }
画图类:
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.Su易做图ceHolder;
13 import android.view.Su易做图ceView;
14
15 /**
16 * @author CZ
17 *
18 */
19 public class Love extends Su易做图ceView implements Su易做图ceHolder.Callback,
20 Runnable {
21
22 boolean mbloop = false;
23 Su易做图ceHolder mSu易做图ceHolder = 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 mSu易做图ceHolder = this.getHolder();
34 mSu易做图ceHolder.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.Su易做图ceHolder.Callback#su易做图ceChanged(android.view.Su易做图ceHolder
45 * , int, int, int)
46 */
47 @Override
48 public void su易做图ceChanged(Su易做图ceHolder 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.Su易做图ceHolder.Callback#su易做图ceCreated(android.view.Su易做图ceHolder
59 * )
60 */
61 @Override
62 public void su易做图ceCreated(Su易做图ceHolder holder) {
63 // TODO Auto-generated method stub
64 new Thread(this).start();
65 }
66
67 /*
68 * (non-Javadoc)
69 *
70 * @seeandroid.view.Su易做图ceHolder.Callback#su易做图ceDestroyed(android.view.
71 * Su易做图ceHolder)
72 */
73 @Override
74 public void su易做图ceDestroyed(Su易做图ceHolder 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 (mSu易做图ceHolder) {
94 Draw();
95 }
96 &nb
补充:移动开发 , Android ,