Android自定義View實(shí)現(xiàn)彈幕效果
在很多視頻直播中都有彈幕功能,而安卓上沒(méi)有簡(jiǎn)單好用的彈幕控件,本文介紹一個(gè)自定義彈幕view的demo。
效果圖:
思路:
1、自定義Textitem類表示彈幕的信息
2、自定義view繼承view,使用ArrayList保存每條Textitem
3、隨機(jī)生成坐標(biāo)點(diǎn)繪制每條TextItem,不斷變換Text的橫坐標(biāo)實(shí)現(xiàn)彈幕的滾動(dòng)
首先創(chuàng)建彈幕類,彈幕包括坐標(biāo),顏色,滾動(dòng)速度,以及文字內(nèi)容:
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;
}
}
接下來(lái)自定義View,彈幕橫坐標(biāo)不斷變換,需要實(shí)現(xiàn)定時(shí)刷新界面,重新繪制text。所以實(shí)現(xiàn)了Runable接口,在構(gòu)造方法中開(kāi)啟線程,不斷循環(huán),每600毫秒刷新界面:
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中獲取彈幕進(jìn)行繪制,由于在其他線程進(jìn)行刷新,所以使用postInvalidate進(jìn)行重繪。
由于只是實(shí)現(xiàn)demo,很多問(wèn)題沒(méi)有考慮,存在問(wèn)題:
彈幕離開(kāi)屏幕后沒(méi)有進(jìn)行清除,使得ArrayList不斷擴(kuò)大,可以進(jìn)行一個(gè)判斷,若Textitem的繪制區(qū)域不在屏幕內(nèi)則刪掉此item
彈幕若沒(méi)有交互需求,可以使用Surfaceview進(jìn)行繪制,SurfaceView可以在子線程更新UI,多緩存機(jī)制也可以避免畫(huà)面跳動(dòng)
另外注意下自定義View的構(gòu)造函數(shù)的調(diào)用時(shí)機(jī):
public View(Context context)是在java代碼創(chuàng)建視圖直接通過(guò)new方法創(chuàng)建的時(shí)候被調(diào)用,
public View(Context context, Attributeset attrs)是在xml創(chuàng)建但是沒(méi)有指定style的時(shí)候被調(diào)用
public View(Context Context,AttributeSet attrs, int defStyle)給View提供一個(gè)基本的style,沒(méi)有對(duì)View設(shè)置屬性就使用style中的屬性
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Android使用SoundPool實(shí)現(xiàn)播放音效
欄 目:Android
下一篇:Android碎片fragment實(shí)現(xiàn)靜態(tài)加載的實(shí)例代碼
本文標(biāo)題:Android自定義View實(shí)現(xiàn)彈幕效果
本文地址:http://www.jygsgssxh.com/a1/Android/9037.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開(kāi)發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子


