亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Android自定義TextBanner實現自動滾動

瀏覽:9日期:2022-09-23 10:44:01

本文實例為大家分享了Android自定義TextBanner實現自動滾動的具體代碼,供大家參考,具體內容如下

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、設置幻燈片的形式滾動 // viewFlipper.startFlipping(); // 2、設置自動翻頁滾動 viewFlipper.setAutoStart(true); viewFlipper.isAutoStart(); }}

用到的資源

1、動畫資源

(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(); }}

具體效果

Android自定義TextBanner實現自動滾動

沒有自測哦 有bug自己解決

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

標簽: Android
相關文章:
主站蜘蛛池模板: 成人av在线播放 | 在线爽| 激情一区 | 久久996re热这里只有精品 | 黄色一毛片 | 中文精品爱久久久国产 | 欧美草| 日韩视频一 | 日韩 亚洲 中文 图片 小说 | 国产亚洲精品一区二区三区 | 国产91精品一区二区 | 九九视频高清视频免费观看 | 欧美一级黄色录像片 | 免费一级黄色毛片 | 欧美一级毛片高清免费观看 | 亚洲欧美色图 | 成人亚洲视频 | 中国一级做a爱片免费 | 青青成人福利国产在线视频 | 成人精品视频一区二区在线 | 中国一级黄色大片 | 一级视频在线免费观看 | 欧美精品二区 | 精品九九九 | 精品国产呦系列在线看 | 香蕉eeww99国产在线观看 | 成免费网站 | 婷婷视频在线 | 真人一级毛片免费完整视 | 亚洲国产天堂久久九九九 | 极品精品国产超清自在线观看 | 香港a毛片 | www.黄色毛片 | 欧美国产在线观看 | 在线成人看片 | 国产成人亚洲综合a∨婷婷 国产成人亚洲综合无 | 1000部又爽又黄无遮挡的视频 | 国产污片在线观看 | 亚洲香蕉在线视频 | 国产婷婷综合丁香亚洲欧洲 | 你懂的免费在线 |