Android特效--粒子效果之雨

时间:2023-03-08 18:26:19
Android特效--粒子效果之雨

1. 单个雨点的行为

2. 完善雨点的行为和构造下雨场景

3. 在XML中定义可以控制下雨的属性

------------------------------------------------------

1. 单个雨点的行为: 一条线段在运动

起始点:startX , startY;

终止点:stopX,  stopY;

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAASUAAACpCAIAAADiALw3AAAa+0lEQVR4nO2d7XMTR57Hf//FVl5QEHKpo1DhtUlINhXi4s0ldVe6Y5egtV2VraXO2dwmq5RtAhgbbxHb8drKg5dgcLhzcJxcRMJBYkAQgtmcZbuAkN0lgHkSWTDCD5KfgrCxPbLY070YeaZnunskjR7a9vw+1ZWaGXX3fDWZL9MaT38HvKmx5t++aDw+3HhkuPHI8HtHWeWr4TX/+gWrqdtpA7uLteKyg83p9qpr5KrbaYMYmtYqRGXWR+oWm92u7FXZSmwkdbidtrlltYMEqylCNd8SsSCQYvucfz7ScCjw9v8EGg4F3v4i0HAo0HA00HA00HAooGxf9fzRVHahdR+CLGAgxfa/LP3Liuc9cYv5HeAlAVlEgGgBRrjsmlEjgix0QLQABLEQIFoAglgIEC0AQSwEmGjT6e062nX18+677p7goi+fd9892nW109uV7iOPWBEw0aa960Z3b/DW3cDg0OIvt+4GunuD7V030n3kESsCJtoc6Bm6OxC4+cOta9d9dIlGo9Fo9Dfv2MgSjUaZled/ufnDrbsDgQM9Q+k+8ogVARNt3D3BwaHA1es+uihmixLIq8z6C6IMDgXcPcF0H3nEioCJNu6e4MCgxm+XvqiQC+2u9PjtVGUu5G0/lWzDju15AACO/9ItJ1cGBtFvSHoAE23cPcH+wUDvNZ9cLh6uoK9myqf0lrnSUZ5HPNmYV3lCX4EoX1fmQl7517wK2q6goPmar/ea70RFHvyypZdaTrb0o9+QNAEm2sh+21vt2lvt2lvdMDvcOjvcOt3XPHbpHcVde6sb5KL67apPWzrKc8GxL7ba7ABwtFB1EiyarpTS7IDc7R30crJ9ot+QdAHGH+/atau2tla30d0TvD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BiBaQKvX19V6v1+PxlJSUuN1xfrV8uHNNaDISmpwNPVD9FnowG5qM3HsQ+XDnU7yGnN9vxtkhZGvW1ZFpstQTPshuaXmMKa3MwBUkI4BoAaki+83r9ba3txcXFxsPLD/4/dNjE7Pj92fHJsKK38buz45PhMcnZpurnuY11JzT9EhQc22QvclMstIZlk7RSUfCR5wLMJUpwq6GZAQQLSBVkrq+7d7xs+F74eF74eGQ6rfhUHgkFB4OhZt2PMNrqL2GkKcsLwBBPoGN8kKURpqmqSd8aFJMGPJYmSLcGAckvYBoAamS1O+3xvJnhn6UzvnHX2s781rbGdlvQ+PhoR+lwLj0XnlifmOMzTTZIU7WpYrO6pirog3pMJ3wofwaYzlWk2qgyxThVUMyAIgWkCpJ3Z9s2PZs/6h0zj+ev/kz2WwDo1L/qNQ/JvWPhl3bnuU11A7uGHdGiKgSorLhLRT1g0Tvdqgb2ck8+oEiWx5lK141JP2AaAGp4vF4Ev/7W90ba/3Dkn9EOnl54OTlAf+I5B+R7oxI/mHJPzLz1pa1GZWaJXBMOI8B0QKySnXZ2r7gzO2g1Bec6Yv9d6YvOHM7OHM7OFNTthj8ho+XzGdAtICssrP0uR0l+VWl+VWl+TuI/1aV5FeV5O8sfU60wNSIDWTx4jZ/AdECEMRCgGgBCGIhQLQABLEQIFoAglgIEC0AQSzE/wNg2DPpQvjxmQAAAABJRU5ErkJggg==" alt="" />

自定义View的框架类

public abstract class BaseView extends View {

    Thread thread;

