国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現垂直跑馬燈效果

2019-10-22 18:12:26
字體:
來源:轉載
供稿:網友

在我們開發過程中,跑馬燈這個功能非常實用的,在實現這個功能的時候,這個時候我們通常需要找demo來實現這個方法,我從github上面找到這個demo感覺很好用,所以就要實現了這個功能嘍MarqueeView,看這個工具類,因為我找這個類的時候是沒有點擊事件的,所以我給它加了一個點擊事件,看這個工具類

public class MarqueeView extends ViewFlipper { private Context mContext; private List<String> notices; private boolean isSetAnimDuration = false; private int contentSize; private int interval = 1000; private int animDuration = 500; private int textSize = 14; private int textColor = 0xffffffff; private int gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL; //點擊事件 private OnItemClickListener onItemClickListener; 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); 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); } // 根據公告字符串啟動輪播 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()); } }); } // 根據公告字符串列表啟動輪播 public void startWithList(List<String> notices) { setNotices(notices); start(); } // 根據寬度和公告字符串啟動輪播 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(); } // 啟動輪播 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; } // 創建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 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); }}

這就是它實現的方式,我從中加了點擊事件,所以它的用法是這樣的

<com.redsun.property.views.MarqueeView android:id="@+id/vertical_switch_textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_toLeftOf="@+id/total_quantity" android:layout_toRightOf="@+id/news_image" android:background="@color/white" android:ellipsize="end" android:maxEms="10" android:maxLength="10" android:textColor="@color/gray_dark" android:textSize="@dimen/font_normal" app:mvAnimDuration="1000" app:mvInterval="3000" app:mvTextColor="@color/black" app:mvTextSize="14sp" tools:text="弘生活APP改版了" />
verticalSwitchTextView1 = (MarqueeView) rootView.findViewById(R.id.vertical_switch_textview1);List<String> info = new ArrayList<>();info.add("1.能夠適應多行長文本的Android TextView的例子");info.add("2./"科比,!");info.add("3. GitHub帳號:zhangyuanchong");info.add("4./"理解的也很簡單,");info.add("5. 破解密鑰");info.add("6. 實現了兩種方式");verticalSwitchTextView1.startWithList(info);verticalSwitchTextView1.setOnItemClickListener(new MarqueeView.OnItemClickListener() { @Override public void onItemClick(int position, TextView textView) { position = position + 1; Toast.makeText(getActivity(), "點擊了" + position, Toast.LENGTH_SHORT).show(); }});

這樣就直接實現嘍,其實還是蠻簡單的呢。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 芦溪县| 寿光市| 香河县| 米林县| 武邑县| 延安市| 宝鸡市| 崇义县| 德钦县| 平顺县| 丹凤县| 清流县| 泌阳县| 嘉鱼县| 怀来县| 宁城县| 金昌市| 洛宁县| 达州市| 英山县| 邳州市| 平罗县| 成武县| 宜黄县| 夏河县| 定陶县| 方城县| 嘉定区| 祥云县| 原平市| 武汉市| 旌德县| 望都县| 琼结县| 富源县| 荆州市| 沙田区| 和政县| 琼海市| 隆昌县| 化隆|