欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

Android

當(dāng)前位置:主頁 > 軟件編程 > Android >

Android自定義View實現(xiàn)彈幕效果

來源:本站原創(chuàng)|時間:2020-01-10|欄目:Android|點擊: 次

在很多視頻直播中都有彈幕功能,而安卓上沒有簡單好用的彈幕控件,本文介紹一個自定義彈幕view的demo。

效果圖:

思路:

1、自定義Textitem類表示彈幕的信息
2、自定義view繼承view,使用ArrayList保存每條Textitem
3、隨機生成坐標(biāo)點繪制每條TextItem,不斷變換Text的橫坐標(biāo)實現(xiàn)彈幕的滾動

首先創(chuàng)建彈幕類,彈幕包括坐標(biāo),顏色,滾動速度,以及文字內(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;
 }
}

接下來自定義View,彈幕橫坐標(biāo)不斷變換,需要實現(xiàn)定時刷新界面,重新繪制text。所以實現(xiàn)了Runable接口,在構(gòu)造方法中開啟線程,不斷循環(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中獲取彈幕進行繪制,由于在其他線程進行刷新,所以使用postInvalidate進行重繪。

由于只是實現(xiàn)demo,很多問題沒有考慮,存在問題:

彈幕離開屏幕后沒有進行清除,使得ArrayList不斷擴大,可以進行一個判斷,若Textitem的繪制區(qū)域不在屏幕內(nèi)則刪掉此item
彈幕若沒有交互需求,可以使用Surfaceview進行繪制,SurfaceView可以在子線程更新UI,多緩存機制也可以避免畫面跳動
另外注意下自定義View的構(gòu)造函數(shù)的調(diào)用時機:

public View(Context context)是在java代碼創(chuàng)建視圖直接通過new方法創(chuàng)建的時候被調(diào)用,
public View(Context context, Attributeset attrs)是在xml創(chuàng)建但是沒有指定style的時候被調(diào)用
public View(Context Context,AttributeSet attrs, int defStyle)給View提供一個基本的style,沒有對View設(shè)置屬性就使用style中的屬性

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:Android使用SoundPool實現(xiàn) 音效

欄    目:Android

下一篇:Android碎片fragment實現(xiàn)靜態(tài)加載的實例代碼

本文標(biāo)題:Android自定義View實現(xiàn)彈幕效果

本文地址:http://mengdiqiu.com.cn/a1/Android/9037.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有