Android實(shí)現(xiàn)垂直跑馬燈效果
在我們開發(fā)過程中,跑馬燈這個(gè)功能非常實(shí)用的,在實(shí)現(xiàn)這個(gè)功能的時(shí)候,這個(gè)時(shí)候我們通常需要找demo來實(shí)現(xiàn)這個(gè)方法,我從github上面找到這個(gè)demo感覺很好用,所以就要實(shí)現(xiàn)了這個(gè)功能嘍MarqueeView,看這個(gè)工具類,因?yàn)槲艺疫@個(gè)類的時(shí)候是沒有點(diǎn)擊事件的,所以我給它加了一個(gè)點(diǎn)擊事件,看這個(gè)工具類
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;
//點(diǎn)擊事件
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);
}
// 根據(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 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);
}
}
這就是它實(shí)現(xiàn)的方式,我從中加了點(diǎn)擊事件,所以它的用法是這樣的
<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="弘生活A(yù)PP改版了" />
verticalSwitchTextView1 = (MarqueeView) rootView.findViewById(R.id.vertical_switch_textview1);
List<String> info = new ArrayList<>();
info.add("1.能夠適應(yīng)多行長文本的Android TextView的例子");
info.add("2.\"科比,!");
info.add("3. GitHub帳號(hào):zhangyuanchong");
info.add("4.\"理解的也很簡單,");
info.add("5. 破解密鑰");
info.add("6. 實(shí)現(xiàn)了兩種方式");
verticalSwitchTextView1.startWithList(info);
verticalSwitchTextView1.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
@Override
public void onItemClick(int position, TextView textView) {
position = position + 1;
Toast.makeText(getActivity(), "點(diǎn)擊了" + position, Toast.LENGTH_SHORT).show();
}
});
這樣就直接實(shí)現(xiàn)嘍,其實(shí)還是蠻簡單的呢。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)的簡單藍(lán)牙程序示例
這篇文章主要介紹了Android實(shí)現(xiàn)的簡單藍(lán)牙程序,結(jié)合實(shí)例形式分析了Android藍(lán)牙程序的原理與客戶端、服務(wù)器端具體實(shí)現(xiàn)步驟,需要的朋友可以參考下2016-10-10
Android實(shí)現(xiàn)多線程下載文件的方法
這篇文章主要介紹了Android實(shí)現(xiàn)多線程下載文件的方法,以實(shí)例形式較為詳細(xì)的分析了Android多線程文件傳輸及合并等操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android普通應(yīng)用升級為系統(tǒng)應(yīng)用并獲取系統(tǒng)權(quán)限的操作
這篇文章主要介紹了Android普通應(yīng)用升級為系統(tǒng)應(yīng)用并獲取系統(tǒng)權(quán)限的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android 進(jìn)程間通信實(shí)現(xiàn)原理分析
只有你允許客戶端從不同的應(yīng)用程序?yàn)榱诉M(jìn)程間的通信而去訪問你的service,以及想在你的service處理多線程,下面為大家詳細(xì)介紹下2013-06-06
Android手勢滑動(dòng)實(shí)現(xiàn)ImageView縮放圖片大小
這篇文章主要為大家詳細(xì)介紹了Android手勢滑動(dòng)實(shí)現(xiàn)ImageView縮放圖片大小的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02
android自定義Toast設(shè)定顯示時(shí)間
這篇文章主要為大家詳細(xì)介紹了android自定義Toast設(shè)定顯示時(shí)間,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android自定義控件單位尺寸實(shí)現(xiàn)代碼
這篇文章主要介紹了Android自定義控件單位尺寸實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
PopupWindow?RecyclerView實(shí)現(xiàn)下拉選擇Spinner示例解析
這篇文章主要介紹了PopupWindow?RecyclerView實(shí)現(xiàn)下拉選擇Spinner示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

