TextSwitcher:仿京东、淘宝滚动小广播,实现文字轮播滚动

用TextSwitche实现滚动式小广播是项目中比较常见的做法,首先看效果图:

创建两个anim,用于控制文字进出的方式:
in_swithcer.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="800"
        android:fromYDelta="100%p"
        android:toYDelta="0%p" />
</set>  
out_switcher.xml:
?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="800"
        android:fromYDelta="0%p"
        android:toYDelta="-100%p" />
</set>
然后是mainactivity布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#e3e3e3"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_centerInParent="true"
        android:background="#ffffff"
        android:layout_height="50dp"
  >

        <ImageView
            android:id="@+id/horn"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:background="@mipmap/horn" />

        <TextSwitcher
            android:id="@+id/textSwitcher"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_centerInParent="true"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:foregroundGravity="center"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/horn"
            android:inAnimation="@anim/in_switcher"
            android:outAnimation="@anim/out_switcher"
            android:textAlignment="center"/>

    </RelativeLayout>
</RelativeLayout>
下面贴出MainActivity 全部代码:
public class MainActivity extends AppCompatActivity {
    /*展示滚动字幕的控件*/
    private TextSwitcher mTextSwitcher;
    private int count;
    /*滚动字幕*/
    private String[] text = {"尾号5449,成功借款3000元",
            "尾号2346,成功借款4000元",
            "尾号4578,成功借款2000元",
            "尾号9587,成功借款5000元",
            "尾号7512,成功借款7000元",
            "尾号1258,成功借款5000元",
            "尾号3597,成功借款8000元",
            "尾号7568,成功借款3000元",
            "尾号2135,成功借款2000元",
            "尾号5449,成功借款3000元"
    };
    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initEvents();

    }

    private void initEvents() {
        setTextSwitcher();
        handler.post(runnable);
    }
    private void setTextSwitcher() {
        mTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                TextView textView = new TextView(MainActivity.this);
                textView.setTextSize(20);
                textView.setTextColor(Color.BLACK);
                return textView;
            }
        });

    }
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (count == 3 * text.length) {
                count = 0;
            }
            /*取出数组里的数据*/
            String str = text[count++ % text.length];
            /*定义一个SpannableString对象
            用于设置TextView中不同字体的颜色
            * */
            SpannableString span = new SpannableString(str);
            span.setSpan(new ForegroundColorSpan(Color.RED), 11, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mTextSwitcher.setText(span);
            /**设置每隔两秒时间切换一次显示数据*/
            handler.postDelayed(runnable, 2000);
        }
    };

    private void initViews() {
        mTextSwitcher = findViewById(R.id.textSwitcher);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        /**
         * 当activity销毁的时候
         * 销毁线程
         * 销毁handler
         * */
        handler.removeCallbacks(runnable);
    }
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值