国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android自定義TextBanner實(shí)現(xiàn)自動(dòng)滾動(dòng)

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

本文實(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)畫資源

(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實(shí)現(xiàn)自動(dòng)滾動(dòng)

沒有自測(cè)哦 有bug自己解決

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 亚欧成人毛片一区二区三区四区 | 国产精品a区 | 国产三级久久久精品三级 | 亚洲精品成人7777在线观看 | 成人国产精品999视频 | 国产成人做受免费视频 | 久久亚洲精品中文字幕三区 | 日韩久久网 | 成 人 黄 色 视频播放16 | 亚洲人成网址在线播放a | 92手机看片福利永久国产 | 成人亚洲天堂 | 视频国产91 | 一级毛片视频播放 | 正在播放的国产a一片 | 日韩三级免费观看 | 国产成人深夜福利短视频99 | 自拍偷拍欧美视频 | 国产亚洲欧美在线人成aaaa | 亚洲欧美另类色妞网站 | 久久国产影院 | 性感一级毛片 | www.99在线| 欧美α一级毛片 | 日韩欧美精品综合一区二区三区 | 中文字幕亚洲综合久久男男 | 日韩一级a毛片欧美区 | 午夜刺激爽爽视频免费观看 | 久久99亚洲精品久久久久网站 | 一区二区三区日韩 | 欧美一做特黄毛片 | 欧美成成人免费 | 精品国产日韩亚洲一区在线 | 久久国产成人午夜aⅴ影院 久久国产成人亚洲精品影院老金 | 国产国产人免费视频成69堂 | 国产手机精品一区二区 | 中文字幕亚洲精品日韩精品 | 成人软件18免费网站 | 久久精品国产亚洲精品2020 | 人人公开免费超级碰碰碰视频 | 欧美美女视频网站 |