Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

Android开发-自定义View-AndroidStudio(十九)HackView

$
0
0
转载请注明出处:http://blog.csdn.net/iwanghang/article/details/53994833
觉得博文有用,请点赞,请评论,请关注,谢谢!~


老规矩,先上GIF动态图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:
在list为20以内看出来BUG,实际上计算算法有问题,先备注一下,以后改善吧。BUG就是第二张GIF可以看出来右边距比较大。

private int list = 20; // 列

private int list = 100; // 列


MainActivity.java:
package com.iwanghang.hackview;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
HkTextGroup.java:
package com.iwanghang.hackview;

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.os.Message;
import android.util.AttributeSet;
import android.view.View;

public class HkTextGroup extends View {

//	private char[] counts = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G',
//			'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U'
//			, 'V', 'W', 'X', 'Y', 'Z'};

	private char[] counts = new char[] { '0', '1'};
	private int row; // 行
	private int list = 100; // 列
	private Paint paint;
	private Word[][] words;
	private int textSize;

	public HkTextGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	private void init() {
		paint = new Paint();
		paint.setAntiAlias(true); // 抗锯齿
		paint.setTextSize(textSize);// PX值
		System.out.println("init textSize = " + textSize);

		words = new Word[list][row];
		for (int j = 0; j < list; j++) {
			for (int i = 0; i < row; i++) {
				words[j][i] = new Word(i, j);
				words[j][i].alpha = 0;
				words[j][i].msg = ""
						+ counts[(int) (Math.random() * counts.length)];
			}
		}
	}

	// 1、先测量控件的宽高
	// 从而计算字体大小
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		System.out.println("onMeasure in" );
		System.out.println("onMeasure getWidth() = " + getWidth());
		System.out.println("onMeasure getHeight() = " + getHeight());
		textSize = getWidth() / list; // 控件宽度 / 列数 = 字体大小
		System.out.println("onMeasure textSize = " + textSize);
		if (textSize != 0){
			row = getHeight()/ textSize;
			System.out.println("onMeasure row = " + row);
		}
		System.out.println("onMeasure out" );
		init();
	}


	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			for (int j = 0; j < list; j++) {
				for (int i = row - 1; i >= 0; i--) {
					// 1、如果第一行透明度为0,则有一定机率变为255
					// 2、如果中间行透明度为0,不做处理
					// 3、中间行不为0,依次减少一个梯度
					// 4、我上面的一个是255,那么我也是255,而他亮度减1
					Word word = words[j][i];
					if (i == 0) {
						if (word.alpha == 0) {
							if (Math.random() * 10 > 9) {
								word.alpha = 255;
							}
						} else {
							word.alpha = word.alpha - 25 > 0 ? word.alpha - 25
									: 0;
						}
					} else if (i > 0 && i <= row - 1) {
						if (words[j][i - 1].alpha == 255) {
							word.alpha = 255;
						} else {
							word.alpha = word.alpha - 25 > 0 ? word.alpha - 25
									: 0;
						}
					}
				}
			}
			invalidate(); // onDraw();
		}
	};

	@Override
	protected void onDraw(Canvas canvas) {

		for (int j = 0; j < list; j++) {
			for (int i = 0; i < row; i++) {
				Word word = words[j][i];
				// 小机率事件,改变内容
				if (Math.random() * 100 > 85) {
					word.msg = ""
							+ counts[(int) (Math.random() * counts.length)];
				}
				// 根据透明度确定颜色
				if (word.alpha == 255) {
					paint.setColor(Color.WHITE);
				} else {
					paint.setColor(Color.GREEN);
				}
				// 设置透明度
				paint.setAlpha(word.alpha);

				// 绘制
				if (word.alpha != 0) {
					/**
					 * text:要绘制的文字
					 * x:绘制原点x坐标
					 * y:绘制原点y坐标
					 * paint:用来做画的画笔
					 * public void drawText(String text, float x, float y, Paint paint)
					 */
					canvas.drawText(word.msg, (float)(word.j * textSize  + textSize * 0.25), (float) (word.i
							* textSize + textSize), paint);
				}
			}
		}
		handler.sendEmptyMessageDelayed(0, 200); // 每次重绘后,等待200毫秒,给handler发消息
	}

	private class Word {
		public Word(int i, int j) {
			super();
			this.i = i;
			this.j = j;
		}

		/** 行 */
		public int i;
		/** 列 */
		public int j;
		/** 透明度 */
		public int alpha;
		public String msg;

	}

}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.iwanghang.hackview.MainActivity">

    <com.iwanghang.hackview.HkTextGroup
        android:id="@+id/hk_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_dark" />

</RelativeLayout>





转载请注明出处:http://blog.csdn.net/iwanghang/article/details/53994833



欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式

微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com



觉得博文有用,请点赞,请评论,请关注,谢谢!~

作者:iwanghang 发表于2017/1/3 15:31:29 原文链接
阅读:261 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles