欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现字母雨的效果

程序员文章站 2024-03-06 11:19:13
首先来看效果: 一、实现原理 在实现过程中,主要考虑整个界面由若干个字母组成的子母线条组成,这样的话把固定数量的字母封装成一个字母线条,而每个字母又封装成一个...

首先来看效果:

Android实现字母雨的效果

一、实现原理

在实现过程中,主要考虑整个界面由若干个字母组成的子母线条组成,这样的话把固定数量的字母封装成一个字母线条,而每个字母又封装成一个对象,这样的话,就形成了如下组成效果:

字母对象--》字母线条对象--》界面效果

每个字母都应该知道自己的位置坐标,自己上面的字母、以及自己的透明度:

class hackcode{
     point p = new point();//每一个字母的坐标
     int alpha = 255;//透明度值 默认255
     string code = "a";//字母的值
  }

而每个子母线条对象都有自己这条线条的初始底部起点,内部的多个字母都是根据线条的初始底部起点依次排列,包含多个字母对象集合,以及这条线条的唯一标示:

class hackline{
  public int num = 0;//用于记录这列的标示
  private point p = new point();//线的初始位置
  list<hackcode> hcs = new arraylist<hackview.hackcode>();//黑客字母的一条线
  }

在初始化的时候创建所有子母线条对象以及字母对象存入集合中:

@override
  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
    super.onmeasure(widthmeasurespec, heightmeasurespec);

    mwidth = getmeasuredwidth();//获取控件宽高
    mheight = getmeasuredheight();
    mhacklines.clear();//清空集合
    initplaydata();//初始化播放数据
  }

  /**
   * 初始化播放数据
   */
  public void initplaydata(){
    inithackline(mwidth/9, mheight/12);
    inithackline(mwidth/9, mheight/7);
    hackline hl;
    for (int i = 3; i < 9; i++) {
      hl= new hackline();
      hl.p.x = mwidth/9*(i+1);
      hl.p.y = mheight/7*(9-i);
      for (int j = 0; j < 7; j++) {
        hackcode hc = new hackcode();
        hc.alpha -= 30*j;
        hc.code = codes[new random().nextint(codes.length)];
        hc.p.x = hl.p.x;
        hc.p.y = hl.p.y-dip2px(getcontext(), 25)*j;
        hl.hcs.add(hc);
      }
      mhacklines.add(hl);
      hl.num = mhacklines.size();
    }
  }

然后在ondraw方法中绘制:

@override
protected void ondraw(canvas canvas) {
  for (int i = 0; i < mhacklines.size(); i++) {
    drawtext(i, canvas);
  }
  mhandler.sendemptymessagedelayed(what, 100);//用于开启循环 线条滚动
  }

public void drawtext(int nindex,canvas canvas){
    hackline hackline = mhacklines.get(nindex);
    for (int i = 0; i < hackline.hcs.size(); i++) {
      hackcode hackcode = hackline.hcs.get(i);
      mpaint.setalpha(hackcode.alpha);
      canvas.drawtext(hackcode.code, hackcode.p.x, hackcode.p.y, mpaint);
    }
  }

接下来要滚动显示由handler发送一个延时100毫秒的消息开始:

class weakhandler extends handler{
    weakreference<activity> mactivity; 
    public weakhandler(activity activity){
      mactivity = new weakreference<activity>(activity);
    }
    @override
    public void handlemessage(message msg) {
      if(mactivity.get() != null){
        switch (msg.what) {
        case what:
          nextplay(dip2px(getcontext(), 20));
          for (int i = 0; i < mhacklines.size(); i++) {
            if(mhacklines.get(i).p.y >= mheight/2*3){
              addhackline(mhacklines.get(i));
            }
          }
          invalidate();
          break;
        }
      }
    }
  }

让整个线条往下走其实也就只用将线条的底部初始值y坐标不断增加,内部字母随之更新位置就可以了:

/**
   * 下一帧播放
   * @param nnum 每次下移多远 距离
   */
  public void nextplay(int nnum){
    for (int i = 0; i < mhacklines.size(); i++) {
      list<hackcode> hcs = mhacklines.get(i).hcs;
      hcs.clear();
      mhacklines.get(i).p.y+=nnum;
      for (int j = 0; j < 7; j++) {
        hackcode hc = new hackcode();
        hc.alpha -= 30*j;
        hc.code = codes[new random().nextint(codes.length)];
        hc.p.x = mhacklines.get(i).p.x;
        hc.p.y = mhacklines.get(i).p.y-dip2px(getcontext(), 25)*j;
        hcs.add(hc);
      }
    }
  }

