Android实现粒子雨效果

本文实例介绍了Android实现粒子雨效果的实现过程,分享给大家供大家参考,具体内容如下

先看看效果图:

具体实现方法:

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>

希望本文所述对大家学习Android软件编程有所帮助。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#niaoge.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。