Android自定义TextBanner实现自动滚动
程序员文章站
2022-04-15 20:13:16
本文实例为大家分享了android自定义textbanner实现自动滚动的具体代码,供大家参考,具体内容如下1、textbannerpackage com.example.myapplication....
本文实例为大家分享了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("你好"); arraylist.add("你好"); textbanner viewbyid = this.findviewbyid(r.id.text_banner); viewbyid.setdata(arraylist); viewbyid.startanimation(); } }
具体效果
没有自测哦 有bug自己解决
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 一秒扫雷
下一篇: 业界首发|阿里云重磅发布云原生架构白皮书