Android文字广告(Textview上下滚动),使用TextSwitcher控件的完整实现
实现步骤:
a、xml布局
<TextSwitcherandroid:id="@+id/textSwitcher"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="8dp" />
b、TextSwitchBanner.java
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;import java.util.List;/*** 结合TextSwitcher空间实现文字广告,轮播*/
public class TextSwitchBanner implements ViewSwitcher.ViewFactory, View.OnClickListener {private Context context;private TextSwitcher textSwitcher;private List<String> texts;private OnItemClickLitener listener;private int marker;private AnimationSet InAnimationSet;private AnimationSet OutAnimationSet;private int delayTime = 2000;private static final int DURATION = 1000;private Handler handler = new Handler();private Runnable task = new Runnable() {@Overridepublic void run() {nextView();handler.postDelayed(task, delayTime * 2);}};public TextSwitchBanner(Context context, TextSwitcher textSwitcher) {this.context = context;this.textSwitcher = textSwitcher;init();}private void init() {textSwitcher.setFactory(this);textSwitcher.setText("");createAnimation();textSwitcher.setInAnimation(InAnimationSet);textSwitcher.setOutAnimation(OutAnimationSet);textSwitcher.setOnClickListener(this);}@Overridepublic View makeView() {TextView textView = new TextView(context);textView.setGravity(Gravity.START);textView.setSingleLine(true);textView.setGravity(Gravity.CENTER_VERTICAL);textView.setEllipsize(TextUtils.TruncateAt.END);textView.setTextColor(Color.BLACK);return textView;}@Overridepublic void onClick(View v) {if(listener != null && texts != null && texts.size() != 0){listener.onClick(texts, marker);}}public void update(List<String> list){if(list == null || list.size() == 0){return;}stop();texts = list;marker = 0;textSwitcher.setText(texts.get(0));start();}public void start() {stop();handler.postDelayed(task, delayTime);}public void stop(){handler.removeCallbacks(task);}public int getMarker() {return marker;}public TextSwitchBanner setTexts(List<String> texts) {this.texts = texts;return this;}public void setDelayTime(int delayTime) {this.delayTime = delayTime;}private void createAnimation() {AlphaAnimation alphaAnimation;TranslateAnimation translateAnimation;int h = textSwitcher.getHeight();if (h <= 0) {textSwitcher.measure(0,0);h = textSwitcher.getMeasuredHeight();}InAnimationSet = new AnimationSet(true);OutAnimationSet = new AnimationSet(true);alphaAnimation = new AlphaAnimation(0,1);translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,Animation.ABSOLUTE, h, Animation.ABSOLUTE, 0);InAnimationSet.addAnimation(alphaAnimation);InAnimationSet.addAnimation(translateAnimation);InAnimationSet.setDuration(DURATION);alphaAnimation = new AlphaAnimation(1,0);translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -h);OutAnimationSet.addAnimation(alphaAnimation);OutAnimationSet.addAnimation(translateAnimation);OutAnimationSet.setDuration(DURATION);}private void nextView() {marker = ++marker % texts.size();textSwitcher.setText(texts.get(marker));}public interface OnItemClickLitener{void onClick(List<String> list, int position);}public void setOnItemClickListener(OnItemClickLitener listener){this.listener = listener;}}
c、MainActivity.java
private TextSwitcher m_textSwitcher;
m_textSwitcher = m_view.findViewById(R.id.textSwitcher);
/*** 初始化textbanner*/private void initTextBanner() {m_textSwitchBanner = new TextSwitchBanner(getActivity(), m_textSwitcher);m_textSwitchBanner.setOnItemClickListener(new TextSwitchBanner.OnItemClickLitener() {@Overridepublic void onClick(List<String> list, int position) {ToastUtils.s(getActivity(), "click: " + position);}});}
/*** 更新文字广告*/private void updateTextViewBanner() {List<String> list = new ArrayList<>();for (int i = 0; i < m_listMsgInfo.size(); i++) {list.add(m_listMsgInfo.get(i).getTitle());}m_textSwitchBanner.update(list);}
完!!!
Android文字广告(Textview上下滚动),使用TextSwitcher控件的完整实现相关推荐
- android文字广告的循环滚动,android怎样写一个循环文字滚动的TextView
效果图: 在layout中这样来声明: android:layout_width="wrap_content" android:layout_height="wrap_c ...
- android中的TextView组件,Android-TextView文本视图控件详解
TextView是Android开发当中运用到最多的控件之一,显示一行或者多行文本,也可以给上下左右设置图片,并且结合SpannableString和其子类可以做到图文混排. TextView文本视图 ...
- android禁止下拉刷新,Android开发之无痕过渡下拉刷新控件的实现思路详解
相信大家已经对下拉刷新熟悉得不能再熟悉了,市面上的下拉刷新琳琅满目,然而有很多在我看来略有缺陷,接下来我将说明一下存在的缺陷问题,然后提供一种思路来解决这一缺陷,废话不多说!往下看嘞! 1.市面一些下 ...
- Android 打造完美的侧滑菜单/侧滑View控件
概述 Android 打造完美的侧滑菜单/侧滑View控件,完全自定义实现,支持左右两个方向弹出,代码高度简洁流畅,兼容性高,控件实用方便. 详细 代码下载:http://www.demodashi. ...
- 如何在Android实现桌面清理内存简单Widget小控件
如何在Android实现桌面清理内存简单Widget小控件 我们经常会看到类似于360.金山手机卫士一类的软件会带一个widget小控件,显示在桌面上,上面会显示现有内存大小,然后会带一个按键功能来一 ...
- Android入门(二)——常见布局与控件
文章目录 一.常见界面布局 1.线性布局 LinearLayout 2.相对布局 RelativeLayout 3.表格布局 TableLayout 4.帧布局 FrameLayout 二.常见界面控 ...
- android 桌面图标的点击放大效果,Android仿英语流利说取词放大控件的实现方法(附demo源码下载)...
本文实例讲述了Android仿英语流利说取词放大控件的实现方法.分享给大家供大家参考,具体如下: 1 取词放大控件 英语流利说是一款非常帮的口语学习app,在app的修炼页面长按屏幕,会弹出一个放大镜 ...
- android点击隐藏控件,Android编程实现点击EditText之外的控件隐藏软键盘功能
本文实例讲述了Android编程实现点击EditText之外的控件隐藏软键盘功能.分享给大家供大家参考,具体如下: 工具类 ... public static void hideKeyboard(Co ...
- Android Native APP开发笔记:使用WebView控件加载网页
文章目录 目的 基础使用 处理网页导航 加载本地网页 Web和Native之间交互 调试Web应用 处理页面重绘 总结 目的 WebView是一个比较常用的控件,功能上也比较单一,就是用来加载网页的, ...
最新文章
- Swift中NSRange和Range的转换
- 如何让低版本IE浏览器支持HTML5和CSS3
- Android 水波效果 | 涟漪效果 实现
- Java-string字符串
- Windows10系统Python语言利用Pywin32模块模拟鼠标点击
- str 类常用的函数
- 《岸上的波妞》观后感
- 对Extjs中store的多种操作
- ORACLE基础学习-RMAN应用-控制文件恢复
- Oracle官方并发教程之中断
- mysql数据库云读取_云数据库 MySQL版
- 全国计算机等级考试题库二级C操作题100套(第17套)
- MySQL数据库事务中的行级锁,表级锁,页级锁
- [你必须知道的.NET] 第四回:后来居上:class和struct
- bzoj 2169: 连边
- python制作表格的语句_python读取excel表格生成sql语句 第一版
- 机刷实名认证软件_代刷网已上架抖音代实名认证和抖音音乐人认证
- linux下cpp文件,linux怎么编译cpp文件?
- 美团成都一面面经及详细答案
- python视频笔记17(控制窗体)