刚学习了自定义view,就按照极客学院的教程做了粒子雨效果,主要用到绘画线条和多线程,其中的抽象类设计方法值得学习,
1.baseview主要是设定雨滴要实现的动作,只是先设定,也就是抽象方法,在子类中实现其方法
2.Rainitems封装雨滴类
3.Rainitems对雨滴集合创建到面板中,显示出来,具体实现就是在这个类中
一.baseview封装类,子类继承后实现方法即可
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雨点类
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具体实现的类
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文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name = "RainView">
<attr name="rainnum" format="integer"/>
</declare-styleable>
</resources>
<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>
效果图’
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u012706811/article/details/47843495