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使用原生組件WebView加載網(wǎng)頁(yè)和數(shù)據(jù)的方法
這篇文章主要介紹了Android使用原生組件WebView加載網(wǎng)頁(yè)和數(shù)據(jù)的方法的相關(guān)資料,需要的朋友可以參考下2016-09-09
Android開(kāi)發(fā)從相機(jī)或相冊(cè)獲取圖片裁剪
當(dāng)我們需要上傳圖片時(shí),想要裁剪成我們需要的尺寸大小,android手機(jī)都帶有這個(gè)功能,很容易,那么此功能是如何實(shí)現(xiàn)的呢?下面小編給大家介紹Android開(kāi)發(fā)從相機(jī)或相冊(cè)獲取圖片裁剪,需要的朋友可以參考下2015-10-10
Input系統(tǒng)截?cái)嗖呗缘姆治雠c應(yīng)用詳解
這篇文章主要為大家介紹了Input系統(tǒng)截?cái)嗖呗缘姆治雠c應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android實(shí)現(xiàn)點(diǎn)擊圖片上傳SQLite數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)點(diǎn)擊圖片上傳SQLite數(shù)據(jù)庫(kù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
手把手教你Android全局觸摸事件監(jiān)聽(tīng)
這篇文章主要介紹了Android全局觸摸事件監(jiān)聽(tīng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Android ListView與RecycleView的對(duì)比使用解析
這篇文章主要介紹了Android ListView與RecycleView的對(duì)比使用解析,需要的朋友可以參考下2017-12-12
Flutter的鍵值存儲(chǔ)數(shù)據(jù)庫(kù)使用示例詳解
這篇文章主要為大家介紹了Flutter的鍵值存儲(chǔ)數(shù)據(jù)庫(kù)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Android studio git創(chuàng)建與刪除標(biāo)簽(Tag)的教程詳解
這篇文章主要介紹了Android studio git創(chuàng)建與刪除標(biāo)簽(Tag)的教程詳解,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12

