本文实例介绍了android实现粒子雨效果的实现过程,分享给大家供大家参考,具体内容如下
先看看效果图:
具体实现方法:
1.baseview主要是设定雨滴要实现的动作,只是先设定,也就是抽象方法,在子类中实现其方法
2.rainitems封装雨滴类
3.rainitems对雨滴集合创建到面板中,显示出来,具体实现就是在这个类中
一、baseview封装类,子类继承后实现方法即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
public abstract class baseview extends view {
private control thread;
public baseview(context context, attributeset attrs) {
super (context, attrs);
}
public baseview(context context) {
super (context);
}
//封装,构造画面,子类继承后需要重写
protected abstract void drawsub(canvas canvas);
//封装移动方法,子类继承后需要重写
protected abstract void move();
//封装的初始化方法
protected abstract void init();
@override
protected final void ondraw(canvas canvas) {
//启动线程
if (thread == null ) {
thread = new control();
thread.start();
} else {
drawsub(canvas);
}
}
public class control extends thread{
@override
public void run() {
init();
while ( true ){
move();
//相当于刷新画布
postinvalidate();
try {
sleep( 30 );
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
}
}
|
二、rainitem雨点类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
public class rainitem {
private int height;
private int width;
private float startx;
private float starty;
private float stopx;
private float stopy;
private float sizex;
private float sizey;
private float of = 0 .5f;
private paint paint;
private random random = new random();
public rainitem( int height, int width) {
this .height = height;
this .width = width;
init();
}
public void init() {
//startx和y对应的分别是起止位置
sizex = 1 + random.nextint( 10 );
sizey = 10 + random.nextint( 20 );
startx = random.nextint(width);
starty = random.nextint(height);
stopx = startx + sizex;
stopy = starty + sizey;
of = ( float ) ( 0.2 + random.nextfloat());
paint = new paint();
}
/**
* 绘画雨滴
* @param canvas
*/
public void draw(canvas canvas) {
paint.setargb( 255 , random.nextint( 255 ), random.nextint( 255 ), random.nextint( 255 ));
canvas.drawline(startx, starty, stopx, stopy, paint);
}
/**
* 雨滴的移动行为
*/
public void movestep() {
//size*of这个是用来控制速度,所谓的速度就是线条增加的速度
startx += sizex*of;
stopx += sizex*of;
starty += sizey*of;
stopy += sizey*of;
//如果超出边界则重新运行
if (starty>height) {
init();
}
}
}
|
三、rainplay具体实现的类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
public class rainplay extends baseview {
list<rainitem> list = new arraylist<rainitem>();
//控制雨滴的数量
private int num = 80 ;
public rainplay(context context) {
super (context);
}
public rainplay(context context, attributeset attrs) {
super (context, attrs);
//与xml链接起来
typedarray ta = context.obtainstyledattributes(attrs, r.styleable.rainview);
num = ta.getinteger(r.styleable.rainview_rainnum, 80 );
ta.recycle();
}
@override
protected void drawsub(canvas canvas) {
for (rainitem item : list) {
item.draw(canvas);
}
}
@override
protected void move() {
for (rainitem item : list) {
item.movestep();
}
}
/**
* 因为获取长宽是放在layout之后才可以获取,所以需要
* 放在线程里面初始化
*/
@override
protected void init() {
for ( int i = 0 ; i < num; i++) {
rainitem item = new rainitem(getheight(), getwidth());
list.add(item);
}
}
}
|
四、value与xml文件
1
2
3
4
5
6
|
<?xml version= "1.0" encoding= "utf-8" ?>
<resources>
<declare-styleable name = "rainview" >
<attr name= "rainnum" format= "integer" />
</declare-styleable>
</resources>
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
xmlns:an= "http://schemas.android.com/apk/res/com.niuli.rain"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
>
<com.niuli.rain.rainplay
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:background= "#ff000000"
an:rainnum = "100" />
</framelayout>
|
希望本文所述对大家学习android软件编程有所帮助。