1. 程式人生 > >TextView垂直方向無限滾動還有跑馬燈效果

TextView垂直方向無限滾動還有跑馬燈效果


用個自定義控制元件 然後顯示上去就可以啦
自定義控制元件

package com.example.textviewscrollv;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.ViewSwitcher;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;


/**
 * Name: TextSwitcherView
 * Author: liuan
 * creatTime:2016-12-26 16:46
 */

public class TextSwitcherView extends TextSwitcher implements ViewSwitcher.ViewFactory {
    private static final int UPDATA_TEXTSWITCHER = 1;
    private Context mContext;
    private int index = 0;
    private ArrayList<String> mReArrayList = new ArrayList<>();
    private TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            Message message = new Message();
            message.what = UPDATA_TEXTSWITCHER;
            handler.sendMessage(message);
        }
    };
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case UPDATA_TEXTSWITCHER:
                    updateTextSwitcher();
                    break;
            }
        }
    };


    public void getResource(ArrayList<String> reArrayList) {
        this.mReArrayList = reArrayList;
    }

    private void updateTextSwitcher() {
        if (mReArrayList != null && mReArrayList.size() > 0) {

            this.setText(mReArrayList.get(index++));
            //實現歸零
            if (index > mReArrayList.size() - 1) {
                index = 0;
            }
        }
    }

    public TextSwitcherView(Context context) {
        super(context, null);
    }

    public TextSwitcherView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    private void init() {
        this.setFactory(this);
        this.setInAnimation(getContext(), R.anim.vcertical_in);
        this.setOutAnimation(getContext(), R.anim.vcertical_out);
        Timer timer = new Timer();
        timer.schedule(timerTask, 1, 6000);
        mReArrayList.add("一段文字");
        mReArrayList.add("一段簡短文字");

        mReArrayList.add("一段很長很長很長很長很長很長很長很長很長的文字");

    }


    @Override
    public View makeView() {
        //如果想實現跑馬燈 可以這麼做 不然直接用TextView也是沒有關係的
        //設定自己的TextView樣式 繼承TextView 重寫isFoused方法為true
        WaterTextView tv = new WaterTextView(getContext());
/*        Android:ellipsize = "end"    省略號在結尾
        android:ellipsize = "start"   省略號在開頭
        android:ellipsize = "middle"     省略號在中間
        android:ellipsize = "marquee"  跑馬燈
        最好加一個約束android:singleline = "true"*/
        tv.setSingleLine(true);
        tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        //設定重複次數 -1則無線迴圈
        tv.setMarqueeRepeatLimit(1);
        tv.setTextColor(Color.parseColor("#e2658f"));
        tv.setBackgroundColor(Color.parseColor("#abaaaa"));
        tv.setTextSize(24);
        tv.setPadding(20,15,20,15);

        return tv;
    }

}

繼承TextView 重寫isFocuesd方法返回True

package com.example.textviewscrollv;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Name: WaterTextView
 * Author: liuan
 * creatTime:2016-12-26 17:13
 */
public class WaterTextView extends TextView {
    public WaterTextView(Context context) {
        super(context);
    }

    public WaterTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }



    public WaterTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}


in動畫檔案
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="100%p"
        android:toYDelta="0%p"
        android:duration="1000"
       />

</set>


out動畫檔案
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0%p"
        android:toYDelta="-100%p"
        android:duration="1000"
        />
</set>


佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="com.example.textviewscrollv.MainActivity">

    <com.example.textviewscrollv.TextSwitcherView
        android:id="@+id/tv"
        android:layout_width="500dp"
        android:layout_height="wrap_content" />
</RelativeLayout>


核心程式碼
// 找到佈局~~~~~也可以把設定的邏輯放到這裡 tv.set

package com.example.textviewscrollv;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextSwitcherView tv = (TextSwitcherView) findViewById(R.id.tv);
    }
}