之后我们要考虑在合适的时间移除掉不需要的字母线条并增加新的子母线条,这里我是判断如果线条底部超过屏幕高度的一半时就移除当前线条并根据唯一标示添加新的线条:

  /**
   * 删除一列 同时添加初始化一列
   * @param hackline 
   */
  public void addhackline(hackline hackline){
      if(hackline == null){
        return;
      }
      int num = hackline.num;
      mhacklines.remove(hackline);//如果存在 删除  重新添加

      hackline hl;
      hl= new hackline();
      hl.p.x = mwidth/9*(num-1);
      hl.p.y = mheight/12*(7-(num-1));
      for (int j = 0; j < 7; j++) {
        hackcode hc = new hackcode();
        hc.alpha -= 30*j;
        hc.code = codes[new random().nextint(codes.length)];
        hc.p.x = hl.p.x;
        hc.p.y = hl.p.y-dip2px(getcontext(), 25)*j;
        hl.hcs.add(hc);
      }
      hl.num = num;
      mhacklines.add(hl);
  }

最后,在控件移除屏幕的时候终止消息循环,运行时记得将根布局设置背景为黑色:

@override
  protected void ondetachedfromwindow() {
    super.ondetachedfromwindow();
    mhandler.removecallbacksandmessages(null);//停止刷新
  }

okok,字母雨已经出来啦~~ 思路清晰之后还是很简单的哦~

二、实现代码

整个代码也不算很长,就直接贴上了:

/**
 * 字母雨
 * @author zhang
 *
 */
public class hackview extends view {
  /** 文字的画笔 */
  private paint mpaint;
  /** 控件的宽 */
  private int mwidth;
  /** 控件的高 */
  private int mheight;
  /** 所有字母 */
  private static final string[] codes = {
    "a","b","c","d","e","f","g","h","i","j","k",
    "l","m","n","o","p","q","r","s","t","u","v",
    "w","k","y","z"
  };

  private static final int what = 1;
  /** 所有的hackline组合 */
  private list<hackline> mhacklines = new arraylist<hackview.hackline>();

  private weakhandler mhandler;

  public hackview(context context) {
    this(context,null);
  }
  public hackview(context context, attributeset attrs) {
    this(context, attrs,0);
  }
  public hackview(context context, attributeset attrs, int defstyleattr) {
    super(context, attrs, defstyleattr);
    init();
    mhandler = new weakhandler((activity) context);
  }

  class weakhandler extends handler{
    weakreference<activity> mactivity; 
    public weakhandler(activity activity){
      mactivity = new weakreference<activity>(activity);
    }
    @override
    public void handlemessage(message msg) {
      if(mactivity.get() != null){
        switch (msg.what) {
        case what:
          nextplay(dip2px(getcontext(), 20));
          for (int i = 0; i < mhacklines.size(); i++) {
            if(mhacklines.get(i).p.y >= mheight/2*3){
              addhackline(mhacklines.get(i));
            }
          }
          invalidate();
          break;
        }
      }
    }
  }
  private void init() {
    mpaint = new paint(paint.anti_alias_flag);
    mpaint.setcolor(color.white);
    mpaint.settextsize(dip2px(getcontext(), 20));
    mpaint.setstrokecap(cap.round);
    mpaint.setstrokewidth(dip2px(getcontext(), 5));
    setlayertype(view.layer_type_software, null);
  }
  @override
  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
    super.onmeasure(widthmeasurespec, heightmeasurespec);

