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.util.TypedValue;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;

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 textList;    private Handler handler;    private boolean isBegin = false;    public VerticalTextView(Context context) {        this(context, null);    }    public VerticalTextView(Context context, AttributeSet attrs) {        super(context, attrs);        mContext = context;        textList = new ArrayList();    }    public void setAnimTime(long animDuration) {        setFactory(this);        Animation in = new TranslateAnimation(0, 0, 70, 0);        in.setDuration(animDuration);        in.setInterpolator(new AccelerateInterpolator());        Animation out = new TranslateAnimation(0, 0, 0, -70);        out.setDuration(animDuration);        out.setInterpolator(new AccelerateInterpolator());        setInAnimation(in);        setOutAnimation(out);    }    /**     * 间隔时间     */    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 titles) {        textList.clear();        textList.addAll(titles);        currentId = -1;    }
//开始滚动
public void startAutoScroll() { if ( isBegin) { return; } isBegin = true; handler.sendEmptyMessage( FLAG_START_AUTO_SCROLL); }
//停止滚动
public void stopAutoScroll() { if (! isBegin){ return; } isBegin = false; 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, 0, mPadding, 0); t.setTextColor( textColor); t.setEllipsize(TextUtils.TruncateAt. END); t.setTextSize(TypedValue. COMPLEX_UNIT_SP, 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; } //点击事件监听 public void setOnItemClickListener(OnItemClickListener itemClickListener) { this. itemClickListener = itemClickListener; } // 轮播文本点击监听器,需要实现它 public interface OnItemClickListener { // 点击回调 void onItemClick( int position); }}

更多相关文章

  1. Android:为控件绑定监听器
  2. Android笔记二十三.Android基于事件监听器处理机制
  3. Android疑惑记录-----在Adapter中配置按钮监听器时,列表项内容获
  4. Android中MediaButtonReceiver广播监听器的机制分析
  5. Android 动画监听器
  6. 使用xml作为数据源,配合asp:Menu类自由扩展菜单项
  7. Web设计中如何使用XML数据源对象详细介绍
  8. 气象XML数据源应用程序的详细解析
  9. 芋道 Spring Boot 多数据源(读写分离)入门

随机推荐

  1. Android外文翻译系列 - Android应用程序应
  2. Android存储-SQLite数据库存储数据(三)
  3. Android入门
  4. 精灵游戏实现
  5. Android中view重绘问题
  6. [Android]笔记19:RatingBar的功能与用法
  7. Android NDK学习笔记
  8. Android实现自动填充验证码
  9. Android修改主机名和IP地址问题
  10. Android(安卓)pulltorefresh上拉下拉刷新