电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

Android自定義TextBanner實現自動滾動

瀏覽:43日期: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
相關文章:
主站蜘蛛池模板: 全自动五线打端沾锡机,全自动裁线剥皮双头沾锡机,全自动尼龙扎带机-东莞市海文能机械设备有限公司 | 铝合金风口-玻璃钢轴流风机-玻璃钢屋顶风机-德州东润空调设备有限公司 | 美国查特CHART MVE液氮罐_查特杜瓦瓶_制造全球品质液氮罐 | 蜘蛛车-高空作业平台-升降机-高空作业车租赁-臂式伸缩臂叉装车-登高车出租厂家 - 普雷斯特机械设备(北京)有限公司 | 精密模具制造,注塑加工,吹塑和吹瓶加工,EPS泡沫包装生产 - 济南兴田塑胶有限公司 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | Boden齿轮油泵-ketai齿轮泵-yuken油研-无锡新立液压有限公司 | EFM 022静电场测试仪-套帽式风量计-静电平板监测器-上海民仪电子有限公司 | 电动卫生级调节阀,电动防爆球阀,电动软密封蝶阀,气动高压球阀,气动对夹蝶阀,气动V型调节球阀-上海川沪阀门有限公司 | 电子书导航网_电子书之家_电子书大全_最新电子书分享发布平台 | 广州中央空调回收,二手中央空调回收,旧空调回收,制冷设备回收,冷气机组回收公司-广州益夫制冷设备回收公司 | 山东柳店新能源科技有限公司| 电气控制系统集成商-PLC控制柜变频控制柜-非标自动化定制-电气控制柜成套-NIDEC CT变频器-威肯自动化控制 | 北京律师事务所_房屋拆迁律师_24小时免费法律咨询_云合专业律师网 | 变色龙PPT-国内原创PPT模板交易平台 - PPT贰零 - 西安聚讯网络科技有限公司 | 电镀电源整流器_高频电解电源_单脉双脉冲电源 - 东阳市旭东电子科技 | 达利园物流科技集团-| TMT观察网_独特视角观察TMT行业| 电加热导热油炉-空气加热器-导热油加热器-翅片电加热管-科安达机械 | 热镀锌槽钢|角钢|工字钢|圆钢|H型钢|扁钢|花纹板-天津千百顺钢铁贸易有限公司 | 东莞压铸厂_精密压铸_锌合金压铸_铝合金压铸_压铸件加工_东莞祥宇金属制品 | 杭州可当科技有限公司—流量卡_随身WiFi_AI摄像头一站式解决方案 | 保镖公司-私人保镖-深圳保镖公司【环宇兄弟保镖】 | 岩棉切条机厂家_玻璃棉裁条机_水泥基保温板设备-廊坊鹏恒机械 | 飞象网 - 通信人每天必上的网站| SDG吸附剂,SDG酸气吸附剂,干式酸性气体吸收剂生产厂家,超过20年生产使用经验。 - 富莱尔环保设备公司(原名天津市武清县环保设备厂) | 北京康百特科技有限公司-分子蒸馏-短程分子蒸馏设备-实验室分子蒸馏设备 | 杭州中策电线|中策电缆|中策电线|杭州中策电缆|杭州中策电缆永通集团有限公司 | 杭州中策电线|中策电缆|中策电线|杭州中策电缆|杭州中策电缆永通集团有限公司 | 电动打包机_气动打包机_钢带捆扎机_废纸打包机_手动捆扎机 | MVR蒸发器厂家-多效蒸发器-工业废水蒸发器厂家-康景辉集团官网 | 周口风机|周风风机|河南省周口通用风机厂 | 雷达液位计_超声波风速风向仪_雨量传感器_辐射传感器-山东风途物联网 | 英超直播_英超免费在线高清直播_英超视频在线观看无插件-24直播网 | 月嫂_保姆_育婴_催乳_母婴护理_产后康复_养老护理-吉祥到家家政 硫酸亚铁-聚合硫酸铁-除氟除磷剂-复合碳源-污水处理药剂厂家—长隆科技 | 工作心得_读书心得_学习心得_找心得体会范文就上学道文库 | 地埋式垃圾站厂家【佳星环保】小区压缩垃圾中转站转运站 | 压砖机_电动螺旋压力机_粉末成型压力机_郑州华隆机械tel_0371-60121717 | 上海小程序开发-小程序制作-上海小程序定制开发公司-微信商城小程序-上海咏熠 | 视频教程导航网_视频教程之家_视频教程大全_最新视频教程分享发布平台 | 渣油泵,KCB齿轮泵,不锈钢齿轮泵,重油泵,煤焦油泵,泊头市泰邦泵阀制造有限公司 |