    mwidth = getmeasuredwidth();//获取控件宽高
    mheight = getmeasuredheight();
    mhacklines.clear();//清空集合
    initplaydata();
  }
  /**
   * 下一帧播放
   * @param nnum 每次下移多远 距离
   */
  public void nextplay(int nnum){
    for (int i = 0; i < mhacklines.size(); i++) {
      list<hackcode> hcs = mhacklines.get(i).hcs;
      hcs.clear();
      mhacklines.get(i).p.y+=nnum;
      for (int j = 0; j < 7; j++) {
        hackcode hc = new hackcode();
        hc.alpha -= 30*j;
        hc.code = codes[new random().nextint(codes.length)];
        hc.p.x = mhacklines.get(i).p.x;
        hc.p.y = mhacklines.get(i).p.y-dip2px(getcontext(), 25)*j;
        hcs.add(hc);
      }
    }
  }
  /**
   * 删除一列 同时添加初始化一列
   * @param hackline 
   */
  public void addhackline(hackline hackline){
      if(hackline == null){
        return;
      }
      int num = hackline.num;
      mhacklines.remove(hackline);//如果存在 删除  重新添加

      hackline hl;
      hl= new hackline();
      hl.p.x = mwidth/9*(num-1);
      hl.p.y = mheight/12*(7-(num-1));
      for (int j = 0; j < 7; j++) {
        hackcode hc = new hackcode();
        hc.alpha -= 30*j;
        hc.code = codes[new random().nextint(codes.length)];
        hc.p.x = hl.p.x;
        hc.p.y = hl.p.y-dip2px(getcontext(), 25)*j;
        hl.hcs.add(hc);
      }
      hl.num = num;
      mhacklines.add(hl);
  }
  /**
   * 初始化每一行数据
   * @param x
   * @param y
   */
  public void inithackline(int x,int y){
    hackline hl;
    for (int i = 0; i < 9; i++) {
      hl= new hackline();
      hl.p.x = x*i;
      hl.p.y = y*(7-i);
      for (int j = 0; j < 7; j++) {
        hackcode hc = new hackcode();
        hc.alpha -= 30*j;
        hc.code = codes[new random().nextint(codes.length)];
        hc.p.x = hl.p.x;
        hc.p.y = hl.p.y-dip2px(getcontext(), 25)*j;
        hl.hcs.add(hc);
      }
      mhacklines.add(hl);
      hl.num = mhacklines.size();
    }
  }
  /**
   * 初始化播放数据
   */
  public void initplaydata(){
    inithackline(mwidth/9, mheight/12);
    inithackline(mwidth/9, mheight/7);
    hackline hl;
    for (int i = 3; i < 9; i++) {
      hl= new hackline();
      hl.p.x = mwidth/9*(i+1);
      hl.p.y = mheight/7*(9-i);
      for (int j = 0; j < 7; j++) {
        hackcode hc = new hackcode();
        hc.alpha -= 30*j;
        hc.code = codes[new random().nextint(codes.length)];
        hc.p.x = hl.p.x;
        hc.p.y = hl.p.y-dip2px(getcontext(), 25)*j;
        hl.hcs.add(hc);
      }
      mhacklines.add(hl);
      hl.num = mhacklines.size();
    }
  }
  @override
  protected void ondraw(canvas canvas) {
    for (int i = 0; i < mhacklines.size(); i++) {
      drawtext(i, canvas);
    }
    mhandler.sendemptymessagedelayed(what, 100);
  }

  public void drawtext(int nindex,canvas canvas){
    hackline hackline = mhacklines.get(nindex);
    for (int i = 0; i < hackline.hcs.size(); i++) {
      hackcode hackcode = hackline.hcs.get(i);
      mpaint.setalpha(hackcode.alpha);
      canvas.drawtext(hackcode.code, hackcode.p.x, hackcode.p.y, mpaint);
    }
  }
  /**
   * 每条线 包含多个字母
   **/
  class hackline{
    public int num = 0;//用于记录这列的标示
    private point p = new point();//线的初始位置
    list<hackcode> hcs = new arraylist<hackview.hackcode>();//黑客字母的一条线
  }
  /**
   * 每个字母
   */
  class hackcode{
     point p = new point();//每一个字母的坐标
     int alpha = 255;//透明度值 默认255
     string code = "a";//字母的值
  }
  @override
  protected void ondetachedfromwindow() {
    super.ondetachedfromwindow();
    mhandler.removecallbacksandmessages(null);//停止刷新
  }
   /** 
   * 根据手机的分辨率从 dip 的单位 转成为 px(像素) 
   */ 
  public static int dip2px(context context, float dpvalue) { 
    final float scale = context.getresources().getdisplaymetrics().density; 
    return (int) (dpvalue * scale + 0.5f); 
  } 
}

xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#000"
  tools:context=".mainactivity" >

  <com.zk.hack.hackview
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

</relativelayout>

以上就是基于android实现字母雨的效果全部内容,效果很好,有需要的小伙伴们可以参考学习。