    public BaseView(Context context, AttributeSet attrs) {
super(context, attrs);
} public BaseView(Context context) {
super(context);
} @Override
final protected void onDraw(Canvas canvas) {
//禁止子类覆盖,用final
if(thread == null ) {
thread = new MyThread();
thread.start();
} else{
drawSub(canvas);
}
} protected abstract void logic();
protected abstract void drawSub(Canvas canvas); @Override
final protected void onDetachedFromWindow() {
// 离开屏幕时结束
//onDetachedFromWindow在销毁资源(既销毁view)之后调用
running = false;
super.onDetachedFromWindow();
}
private boolean running = true;
class MyThread extends Thread {
@Override
public void run() {
while(running) {
logic();
postInvalidate(); //线程中更新绘制,重新调用onDraw方法
try {
Thread.sleep(50); //速度太快肉眼看不到,要睡眠
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} }

自定义单个雨点RainView

public class RainView extends BaseView {

    private float startX = 0;
private float startY = 0; // Start Point
private float stopX = 0;
private float stopY = 0; // Stop Point
private float sizeX = 0;
private float sizeY = 0;
private Paint paint; public RainView(Context context) {
super(context);
// TODO Auto-generated constructor stub
} public RainView(Context context, AttributeSet attrs) {
super(context, attrs);
sizeX = 10;
sizeY = 30;
//单个雨点的形状,用线段表示
startX = 100;
startY = 0;
stopX = startX + sizeX;
stopY = startY + sizeY; paint = new Paint();
paint.setColor(0xffffffff);
} @Override
protected void logic() {
// 让雨点运动
float opt = 0.3f;
startX += sizeX * opt;
stopX += sizeX * opt;
startY += sizeY * opt;
stopY += sizeY * opt; // 雨点出了屏幕的时候让它回到起始点
if(startY > getHeight() || startX > getWidth()){
startX = 100;
startY = 0;
stopX = startX + sizeX;
stopY = startY + sizeY;
}
} @Override
protected void drawSub(Canvas canvas) {
// drawSub 完成绘制操作
canvas.drawLine(startX, startY, stopX, stopY, paint);
} }

在布局文件中使用

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.carloz.raineffect.MainActivity" > <com.carloz.raineffect.v1.single_rain.RainView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000" /> </FrameLayout>

效果如下:

Android特效--粒子效果之雨

2. 完善雨点的行为和构造下雨场景

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAASAAAADOCAIAAAAR2EiRAAAZ6ElEQVR4nO2d/28bZZ7HP/8F4oeqpRy6qhYsDRQWUaL+ciCdLJU7KVIigbbaALfLGiVhgRKaVYlNSL2FkmuW0pNoSDnMUtGle3V3S7egTFm1pb9wtAe0YnVNvWm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EWyBtVGem9Lw1bwAtgCCCDOALYAgwgxgCyCIMAPYAggizAC2AIIIM9CYw5RKpVKp9PSuCLuVSqXGHJ0gsPh/++vfy4CyOKMAAAAASUVORK5CYII=" alt="" />

2.1 将雨点 效果和 行为 抽象出来

* 雨点的速度和长度是可变的

* 雨点的倾斜角度是可变的

* 雨点的位置是可变的

public class RainItem {

    private int width;
private int height; public RainItem(int width, int height) {
this.width = width;
this.height = height;
init();
} private float startX = 0;
private float startY = 0; // Start Point
private float stopX = 0;
private float stopY = 0; // Stop Point
private float sizeX = 0;
private float sizeY = 0;
private Paint paint;
private float opt;
private Random random; public void init() {
// TODO Auto-generated method stub
random = new Random(); sizeX = 1 + random.nextInt(10); // 随机改变雨点的角度
sizeY = 1 + random.nextInt(20);
// 单个雨点的形状,用线段表示
startX = random.nextInt(this.width);
startY = random.nextInt(this.height); // 随机改变雨点的位置
stopX = startX + sizeX;
stopY = startY + sizeY; paint = new Paint();
paint.setColor(0xffffffff);
} public void draw(Canvas canvas) {
canvas.drawLine(startX, startY, stopX, stopY, paint);
} public void move() {
// 让雨点运动
opt = 0.2f + random.nextFloat(); // 随机改变雨点的速度和长度
startX += sizeX * opt;
stopX += sizeX * opt;
startY += sizeY * opt;
stopY += sizeY * opt; // 雨点出了屏幕的时候让它回到起始点
if (startY > this.height || startX > this.width) {
startX = random.nextInt(this.width);
startY = random.nextInt(this.height);
stopX = startX + sizeX;
stopY = startY + sizeY;
}
} }

2.2 定义多个雨点

在 添加抽象 BaseView 方法: protected abstract void init();

class MyThread extends Thread {
@Override
public void run() {
init();
while(running) {
logic();
postInvalidate(); //线程中更新绘制,重新调用onDraw方法
try {
Thread.sleep(50); //速度太快肉眼看不到,要睡眠
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class RainView extends BaseView {

    ArrayList<RainItem> rainList = new ArrayList<RainItem>();
int size = 80; public RainView(Context context) {
super(context);
} public RainView(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
protected void init() {
// TODO Auto-generated method stub
for (int i = 0; i < size; i++) {
RainItem item = new RainItem(getWidth(), getHeight());
rainList.add(item);
}
} @Override
protected void logic() {
for (RainItem item : rainList) {
item.move();
}
} @Override
protected void drawSub(Canvas canvas) {
// drawSub 完成绘制操作
for (RainItem item : rainList) {
item.draw(canvas);
}
} }

效果如下:

Android特效--粒子效果之雨

3. 在XML中定义可以控制下雨的属性