亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android跑馬燈MarqueeView源碼解析

 更新時(shí)間:2020年07月29日 13:29:19   投稿:lijiao  
這篇文章主要為大家詳細(xì)介紹了Android跑馬燈MarqueeView源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

跑馬燈效果,大家可以去原作者頁(yè)面瀏覽

下面看自定義控件的代碼

public class MarqueeView extends ViewFlipper {

 private Context mContext;
 private List<String> notices;
 private boolean isSetAnimDuration = false;
 private OnItemClickListener onItemClickListener;

 private int interval = 2000;
 private int animDuration = 500;
 private int textSize = 14;
 private int textColor = 0xffffffff;

 private int gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
 private static final int TEXT_GRAVITY_LEFT = 0, TEXT_GRAVITY_CENTER = 1, TEXT_GRAVITY_RIGHT = 2;

 public MarqueeView(Context context, AttributeSet attrs) {
  super(context, attrs);
  init(context, attrs, 0);
 }

 private void init(Context context, AttributeSet attrs, int defStyleAttr) {
  this.mContext = context;
  if (notices == null) {
   notices = new ArrayList<>();
  }

  TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MarqueeViewStyle, defStyleAttr, 0);
  interval = typedArray.getInteger(R.styleable.MarqueeViewStyle_mvInterval, interval);
  isSetAnimDuration = typedArray.hasValue(R.styleable.MarqueeViewStyle_mvAnimDuration);
  animDuration = typedArray.getInteger(R.styleable.MarqueeViewStyle_mvAnimDuration, animDuration);
  if (typedArray.hasValue(R.styleable.MarqueeViewStyle_mvTextSize)) {
   textSize = (int) typedArray.getDimension(R.styleable.MarqueeViewStyle_mvTextSize, textSize);
   textSize = DisplayUtil.px2sp(mContext, textSize);
  }
  textColor = typedArray.getColor(R.styleable.MarqueeViewStyle_mvTextColor, textColor);
  int gravityType = typedArray.getInt(R.styleable.MarqueeViewStyle_mvGravity, TEXT_GRAVITY_LEFT);
  switch (gravityType) {
   case TEXT_GRAVITY_CENTER:
    gravity = Gravity.CENTER;
    break;
   case TEXT_GRAVITY_RIGHT:
    gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
    break;
  }
  typedArray.recycle();

  setFlipInterval(interval);

  Animation animIn = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_in);
  if (isSetAnimDuration) animIn.setDuration(animDuration);
  setInAnimation(animIn);

  Animation animOut = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_out);
  if (isSetAnimDuration) animOut.setDuration(animDuration);
  setOutAnimation(animOut);
 }

 // 根據(jù)公告字符串啟動(dòng)輪播
 public void startWithText(final String notice) {
  if (TextUtils.isEmpty(notice)) return;
  getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
   @Override
   public void onGlobalLayout() {
    getViewTreeObserver().removeGlobalOnLayoutListener(this);
    startWithFixedWidth(notice, getWidth());
   }
  });
 }

 // 根據(jù)公告字符串列表啟動(dòng)輪播
 public void startWithList(List<String> notices) {
  setNotices(notices);
  start();
 }

 // 根據(jù)寬度和公告字符串啟動(dòng)輪播
 private void startWithFixedWidth(String notice, int width) {
  int noticeLength = notice.length();
  int dpW = DisplayUtil.px2dip(mContext, width);
  int limit = dpW / textSize;
  if (dpW == 0) {
   throw new RuntimeException("Please set MarqueeView width !");
  }

  if (noticeLength <= limit) {
   notices.add(notice);
  } else {
   int size = noticeLength / limit + (noticeLength % limit != 0 ? 1 : 0);
   for (int i = 0; i < size; i++) {
    int startIndex = i * limit;
    int endIndex = ((i + 1) * limit >= noticeLength ? noticeLength : (i + 1) * limit);
    notices.add(notice.substring(startIndex, endIndex));
   }
  }
  start();
 }

 // 啟動(dòng)輪播
 public boolean start() {
  if (notices == null || notices.size() == 0) return false;
  removeAllViews();

  for (int i = 0; i < notices.size(); i++) {
   final TextView textView = createTextView(notices.get(i), i);
   final int finalI = i;
   textView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
     if (onItemClickListener != null) {
      onItemClickListener.onItemClick(finalI, textView);
     }
    }
   });
   addView(textView);
  }

  if (notices.size() > 1) {
   startFlipping();
  }
  return true;
 }

 // 創(chuàng)建ViewFlipper下的TextView
 private TextView createTextView(String text, int position) {
  TextView tv = new TextView(mContext);
  tv.setGravity(gravity);
  tv.setText(text);
  tv.setTextColor(textColor);
  tv.setTextSize(textSize);
  tv.setTag(position);
  return tv;
 }

 public int getPosition() {
  return (int) getCurrentView().getTag();
 }

 public List<String> getNotices() {
  return notices;
 }

 public void setNotices(List<String> notices) {
  this.notices = notices;
 }

 public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
  this.onItemClickListener = onItemClickListener;
 }

 public interface OnItemClickListener {
  void onItemClick(int position, TextView textView);
 }

}

