TextSwitcher(仿京东快报)

动画:

top.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>

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%"
        android:toYDelta="0" />


</set>

布局:
<TextSwitcher
    android:id="@+id/frag1_adapter1_tsw"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:inAnimation="@anim/bottom"
    android:outAnimation="@anim/top" />

这是在适配器里的代码:
private TextView textView;
private String[] strs;
private int index = 0;

//京东快报
// 要显示的文本
strs = new String[]{"双11回馈活动产品利率增长0.05%", "国家大数据发展纲要", "郑重公告", "某某网站会员须知", "网站维护公告"};

holder.getTsw().setFactory(new ViewSwitcher.ViewFactory() {
    @Override
    public View makeView() {
        //new 一个TextView
        textView = new TextView(context);
        textView.setTextSize(20);
        textView.setText("京东双11优惠活动!!!");
        return textView;
    }
});

//new一个Handler
final android.os.Handler handler = new android.os.Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case 0:
                index++;
                holder.getTsw().setText(strs[index % strs.length]);
                if (index == strs.length) {
                    index = 0;
                }
                break;
        }
    }
};
//new一个线程
new Thread(new Runnable() {
    @Override
    public void run() {
        while (index < strs.length) {
            synchronized (this) {
                SystemClock.sleep(5000);//每隔4秒滚动一次
                handler.sendEmptyMessage(0);
            }
        }
    }
}).start();



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值