• 自定义控件:瀑布流水字母


     1 <RelativeLayout 
     2     xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:background="@android:color/background_light"
     7     tools:context=".MainActivity" >
     8 
     9     <TextView
    10         android:layout_width="wrap_content"
    11         android:layout_height="wrap_content"
    12         android:layout_centerHorizontal="true"
    13         android:text="@string/hello_world"
    14         android:textColor="@android:color/white" />
    15 
    16     <com.example.empire.HkTextGroup
    17         android:id="@+id/hk_text"
    18         android:layout_width="fill_parent"
    19         android:layout_height="fill_parent"
    20         android:layout_marginTop="20dp"
    21         android:background="@android:color/background_dark" />
    22 
    23 </RelativeLayout>
      1 package com.example.empire;
      2 
      3 import android.content.Context;
      4 import android.graphics.Canvas;
      5 import android.graphics.Color;
      6 import android.graphics.Paint;
      7 import android.graphics.Paint.Align;
      8 import android.graphics.Paint.Style;
      9 import android.os.Handler;
     10 import android.util.AttributeSet;
     11 import android.view.View;
     12 
     13 public class HkTextGroup extends View {
     14 
     15     private char[] counts = new char[] { 
     16             'A', 'B', 'C', 'D', 'E', 'F', 'G',
     17             'H', 'J', 'K', 'L', 'M', 'N', 'O' };
     18 
     19     private Paint paint;
     20     private Context ctx;
     21     private Cell[][] cells;
     22     // 像素值
     23     private int textSize = 20;
     24 
     25     public HkTextGroup(Context context, AttributeSet attrs) {
     26         super(context, attrs);
     27         ctx = context;
     28         init();
     29     }
     30 
     31     private void init() {
     32         paint = new Paint();
     33         paint.setAntiAlias(true);
     34         paint.setColor(Color.WHITE);
     35         paint.setTextSize(textSize);// PX值
     36         paint.setTextAlign(Align.LEFT);
     37         paint.setStyle(Style.FILL);
     38 
     39         cells = new Cell[list][row];
     40         for (int j = 0; j < list; j++) {
     41             for (int i = 0; i < row; i++) {
     42                 cells[j][i] = new Cell(i, j);
     43                 cells[j][i].alpha = 0;
     44                 cells[j][i].msg = ""
     45                         + counts[(int) (Math.random() * counts.length)];
     46             }
     47         }
     48     }
     49 
     50     @Override
     51     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     52         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     53         Utils.logleo("getWidth()::" + getWidth());
     54         Utils.logleo("getHeight()::" + getHeight());
     55 
     56         textSize = getWidth() / 10;
     57         left_bottom = getHeight();
     58     }
     59 
     60     public float left;
     61     public float left_bottom;
     62 
     63     private Handler handler = new Handler() {
     64         public void handleMessage(android.os.Message msg) {
     65             int flag = msg.what;// 0 -- 10
     66 
     67             for (int j = 0; j < list; j++) {
     68 
     69                 for (int i = row - 1; i >= 0; i--) {
     70                     // 1、如果第一行透明度为0,则有一定机率变为255
     71                     // 2、如果中间行透明度为0,不做处理
     72                     // 3、中间行不为0,依次减少一个梯度
     73                     // 4、我上面的一个是255,那么我也是255,而他亮度减1
     74                     Cell cell = cells[j][i];
     75 
     76                     if (i == 0) {
     77                         if (cell.alpha == 0) {
     78                             if (Math.random() * 10 > 9) {
     79                                 cell.alpha = 255;
     80                             }
     81                         } else {
     82                             cell.alpha = cell.alpha - 25 > 0 ? cell.alpha - 25
     83                                     : 0;
     84                         }
     85                     } else if (i > 0 && i <= row - 1) {
     86                         if (cells[j][i - 1].alpha == 255) {
     87                             cell.alpha = 255;
     88                         } else {
     89                             cell.alpha = cell.alpha - 25 > 0 ? cell.alpha - 25
     90                                     : 0;
     91                         }
     92                     }
     93                 }
     94             }
     95             invalidate();
     96 
     97         };
     98     };
     99 
    100     private int seed = 0;
    101     private int stepCount = 11;
    102     /***/
    103     private int row = 20;
    104     /***/
    105     private int list = 20;
    106 
    107     @Override
    108     protected void onDraw(Canvas canvas) {
    109 
    110         for (int j = 0; j < list; j++) {
    111             for (int i = 0; i < row; i++) {
    112                 Cell cell = cells[j][i];
    113                 // 小机率事件,改变内容
    114                 if (Math.random() * 100 > 85) {
    115                     cell.msg = ""
    116                             + counts[(int) (Math.random() * counts.length)];
    117                 }
    118                 // 根据透明度确定颜色
    119                 if (cell.alpha == 255) {
    120                     paint.setColor(Color.WHITE);
    121                 } else {
    122                     paint.setColor(Color.GREEN);
    123                 }
    124                 // 设置透明度
    125                 paint.setAlpha(cell.alpha);
    126 
    127                 // 绘制
    128                 if (cell.alpha != 0) {
    129                     canvas.drawText(cell.msg, cell.j * 15, (float) (cell.i
    130                             * textSize * 0.6 + textSize), paint);
    131                 }
    132             }
    133         }
    134 
    135         // seed = (seed + 1) % stepCount;
    136         handler.sendEmptyMessageDelayed(seed, 10);
    137     }
    138 
    139     private class Cell {
    140         public Cell(int i, int j) {
    141             super();
    142             this.i = i;
    143             this.j = j;
    144         }
    145 
    146         /***/
    147         public int i;
    148         /***/
    149         public int j;
    150         /** 种子 */
    151         public int seed;
    152         /** 透明度 */
    153         public int alpha;
    154         public String msg;
    155 
    156     }
    157 
    158 }

      1 package com.example.empire;
      2 
      3 import android.content.Context;
      4 import android.graphics.Canvas;
      5 import android.graphics.Color;
      6 import android.graphics.Paint;
      7 import android.graphics.Paint.Align;
      8 import android.graphics.Paint.Style;
      9 import android.os.Handler;
     10 import android.util.AttributeSet;
     11 import android.view.View;
     12 
     13 public class HkText extends View {
     14 
     15     private char[] counts = new char[] { 
     16             'A', 'B', 'C', 'D', 'E', 'F', 'G',
     17             'H', 'J', 'K', 'L', 'M', 'N', 'O' };
     18 
     19     private Paint paint;
     20     private Context ctx;
     21 
     22     public HkText(Context context, AttributeSet attrs) {
     23         super(context, attrs);
     24         ctx = context;
     25         init();
     26     }
     27 
     28     /**
     29      * 像素值
     30      */
     31     private int textSize = 20;
     32 
     33     private void init() {
     34         paint = new Paint();
     35         paint.setAntiAlias(true);
     36         paint.setColor(Color.WHITE);
     37         paint.setTextSize(textSize);// PX值
     38         paint.setTextAlign(Align.LEFT);
     40         paint.setStyle(Style.FILL);
     41     }
     42 
     43     // private float sp2px(int sp){
     44     // TypedValue.applyDimension(
     45     // TypedValue.COMPLEX_UNIT_SP, sp, ctx.getResources().getDisplayMetrics());
     46     //
     47     // float px = sp * ctx.getResources().getDisplayMetrics().scaledDensity;
     48     // return px;
     49     // }
     50     //
     51     // private float px2sp(int px){
     52     // float sp = px / ctx.getResources().getDisplayMetrics().scaledDensity;
     53     // return sp;
     54     // }
     55 
     56     @Override
     57  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     58         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     59         Utils.logleo("getWidth()::" + getWidth());
     60         Utils.logleo("getHeight()::" + getHeight());
     61 
     62         textSize = getWidth() / 10;
     63     }
     64 
     65     public float left = 150;
     66     public float left_bottom = 200;
     67 
     68     private Handler handler = new Handler() {
     69         public void handleMessage(android.os.Message msg) {
     70             // int flag = msg.what;// 0 -- 10
     72             // paint.setColor(Color.BLUE);
     73             invalidate();
     74         };
     75     };
     76 
     77     private int seed = 0;
     79     private int stepCount = 11;
     80 
     81     @Override
     82     protected void onDraw(Canvas canvas) {
     84         left = 10;
     85         left_bottom = 400;
     86         for (int i = 0; i < 20; i++) {
     87 
     88             int seed_tem = seed;
     89 
     90             int alpha = 255 - (i + seed_tem) * 25;
     91             paint.setAlpha(alpha);// 0是没有
     92 
     93             canvas.drawText(counts, i % counts.length, 1, left, 
    left_bottom,
    paint); 96 left_bottom = (float) (left_bottom - textSize * 0.6); 98 } 99 seed = (seed + 1) % stepCount; 100 handler.sendEmptyMessageDelayed(seed, 500); 101 } 102 103 }
     1 package com.example.empire;
     3 import android.util.Log;
     4 
     5 public class Utils {
     7     public static boolean isShow = true;
     9     public static void logleo(String msg) {
    10         if (isShow) {
    11             Log.i("leo", msg);
    12         }
    13     }
    14 }

    DEMO下载地址:http://pan.baidu.com/s/1mgkfQh6

  • 相关阅读:
    使用PowerDesigner创建表并导入到数据库
    第二次作业——结对项目之需求分析与原型模型设计
    使用Git进行代码管理
    常用
    头文件
    只出现一次的数
    链表实现基础排序算法
    判断链表有公共点
    单链表判环
    二叉树非递归遍历
  • 原文地址:https://www.cnblogs.com/androidsj/p/3995583.html
Copyright © 2020-2023  润新知