跑馬燈view是繼承ViewFlipper,可以看到他的結(jié)構(gòu)體

其實(shí)ViewFlipper工作機(jī)制很簡(jiǎn)單,如上圖,就是將添加到ViewFlipper中的子View按照順序定時(shí)的顯示是其中一個(gè)子View,其他的子View設(shè)置為Gone狀態(tài)

private Context mContext;
 private List<String> notices;
 private boolean isSetAnimDuration = false;
 private OnItemClickListener onItemClickListener;

 private int interval = 2000;
 private int animDuration = 500;
 private int textSize = 14;
 private int textColor = 0xffffffff;

 private int gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
 private static final int TEXT_GRAVITY_LEFT = 0, TEXT_GRAVITY_CENTER = 1, TEXT_GRAVITY_RIGHT = 2;

看出view的一些屬性,上下文,集合,是否動(dòng)畫延遲,點(diǎn)擊事件,跑馬燈的時(shí)間間隔,動(dòng)畫延遲時(shí)間,字體大小顏色,設(shè)置位置在靠左垂直中間對(duì)齊,文字的位置常量。

修改MarqueeView的構(gòu)造方法,我們可以在右鍵generate快速生成。

<declare-styleable name="MarqueeViewStyle">
  <attr name="mvInterval" format="integer|reference"/>
  <attr name="mvAnimDuration" format="integer|reference"/>
  <attr name="mvTextSize" format="dimension|reference"/>
  <attr name="mvTextColor" format="color|reference"/>
  <attr name="mvGravity">
   <enum name="left" value="0"/>
   <enum name="center" value="1"/>
   <enum name="right" value="2"/>
  </attr>
 </declare-styleable>

TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MarqueeViewStyle, defStyleAttr, 0);

首先獲取屬性集合,獲取一個(gè)mv的間隔,默認(rèn)值2000

 isSetAnimDuration = typedArray.hasValue(R.styleable.MarqueeViewStyle_mvAnimDuration);

是否設(shè)置動(dòng)畫時(shí)間的延遲

if (typedArray.hasValue(R.styleable.MarqueeViewStyle_mvTextSize)) {
   textSize = (int) typedArray.getDimension(R.styleable.MarqueeViewStyle_mvTextSize, textSize);
   textSize = DisplayUtil.px2sp(mContext, textSize);
  }

假如設(shè)置有自定義文字大小,就獲取然后px轉(zhuǎn)成sp
獲取控件位置,自由設(shè)置
在后面要回收

typedArray.recycle();

setFlipInterval(interval);設(shè)置滾屏間隔,單位毫秒

Animation animIn = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_in);
  if (isSetAnimDuration) animIn.setDuration(animDuration);
  setInAnimation(animIn);

  Animation animOut = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_out);
  if (isSetAnimDuration) animOut.setDuration(animDuration);
  setOutAnimation(animOut);

一進(jìn)一出的動(dòng)畫效果

// 根據(jù)公告字符串啟動(dòng)輪播
public void startWithText(final String notice)
暴露個(gè)公共方法,里面有測(cè)量view的getViewTreeObserver方法,里面內(nèi)部類調(diào)用了startWithFixedWidth(notice, getWidth());方法

 // 根據(jù)寬度和公告字符串啟動(dòng)輪播
 private void startWithFixedWidth(String notice, int width) {
  int noticeLength = notice.length();
  int dpW = DisplayUtil.px2dip(mContext, width);
  int limit = dpW / textSize;
  if (dpW == 0) {
   throw new RuntimeException("Please set MarqueeView width !");
  }

  if (noticeLength <= limit) {
   notices.add(notice);
  } else {
   int size = noticeLength / limit + (noticeLength % limit != 0 ? 1 : 0);
   for (int i = 0; i < size; i++) {
    int startIndex = i * limit;
    int endIndex = ((i + 1) * limit >= noticeLength ? noticeLength : (i + 1) * limit);
    notices.add(notice.substring(startIndex, endIndex));
   }
  }
  start();
 }

轉(zhuǎn)換得到一個(gè)dp的寬度,限制字?jǐn)?shù)長(zhǎng)度大小。假如字符串小于直接add。假如過(guò)長(zhǎng)取余得到行數(shù)。然后循環(huán),獲取那行的頭尾,末尾假如2行字?jǐn)?shù)還是比總體長(zhǎng)度大就取總體長(zhǎng)度,假如小于總體長(zhǎng)度,就取那行的末尾下表。
然后開始播放

 // 啟動(dòng)輪播
 public boolean start() {
  if (notices == null || notices.size() == 0) return false;
  removeAllViews();

  for (int i = 0; i < notices.size(); i++) {
   final TextView textView = createTextView(notices.get(i), i);
   final int finalI = i;
   textView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
     if (onItemClickListener != null) {
      onItemClickListener.onItemClick(finalI, textView);
     }
    }
   });
   addView(textView);
  }

  if (notices.size() > 1) {
   startFlipping();
  }
  return true;
 }

