欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

在很多視頻直播中都有彈幕功能,而安卓上沒(méi)有簡(jiǎn)單好用的彈幕控件,本文介紹一個(gè)自定義彈幕view的demo。

10年積累的網(wǎng)站制作、網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有紅河哈尼免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

效果圖:

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

思路:

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

首先創(chuàng)建彈幕類(lèi),彈幕包括坐標(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)啟線(xiàn)程,不斷循環(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)行繪制,由于在其他線(xià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可以在子線(xiàn)程更新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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前文章:Android自定義View實(shí)現(xiàn)彈幕效果
新聞來(lái)源:http://chinadenli.net/article48/jeishp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站電子商務(wù)、ChatGPT、關(guān)鍵詞優(yōu)化、軟件開(kāi)發(fā)、全網(wǎng)營(yíng)銷(xiāo)推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)
性欧美唯美尤物另类视频 | 天海翼精品久久中文字幕| 日韩美成人免费在线视频| 亚洲精品一二三区不卡| 免费性欧美重口味黄色| 日韩国产亚洲欧美另类| 精品午夜福利无人区乱码| 国产麻豆精品福利在线| 久久亚洲成熟女人毛片| 一本色道久久综合狠狠躁| 91久久精品中文内射| 麻豆国产精品一区二区| 97人妻精品免费一区二区| 91福利免费一区二区三区| 亚洲夫妻性生活免费视频| 久久国产青偷人人妻潘金莲| 国产亚洲系列91精品| 亚洲欧洲一区二区综合精品| 久久大香蕉精品在线观看| 国产精品激情在线观看| 99热九九热这里只有精品| 日韩精品综合免费视频| 99久久免费看国产精品| 国产免费自拍黄片免费看| 久久热九九这里只有精品| 国产丝袜美女诱惑一区二区| 出差被公高潮久久中文字幕| 国产精品福利一二三区| 99热九九热这里只有精品| 国产又粗又深又猛又爽又黄| 中文字幕亚洲精品乱码加勒比| 国产日韩欧美在线亚洲| 午夜精品在线观看视频午夜| 精品日韩视频在线观看| 亚洲一区二区三区一区| 91欧美亚洲精品在线观看| 五月激情综合在线视频| 亚洲品质一区二区三区| 国产亚洲欧美自拍中文自拍| 亚洲精品中文字幕欧美| 中文字幕欧美视频二区|