Android自定义View实现弹幕效果 |
您所在的位置:网站首页 › 植物大战僵尸2美服怎么调中文 › Android自定义View实现弹幕效果 |
在很多视频直播中都有弹幕功能,而安卓上没有简单好用的弹幕控件,本文介绍一个自定义弹幕view的demo。 效果图: ![]() 思路: 1、自定义Textitem类表示弹幕的信息 2、自定义view继承view,使用ArrayList保存每条Textitem 3、随机生成坐标点绘制每条TextItem,不断变换Text的横坐标实现弹幕的滚动 首先创建弹幕类,弹幕包括坐标,颜色,滚动速度,以及文字内容: public class Textitem { private String content; private float fx; private float fy; private float perstep; private int textcolor; public Textitem(String content,float fx,float fy,float perstep,int textcolor){ this.content = content; this.fx = fx; this.fy = fy; this.perstep = perstep; this.textcolor = textcolor; } public String getContent(){ return content; } public void setContent(String content){ this.content = content; } public int getTextcolor(){ return textcolor; } public void setTextcolor(int textcolor){ this.textcolor = textcolor; } public float getFx(){ return fx; } public void setFx(float fx){ this.fx = fx; } public float getFy(){ return fy; } public void setFy(float fy){ this.fy = fy; } public float getPerstep(){ return perstep; } public void setPerstep(){ fx -= perstep; } }接下来自定义View,弹幕横坐标不断变换,需要实现定时刷新界面,重新绘制text。所以实现了Runable接口,在构造方法中开启线程,不断循环,每600毫秒刷新界面: public class barrageview extends View implements Runnable{ private List |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |