TextSwitcher实现文字上下翻牌效果

我们知道TextView是最常用来显示文字的组件,那么加入有一系列的内容,需要逐条的上下滚动播出,这样的效果就需要TextSwitcher来帮忙了。

加入布局中已经存在一个TextSwitcher元素,那么使用时,需要写如下代码

        tvNotice = (TextSwitcher)rootView.findViewById(R.id.tv_notice);
        tvNotice.setFactory(new ViewSwitcher.ViewFactory() {
			//这里 用来创建内部的视图,这里创建TextView,用来显示文字
            public View makeView() {
                TextView tv =new TextView(getContext());
				//设置文字大小
                tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,getResources().getDimension(R.dimen.group_notice_font_size));
				//设置文字 颜色
                tv.setTextColor(getResources().getColor(R.color.font_333333));
                return tv;
            }
        });

然后自己可用timer或者Thread去控制轮播,轮播中控制tvNotice的代码如下

                // 设置切入动画
                tvNotice.setInAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.slide_in_bottom));
                // 设置切出动画
                tvNotice.setOutAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.slide_out_up));
				//items是一个字符串列表,index就是动态的要显示的items中的索引
                tvNotice.setText(itmes.get(index).getTitle());

我们看到上面用到了两个动画效果,需要自己定义,如下

slide_in_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:shareInterpolator="false"    android:zAdjustment="top"
    >
    <translate
        android:duration="1000"
        android:fromYDelta="100%p"
        android:toYDelta="0" />
</set>


slide_out_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:shareInterpolator="false"    android:zAdjustment="top"
    >
    <translate
        android:duration="1000"
        android:fromYDelta="0"
        android:toYDelta="-100%p" />
</set>


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bdmh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值