Android自定義TextBanner實(shí)現(xiàn)自動(dòng)滾動(dòng)
本文實(shí)例為大家分享了Android自定義TextBanner實(shí)現(xiàn)自動(dòng)滾動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
1、TextBanner
package com.example.myapplication.customview;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ViewFlipper;
import com.example.myapplication.R;
import java.util.ArrayList;
import java.util.List;
public class TextBanner extends ViewGroup {
private List<String> mData = new ArrayList<>();
private ViewFlipper viewFlipper;
private int parentWidthSpec;
public TextBanner(Context context) {
super(context);
}
public TextBanner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextBanner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int top = 0;
int bottom = getChildAt(0).getMeasuredHeight();
int left = 0;
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
left = (parentWidthSpec - view.getMeasuredWidth()) / 2;
view.layout(left, top, left + view.getMeasuredWidth(), bottom);
top += view.getMeasuredHeight();
bottom = top + view.getMeasuredHeight();
}
Log.d("tzg", "bottom: " + bottom);
Log.d("tzg", "top: " + top);
}
public void setData(List<String> data) {
mData.clear();
if (data.isEmpty()) {
return;
}
this.mData = data;
setTextList();
}
private void setTextList() {
viewFlipper = (ViewFlipper) LayoutInflater.from(getContext()).inflate(R.layout.flow_layout_viewflip, this, false);
for (String mDatum : mData) {
TextView view = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.flow_layout_textview, this, false);
view.setText(mDatum);
viewFlipper.addView(view);
}
viewFlipper.setInAnimation(getContext(), R.anim.come_in);
viewFlipper.setOutAnimation(getContext(), R.anim.come_out);
viewFlipper.setFlipInterval(2000);
addView(viewFlipper);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
parentWidthSpec = MeasureSpec.getSize(widthMeasureSpec);
int parentHeightSpec = MeasureSpec.getSize(heightMeasureSpec);
int childWidth = MeasureSpec.makeMeasureSpec(parentWidthSpec, MeasureSpec.AT_MOST);
int childHeight = MeasureSpec.makeMeasureSpec(parentHeightSpec, MeasureSpec.AT_MOST);
int totalHeight = getChildAt(0).getMeasuredHeight();
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
measureChild(view, childWidth, childHeight);
}
Log.d("tzg", "totalCount: " + totalHeight);
setMeasuredDimension(parentWidthSpec, totalHeight);
}
public void startAnimation() {
// 1、設(shè)置幻燈片的形式滾動(dòng)
// viewFlipper.startFlipping();
// 2、設(shè)置自動(dòng)翻頁(yè)滾動(dòng)
viewFlipper.setAutoStart(true);
viewFlipper.isAutoStart();
}
}
用到的資源
1、動(dòng)畫(huà)資源
(1)、come_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1000" android:fromYDelta="100%p" android:toYDelta="0"/> </set>
(2)、come_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1000" android:fromYDelta="0" android:toYDelta="-100%p"/> </set>
2、布局資源
(1)、flow_layout_viewflip.xml
<?xml version="1.0" encoding="utf-8"?> <ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> </ViewFlipper>
(2)、flow_layout_textview.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="demo" android:textColor="#FF00FF" />
3、在mainActivity中的使用
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.example.myapplication.customview.FlowLayout;
import com.example.myapplication.customview.TextBanner;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("111111111");
arrayList.add("222222222222444444444444");
arrayList.add("你好5");
arrayList.add("你好633");
arrayList.add("你好a7好a7");
arrayList.add("你好7889");
arrayList.add("你好2323423423 ");
arrayList.add("你好sdfsfada你好sdfsfada ");
arrayList.add("你好34345");
arrayList.add("pppppppp");
arrayList.add("你好");
arrayList.add("你好你好");
arrayList.add("電視");
arrayList.add("冰箱冰箱冰箱冰箱冰箱冰箱冰箱冰箱冰箱冰箱");
arrayList.add("woaoni");
arrayList.add("你好");
arrayList.add("你好");
TextBanner viewById = this.findViewById(R.id.text_banner);
viewById.setData(arrayList);
viewById.startAnimation();
}
}
具體效果

沒(méi)有自測(cè)哦 有bug自己解決
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android 實(shí)現(xiàn)ScrollView自動(dòng)滾動(dòng)的實(shí)例代碼
- android ListView自動(dòng)滾動(dòng)方法
- Android使用Recyclerview實(shí)現(xiàn)圖片水平自動(dòng)循環(huán)滾動(dòng)效果
- android scrollview 自動(dòng)滾動(dòng)到頂部或者底部的實(shí)例
- Android 使用ViewPager自動(dòng)滾動(dòng)循環(huán)輪播效果
- Android ViewPager無(wú)限循環(huán)滑動(dòng)并可自動(dòng)滾動(dòng)完整實(shí)例
- Android ListView滾動(dòng)到底后自動(dòng)加載數(shù)據(jù)
- Android使用自定義屬性實(shí)現(xiàn)圖片自動(dòng)播放滾動(dòng)的功能
- Android簡(jiǎn)單實(shí)現(xiàn)無(wú)限滾動(dòng)自動(dòng)滾動(dòng)的ViewPager
- Android編程實(shí)現(xiàn)TextView垂直自動(dòng)滾動(dòng)功能【附demo源碼下載】
相關(guān)文章
Android DrawableTextView圖片文字居中顯示實(shí)例
在我們開(kāi)發(fā)中,TextView設(shè)置Android:drawableLeft一定使用的非常多,但Drawable和Text同時(shí)居中顯示可能不好控制,小編想到通過(guò)自定義TextView實(shí)現(xiàn),具體詳情大家參考下本文2017-03-03
Android網(wǎng)絡(luò)判斷知識(shí)小結(jié)
本文通過(guò)兩段實(shí)例代碼分別給大家介紹Android中判斷當(dāng)前網(wǎng)絡(luò)是否可用和Android 關(guān)于判斷應(yīng)用是否有網(wǎng)絡(luò)的相關(guān)知識(shí),對(duì)android網(wǎng)絡(luò)判斷相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-12-12
Android編程之內(nèi)存溢出解決方案(OOM)實(shí)例總結(jié)
這篇文章主要介紹了Android編程之內(nèi)存溢出解決方案(OOM),結(jié)合實(shí)例實(shí)例總結(jié)分析了Android編程過(guò)程中常見(jiàn)的內(nèi)存溢出情況與對(duì)應(yīng)的解決方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android實(shí)現(xiàn)計(jì)步進(jìn)度的環(huán)形Progress
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)計(jì)步進(jìn)度的環(huán)形Progress,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android 監(jiān)聽(tīng)軟鍵盤(pán)狀態(tài)的實(shí)例詳解
這篇文章主要介紹了Android 監(jiān)聽(tīng)軟鍵盤(pán)狀態(tài)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能掌握這樣的知識(shí),需要的朋友可以參考下2017-09-09
android的RecyclerView實(shí)現(xiàn)拖拽排序和側(cè)滑刪除示例
在平時(shí)開(kāi)發(fā)應(yīng)用的時(shí)候,經(jīng)常會(huì)遇到列表排序、滑動(dòng)刪除的需求。這篇文章主要介紹了android的RecyclerView實(shí)現(xiàn)拖拽排序和側(cè)滑刪除示例,有興趣的可以了解一下。2017-02-02
android實(shí)現(xiàn)通過(guò)NFC讀取卡號(hào)
這篇文章主要介紹了android實(shí)現(xiàn)通過(guò)NFC讀取卡號(hào),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09