創(chuàng)建部署每行的tv

 // 創(chuàng)建ViewFlipper下的TextView
 private TextView createTextView(String text, int position) {
  TextView tv = new TextView(mContext);
  tv.setGravity(gravity);
  tv.setText(text);
  tv.setTextColor(textColor);
  tv.setTextSize(textSize);
  tv.setTag(position);
  return tv;
 }

然后將所有的textview add起來(lái),然后開始播放。后面就是做個(gè)點(diǎn)擊回調(diào),然后set get這個(gè)公告的集合。
最后不要忘了在布局頂層加入

xmlns:app="http://schemas.android.com/apk/res-auto"

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android仿百度福袋紅包界面

    android仿百度福袋紅包界面

    雙十一馬上到了,又進(jìn)入到搶紅包的季節(jié),本篇文章介紹了android仿百度福袋紅包界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2016-11-11
  • 詳解Android Bitmap的使用

    詳解Android Bitmap的使用

    這篇文章主要介紹了詳解Android Bitmap的使用方法,幫助大家更好的理解和利用Android進(jìn)行開發(fā),感興趣的朋友可以了解下
    2021-02-02
  • Android studio配置lambda表達(dá)式教程

    Android studio配置lambda表達(dá)式教程

    Java 8的一個(gè)大亮點(diǎn)是引入Lambda表達(dá)式,使用它設(shè)計(jì)的代碼會(huì)更加簡(jiǎn)潔。接下來(lái)通過(guò)本文給大家介紹Android studio配置lambda表達(dá)式教程,需要的朋友參考下吧
    2017-05-05
  • 分享Android中Toast的自定義使用

    分享Android中Toast的自定義使用

    Android中的Toast是一種簡(jiǎn)易的消息提示框,toast提示框不能被用戶點(diǎn)擊,toast會(huì)根據(jù)用戶設(shè)置的顯示時(shí)間后自動(dòng)消失。本文將介紹Toast的自定義使用,下面一起來(lái)看看吧。
    2016-08-08
  • Android學(xué)習(xí)之介紹Binder的簡(jiǎn)單使用

    Android學(xué)習(xí)之介紹Binder的簡(jiǎn)單使用

    BInder方面的資料雖然感覺(jué)看的比較多,但是真正用的時(shí)候才發(fā)現(xiàn)有很多地方模棱兩棵的,所以,打算用一個(gè)實(shí)例再來(lái)鞏固一下binder的使用方法。這篇文章主要介紹了Android中Binder的簡(jiǎn)單使用,文中給出詳細(xì)的示例代碼,需要的朋友可以參考下
    2016-12-12
  • Android中TextView自動(dòng)識(shí)別url且實(shí)現(xiàn)點(diǎn)擊跳轉(zhuǎn)

    Android中TextView自動(dòng)識(shí)別url且實(shí)現(xiàn)點(diǎn)擊跳轉(zhuǎn)

    這篇文章主要介紹了關(guān)于Android中TextView自動(dòng)識(shí)別url且實(shí)現(xiàn)點(diǎn)擊跳轉(zhuǎn)的相關(guān)資料,文中給出了詳細(xì)的示例代碼,對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-03-03
  • Android截屏SurfaceView黑屏問(wèn)題的解決辦法

    Android截屏SurfaceView黑屏問(wèn)題的解決辦法

    這篇文章主要為大家詳細(xì)介紹了Android截屏SurfaceView黑屏問(wèn)題的解決辦法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Android亮屏和熄屏控制實(shí)例詳解

    Android亮屏和熄屏控制實(shí)例詳解

    這篇文章主要介紹了Android亮屏和熄屏控制的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android亮屏與息屏的原理,實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-02-02
  • Android中使用GridLayout網(wǎng)格布局來(lái)制作簡(jiǎn)單的計(jì)算器App

    Android中使用GridLayout網(wǎng)格布局來(lái)制作簡(jiǎn)單的計(jì)算器App

    這篇文章主要介紹了Android中使用GridLayout網(wǎng)格布局來(lái)制作簡(jiǎn)單的計(jì)算器App的實(shí)例,GridLayout比表格布局TabelLayout更容易用來(lái)制作計(jì)算器這樣的多按鈕排列的界面,需要的朋友可以參考下
    2016-04-04
  • Android顯式啟動(dòng)與隱式啟動(dòng)Activity的區(qū)別介紹

    Android顯式啟動(dòng)與隱式啟動(dòng)Activity的區(qū)別介紹

    為什么要寫顯式啟動(dòng)與隱式啟動(dòng)Activity,Android的Acitivity啟動(dòng)大致有兩種方式:顯式啟動(dòng)與隱式啟動(dòng),下面分別介紹
    2014-09-09

最新評(píng)論