免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

Android中怎么通過自定義View實現彈幕效果

Android中怎么通過自定義View實現彈幕效果,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

10年積累的網站設計、做網站經驗,可以快速應對客戶對網站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網絡服務。我雖然不認識你,你也不認識我。但先網站設計后付款的網站建設流程,更有鄂倫春免費網站建設讓你可以放心的選擇與我們合作。

1、自定義Textitem類表示彈幕的信息2、自定義view繼承view,使用ArrayList保存每條Textitem3、隨機生成坐標點繪制每條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<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中的屬性

看完上述內容,你們掌握Android中怎么通過自定義View實現彈幕效果的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創新互聯行業資訊頻道,感謝各位的閱讀!

當前題目:Android中怎么通過自定義View實現彈幕效果
文章來源:http://m.newbst.com/article32/jechpc.html

成都網站建設公司_創新互聯,為您提供手機網站建設網站內鏈、網站收錄、微信小程序、服務器托管響應式網站

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

h5響應式網站建設