仿黑客帝国文字雨效果

    1. package com.xuefeng.demo.widget.hkrainie;

    2. import android.content.Context;
    3. import android.graphics.Canvas;
    4. import android.graphics.Color;
    5. import android.graphics.Paint;
    6. import android.graphics.Paint.Align;
    7. import android.graphics.Paint.Style;
    8. import android.os.Handler;
    9. import android.util.AttributeSet;
    10. import android.view.View;

    11. public class HKTextGroup extends View{
    12.         private char[] count = {'A','B','C','D','E','F','G','H','J','K','L','M','N','O'};
    13.        
    14.         private Paint paint;
    15.         private float textsize = 40;
    16.         public HKTextGroup(Context context, AttributeSet attrs) {
    17.                 super(context, attrs);
    18.                 init();
    19.         }
    20.         private Cell[][]cells;
    21.         private void init(){
    22.                 paint = new Paint();
    23.                 paint.setAntiAlias(true);
    24.                 paint.setColor(Color.WHITE);
    25.                 paint.setTextSize(textsize);
    26.                 paint.setTextAlign(Align.LEFT);
    27.                 paint.setStyle(Style.FILL);
    28.                 cells = new Cell[row][line];
    29.                 for (int i = 0; i < row; i++) {
    30.                         for (int j = 0; j < line; j++) {
    31.                                 cells[i][j] = new Cell(i, j);
    32.                                 cells[i][j].alpha = 0;
    33.                                 cells[i][j].msg = ""+count[(int) (Math.random()*count.length)];
    34.                                
    35.                         }
    36.                 }
    37.                
    38.         }
    39.         //行
    40.         private int line = 20;
    41.         //列
    42.         private int row = 20;
    43.        
    44.         private int seed = 0;
    45.         private int stepCount = 11;
    46.        
    47.         @Override
    48.         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    49.                 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    50.                 textsize = getWidth() /10;
    51.         }
    52.        
    53.         @Override
    54.         protected void onDraw(Canvas canvas) {
    55.                 super.onDraw(canvas);
    56.                 for (int i = 0; i < row; i++) {
    57.                         for (int j = 0; j < line; j++) {
    58.                                 Cell cell = cells[i][j];
    59.                                 //根据透明度设置颜色
    60.                                 if (cell.alpha == 255) {
    61.                                         paint.setColor(Color.WHITE);
    62.                                 }else {
    63.                                         paint.setColor(Color.GREEN);
    64.                                 }
    65.                                 //设置透明度
    66.                                 paint.setAlpha(cell.alpha);
    67.                                 if (cell.alpha != 0) {
    68.                                        
    69.                                         canvas.drawText(cell.msg, cell.l*20,(float)(cell.r*textsize*0.8+textsize), paint);
    70.                                 }
    71.                         }
    72.                 }
    73.                 handler.sendEmptyMessageDelayed(seed, 10);
    74.                
    75.         }
    76.        
    77.         private Handler handler = new Handler(){
    78.                 public void handleMessage(android.os.Message msg) {
    79.                         for (int i = 0; i < row; i++) {
    80.                                 for (int j = line-1; j >=0; j--) {
    81.                                         //1.如果第一行透明度为0,则有几率变为255;
    82.                                         //2、如果中间行透明度为0,不做处理
    83.                                         //3、中间行不为0,依次减少一个梯度
    84.                                         //4、我上面的一个是255,那么我也是255,而他亮度减1
    85.                                         Cell cell = cells[i][j];
    86.                                         if (j == 0) {
    87.                                                 if (cell.alpha == 0) {
    88.                                                         if (Math.random()*10>9) {
    89.                                                                 cell.alpha = 255;
    90.                                                         }
    91.                                                 }else {
    92.                                                         cell.alpha = cell.alpha - 25>0?cell.alpha-25:0;
    93.                                                 }
    94.                                         }else if (j>0 && j<=line -1) {
    95.                                                 if (cells[i][j-1].alpha == 255) {
    96.                                                         cell.alpha = 255;
    97.                                                 }else {
    98.                                                         cell.alpha = cell.alpha-25>0?cell.alpha-25:0;
    99.                                                 }
    100.                                         }
    101.                                 }
    102.                         }
    103.                        
    104.                         //刷新,重新绘制
    105.                         invalidate();
    106.                        
    107.                 };
    108.         };
    109.        
    110.         class Cell{
    111.                 public Cell(int l,int r){
    112.                         this.l = l;
    113.                         this.r = r;
    114.                 }
    115.                 //行
    116.                 public int l;
    117.                 //列
    118.                 public int r;
    119.                 //内容
    120.                 public String msg;
    121.                 //zhongzi
    122.                 public int seed;
    123.                 //透明度
    124.                 public int alpha;
    125.         }

    126. }
原文地址:https://www.cnblogs.com/devilthrone/p/4234259.html