关键时刻,第一时间送达!
[原文来自:www.ii77.com]
[原创文章:www.ii77.com]
来源:Android开发中文站
https://mp.weixin.qq.com/s/l_UNrpHWLQrbBxPdJeaXUw
Android编程精整理发布,转载请联系作者获得授权
在很多视频直播中都有弹幕功能,而安卓上没有简单好用的弹幕控件,本文介绍一个自定义弹幕view的demo。
效果图:
自定义Textitem类表示弹幕的信息
自定义view继承view,使用ArrayList保存每条Textitem
随机生成坐标点绘制每条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;
}
}
public class barrageview extends View implements Runnable{
private List<Textitem> items = new ArrayList<>();
Random random = new Random();
private Paint paint;
public barrageview(Context context) {
super(context);
initpaint();
new Thread(this).start();
}
public barrageview(Context context, AttributeSet attrs) {
super(context, attrs);
initpaint();
new Thread(this).start();
}
public barrageview(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initpaint();
new Thread(this).start();
}
public void addTextitem(String content){
float x = random.nextFloat()*getWidth();
float y = Math.abs(random.nextFloat()*(getHeight()-50))+40;
float step = random.nextFloat()*50;
int r = random.nextInt(255);
int g = random.nextInt(255);
int b = random.nextInt(255);
Textitem item = new Textitem(content,x,y,step, Color.rgb(r,g,b));
items.add(item);
}
public void initpaint(){
paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
paint.setTextSize(30);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
for(Textitem item:items){
paint.setColor(item.getTextcolor());
canvas.drawText(item.getContent(),item.getFx(),item.getFy(),paint);
}
}
@Override
public void run() {
while(true){
try{
Thread.sleep(600);
for(Textitem item:items){
item.setPerstep();
}
postInvalidate();
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
弹幕VIew就是不断从ArrayList中获取弹幕进行绘制,由于在其他线程进行刷新,所以使用postInvalidate进行重绘。
由于只是实现demo,很多问题没有考虑,存在问题:
弹幕离开屏幕后没有进行清除,使得ArrayList不断扩大,可以进行一个判断,若Textitem的绘制区域不在屏幕内则删掉此item
弹幕若没有交互需求,可以使用Surfaceview进行绘制,SurfaceView可以在子线程更新UI,多缓存机制也可以避免画面跳动
另外注意下自定义View的构造函数的调用时机:
public View(Context context)是在java代码创建视图直接通过new方法创建的时候被调用,
public View(Context context, Attributeset attrs)是在xml创建但是没有指定style的时候被调用
public View(Context Context,AttributeSet attrs, int defStyle)给View提供一个基本的style,没有对View设置属性就使用style中的属性
关注我☝有型有款 如今短裤已然是最时髦的单品之一。 今天就给大家 全方位解剖短裤的穿搭问题。 1 要了解自己的腿型适合哪种款式 # 上身强壮、腿粗# 比起裤腿窄的短裤,裤腿
七夕节为太太选表攻略~
当 我们还在追随国际大牌的时候,其实很多知名的外国明星早就已经开始穿中国的设计师品牌了。 明星示范 复古时髦的代言人Bella前不久用李宁的Photo Tee搭配休闲风的小西装,再背一
高跟鞋再一次如愿流浪于巴塞罗那这一片被上帝亲吻过的土壤!穿街走巷,苦寻历史遗留下的踪迹;也在各处唤醒自己沉寂已久的艺术细胞~今天这一篇,就让我们带着你感悟「神秘
真正的偶像是什么样子? 请来认识这两位活在青春期的70后。 Yuesai Kan 靳羽西 1946年出生于广西桂林,美籍华人,1980年代以电视片《世界各地》的主持人身份为人所知,成为中国家喻
七夕就快要到啦,对于这个即将到来的大型虐狗的节日, 小搭还是要为有男票的小仙女们奉上今天的情侣穿搭,很值得借鉴的哦,有对象的宝宝赶紧学起来, 出门约会去吧。 要说到
法国总统马克龙年仅39岁就成为了总统,参加各种活动都是英姿飒爽,很受各界媒体和广大网友们的关注。不过我们看到的马克龙,往往都是出席正式场合,他的着装也和其他总统一样
男朋友,从来都是别人家的不会让人失望,自己家的这位:食之无味,弃之可惜!情商大概丢在马里亚纳海沟,和他说话患脑梗几率达99%...哄你?别想了,甜言蜜语,不存在的,这时
转眼立秋都过了。 这意味着……今年还没去海边下过水的姑娘们,你们再不下水,就真不剩几个高温天能让你们去戏水了。 眼看着黄金周大概是今年能下水的最后假期,更要挖空心思
是的,今天我们又要来说说风口浪尖上的火箭少女,虽然成团已经有一段日子了,但是因为团员的合约问题导致火箭一直无法起飞,明明当初一腔热血的搞土创,如今中国的第一女团
本文内容来自网友供稿,如有信息侵犯了您的权益,请联系反馈核实
Copyright 2024.爱妻自媒体,让大家了解更多图文资讯!