- package com.xuefeng.demo.widget.hkrainie;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Paint.Align;
- import android.graphics.Paint.Style;
- import android.os.Handler;
- import android.util.AttributeSet;
- import android.view.View;
- public class HKTextGroup extends View{
- private char[] count = {‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘G‘,‘H‘,‘J‘,‘K‘,‘L‘,‘M‘,‘N‘,‘O‘};
- private Paint paint;
- private float textsize = 40;
- public HKTextGroup(Context context, AttributeSet attrs) {
- super(context, attrs);
- init();
- }
- private Cell[][]cells;
- private void init(){
- paint = new Paint();
- paint.setAntiAlias(true);
- paint.setColor(Color.WHITE);
- paint.setTextSize(textsize);
- paint.setTextAlign(Align.LEFT);
- paint.setStyle(Style.FILL);
- cells = new Cell[row][line];
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < line; j++) {
- cells[i][j] = new Cell(i, j);
- cells[i][j].alpha = 0;
- cells[i][j].msg = ""+count[(int) (Math.random()*count.length)];
- }
- }
- }
- //行
- private int line = 20;
- //列
- private int row = 20;
- private int seed = 0;
- private int stepCount = 11;
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- textsize = getWidth() /10;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < line; j++) {
- Cell cell = cells[i][j];
- //根据透明度设置颜色
- if (cell.alpha == 255) {
- paint.setColor(Color.WHITE);
- }else {
- paint.setColor(Color.GREEN);
- }
- //设置透明度
- paint.setAlpha(cell.alpha);
- if (cell.alpha != 0) {
- canvas.drawText(cell.msg, cell.l*20,(float)(cell.r*textsize*0.8+textsize), paint);
- }
- }
- }
- handler.sendEmptyMessageDelayed(seed, 10);
- }
- private Handler handler = new Handler(){
- public void handleMessage(android.os.Message msg) {
- for (int i = 0; i < row; i++) {
- for (int j = line-1; j >=0; j--) {
- //1.如果第一行透明度为0,则有几率变为255;
- //2、如果中间行透明度为0,不做处理
- //3、中间行不为0,依次减少一个梯度
- //4、我上面的一个是255,那么我也是255,而他亮度减1
- Cell cell = cells[i][j];
- if (j == 0) {
- if (cell.alpha == 0) {
- if (Math.random()*10>9) {
- cell.alpha = 255;
- }
- }else {
- cell.alpha = cell.alpha - 25>0?cell.alpha-25:0;
- }
- }else if (j>0 && j<=line -1) {
- if (cells[i][j-1].alpha == 255) {
- cell.alpha = 255;
- }else {
- cell.alpha = cell.alpha-25>0?cell.alpha-25:0;
- }
- }
- }
- }
- //刷新,重新绘制
- invalidate();
- };
- };
- class Cell{
- public Cell(int l,int r){
- this.l = l;
- this.r = r;
- }
- //行
- public int l;
- //列
- public int r;
- //内容
- public String msg;
- //zhongzi
- public int seed;
- //透明度
- public int alpha;
- }
- }
时间: 2024-11-03 05:26:58