通过TextSwitcher实现上下滚动的广告动画效果

本文介绍了一个使用TextSwitcher实现文字切换效果的例子。通过XML布局定义、代码实现及自定义动画,实现了两个字符串之间的平滑过渡效果。具体包括TextSwitcher组件配置、动画文件设置、线程控制及Handler消息传递机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1.布局xml

<TextSwitcher
            android:id="@+id/profileSwitcher"
            android:layout_below="@id/ll_path_container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/s2"
            android:background="@drawable/map_top_bg_shape"
            android:inAnimation="@anim/push_up_in"
            android:minHeight="48dp"
            android:outAnimation="@anim/push_up_out"></TextSwitcher>

2.代码

//===========================================================
    @InjectView(R.id.profileSwitcher)
    TextSwitcher textSwitcher;
    private BitHandler bitHandler;
    private String[] strings={"text00001","text00002"};
    private int index = 0;
    private void test(){
        textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                TextView textView = new TextView(mContext);
                textView.setSingleLine();
                textView.setTextSize(15);
                textView.setTextColor(Color.parseColor("#ff0000"));
                textView.setEllipsize(TextUtils.TruncateAt.END);
                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
                );
                lp.gravity = Gravity.CENTER;
                textView.setLayoutParams(lp);
                return textView;
            }
        });
        bitHandler = new BitHandler();
        new myThread().start();
    }

    class BitHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            textSwitcher.setText(strings[index]);
            index++;
            if (index == strings.length) {
                index = 0;
            }
        }
    }

    private class myThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (index < strings.length) {
                try {
                    synchronized (this) {
                        bitHandler.sendEmptyMessage(0);
                        this.sleep(2000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

3.anim

push_up_in

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false"
    android:zAdjustment="top">
    <translate
        android:duration="400"
        android:fromYDelta="100%"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="0"

        />

    <alpha
        android:duration="400"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>

push_up_out

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false"
    android:zAdjustment="bottom">
    <translate
        android:duration="400"
        android:fromYDelta="0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="-100%"

        />

    <alpha
        android:duration="400"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>

实现效果:





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值