通知公告TextSwitcher自动上下滚动带点击事件

    今天项目中用到这样一个小功能,找了好些都不是很合适,参考了很多资料然后根据项目需求终于在下班前不辱使命。马上下班了,就不墨迹了。直接上代码,很简单稍微有点功底的朋友都能看懂

效果图:


通知公告TextSwitcher自动上下滚动带点击事件_第1张图片

 首先是anim下面有两个文件 

push_up_in.xml
 
  
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="@android:integer/config_mediumAnimTime"
        android:fromYDelta="100%"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="0" />
    <alpha
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />


set>
还有一个push_up_out.xml
 
  

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>
然后ManiActivity的布局文件中
<TextSwitcher
    android:id="@+id/tv_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inAnimation="@anim/push_up_in"
    android:outAnimation="@anim/push_up_out"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true">

TextSwitcher>
然后下来是 MainActivity了
 
  
package text.my.mytextswitcher;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity {
    private TextSwitcher textSwitcher;
    private String[] news={"双11回馈活动产品利率增长0.05%","国家大数据发展纲要","郑重公告","某某网站会员须知","网站维护公告"};
    private int index = 0;//textview上下滚动下标
    public static final int NEWS_MESSAGE_TEXTVIEW = 100;//通知公告信息



    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case NEWS_MESSAGE_TEXTVIEW:
                    index++;
                    textSwitcher.setText(news[index%news.length]);
                    if (index == news.length) {
                        index = 0;
                    }
                    break;
                default:
                    break;
            }
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textSwitcher = (TextSwitcher) findViewById(R.id.tv_message);
        newsMessage();
        textSwitcher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (index%news.length){
                    case 0:
                        Toast.makeText(MainActivity.this,news[0],Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Toast.makeText(MainActivity.this,news[1],Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(MainActivity.this,news[2],Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        Toast.makeText(MainActivity.this,news[3],Toast.LENGTH_SHORT).show();
                        break;
                    case 4:
                        Toast.makeText(MainActivity.this,news[4],Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    private void newsMessage() {
        textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                TextView textView = new TextView(MainActivity.this);
                textView.setSingleLine();
                textView.setTextSize(22);//字号
                textView.setTextColor(Color.parseColor("#ff3333"));
                textView.setEllipsize(TextUtils.TruncateAt.END);
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.CENTER_VERTICAL;
                textView.setLayoutParams(params);
                return textView;
            }
        });

        new Thread(){
            @Override
            public void run() {
                while (index

到此大功告成~~~!!!好了 不说了,收拾收拾准备下班咯


你可能感兴趣的:(实用小功能)