1. 程式人生 > >Android-垂直上下滾動的TextView

Android-垂直上下滾動的TextView

相關類的繼承關係:

public class View implements Drawable.Callback, KeyEvent.Callback,
        AccessibilityEventSource {...}


public abstract class ViewGroup extends View implements ViewParent, ViewManager {...}


public class FrameLayout extends ViewGroup {...}


public class ViewAnimator extends FrameLayout
{
...} public class ViewSwitcher extends ViewAnimator {...} public class TextSwitcher extends ViewSwitcher {...}

1、自定義view:

package com.example.lenovo.myapplication.textviewtop;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import
android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.TextSwitcher; import android.widget.TextView; import
android.widget.ViewSwitcher; import com.example.lenovo.myapplication.R; import java.util.ArrayList; import java.util.Timer; import java.util.TimerTask; /** * Created by lenovo on 2017/7/5. */ public class VerticalTextview extends TextSwitcher implements ViewSwitcher.ViewFactory { private static final int FLAG_START_AUTO_SCROLL = 0; private static final int FLAG_STOP_AUTO_SCROLL = 1; private float mTextSize = 16; private int mPadding = 5; private int textColor = Color.BLACK; /** * @param textSize 字號 * @param padding 內邊距 * @param textColor 字型顏色 */ public void setText(float textSize, int padding, int textColor) { mTextSize = textSize; mPadding = padding; this.textColor = textColor; } private OnItemClickListener itemClickListener; private Context mContext; private int currentId = -1; private ArrayList<String> textList; private Handler handler; public VerticalTextview(Context context) { this(context, null); mContext = context; } public VerticalTextview(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; textList = new ArrayList<String>(); } public void setAnimTime(long animDuration) { setFactory(this); Animation in = new TranslateAnimation(0, 0, animDuration, 0); in.setDuration(animDuration); in.setInterpolator(new AccelerateInterpolator()); Animation out = new TranslateAnimation(0, 0, 0, -animDuration); out.setDuration(animDuration); out.setInterpolator(new AccelerateInterpolator()); setInAnimation(in); setOutAnimation(out); } /** * 間隔時間 * * @param time */ public void setTextStillTime(final long time) { handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case FLAG_START_AUTO_SCROLL: if (textList.size() > 0) { currentId++; setText(textList.get(currentId % textList.size())); } handler.sendEmptyMessageDelayed(FLAG_START_AUTO_SCROLL, time); break; case FLAG_STOP_AUTO_SCROLL: handler.removeMessages(FLAG_START_AUTO_SCROLL); break; } } }; } /** * 設定資料來源 * * @param titles */ public void setTextList(ArrayList<String> titles) { textList.clear(); textList.addAll(titles); currentId = -1; } /** * 開始滾動 */ public void startAutoScroll() { handler.sendEmptyMessage(FLAG_START_AUTO_SCROLL); } /** * 停止滾動 */ public void stopAutoScroll() { handler.sendEmptyMessage(FLAG_STOP_AUTO_SCROLL); } @Override public View makeView() { TextView t = new TextView(mContext); t.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); t.setMaxLines(1); t.setPadding(mPadding, mPadding, mPadding, mPadding); t.setTextColor(textColor); t.setTextSize(mTextSize); t.setClickable(true); t.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (itemClickListener != null && textList.size() > 0 && currentId != -1) { itemClickListener.onItemClick(currentId % textList.size()); } } }); return t; } /** * 設定點選事件監聽 * * @param itemClickListener */ public void setOnItemClickListener(OnItemClickListener itemClickListener) { this.itemClickListener = itemClickListener; } /** * 輪播文字點選監聽器 */ public interface OnItemClickListener { /** * 點選回撥 * * @param position 當前點選ID */ void onItemClick(int position); } }

2、佈局:

   <com.example.lenovo.myapplication.textviewtop.VerticalTextview
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

3、使用:

package com.example.lenovo.myapplication.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.example.lenovo.myapplication.R;
import com.example.lenovo.myapplication.textviewtop.VerticalTextview;
import com.squareup.leakcanary.RefWatcher;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

/**
 * Created by lenovo on 2017/7/4.
 */

public class MainActivityII extends AppCompatActivity {

    private VerticalTextview TextView;
    private ArrayList<String> titleList = new ArrayList<String>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        init();
    }

    private void init() {
        TextView = (VerticalTextview) findViewById(R.id.text);
        titleList.add("你是天上最受寵的一架鋼琴");
        titleList.add("我是醜人臉上的鼻涕");
        titleList.add("你發出完美的聲音");
        titleList.add("我被默默揩去");
        titleList.add("你冷酷外表下藏著詩情畫意");
        titleList.add("我已經夠胖還吃東西");
        titleList.add("你踏著七彩祥雲離去");
        titleList.add("我被留在這裡");
        TextView.setTextList(titleList);
        TextView.setText(26, 5, Color.RED);//設定屬性
        TextView.setTextStillTime(3000);//設定停留時長間隔
        TextView.setAnimTime(300);//設定進入和退出的時間間隔
        TextView.setOnItemClickListener(new VerticalTextview.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(MainActivityII.this, "點選了 : " + titleList.get(position), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        TextView.startAutoScroll();
    }

    @Override
    protected void onPause() {
        super.onPause();
        TextView.stopAutoScroll();
    }

}