首先来看效果:
一、实现原理
在实现过程中,主要考虑整个界面由若干个字母组成的子母线条组成,这样的话把固定数量的字母封装成一个字母线条,而每个字母又封装成一个对象,这样的话,就形成了如下组成效果:
字母对象--》字母线条对象--》界面效果
每个字母都应该知道自己的位置坐标,自己上面的字母、以及自己的透明度:
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
方法中绘制:
@OverrIDeprotected 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","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,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,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,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,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实现字母雨的效果全部内容,效果很好,有需要的小伙伴们可以参考学习。
总结以上是内存溢出为你收集整理的Android实现字母雨的效果全部内容,希望文章能够帮你解决Android实现字母雨的效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)