android 自定义View开发实战(五) TextView滚动显示

来源:互联网 发布:h3c路由配置端口ip网关 编辑:程序博客网 时间:2024/05/16 11:18

1 前言

由于项目需求,需要显示如下的效果
这里写图片描述
查了下资料,可以使用TextSwitcher实现,废话不多说。直接上干货

2 定义attrs属性

对于这个滚动TextSwitchView,我们一般用得比较多的属性就是字体大小与颜色了。我们将它定义在attrs.xml文件中

    <!--TextSwitchView属性-->    <declare-styleable name="TextSwitchView">        <attr name="textSize" format="dimension"/>        <attr name="textColor" format="color"/>    </declare-styleable>

3 定义Translate Animation

我们想要达到的效果是上下滚动,因此,定义在anim下的动画如下:

in_animation.xml

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

可以看到,进入动画为:Y方向上的100%p到0%p(表示完全显示出来),那么相应的出去的动画为:
out_animation.xml

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

4 java 代码

/** * Created by qiyei2015 on 2016/11/27. * 1273482124@qq.com */public class TextSwitchView extends TextSwitcher implements ViewSwitcher.ViewFactory {    private final static String TAG = "TextSwitchView";    private Context mContext;    private Timer mTimer;           //定时器,用于定时执行动画,使TextView滚动播放    private int mIndex = -1;        //索引下标    private List<String> mResource; //字符串资源    private float mTextSize;          //字体大小    private int mTextColor;         //字体颜色    private final static int SCROLL = 1;    private Handler mHandler = new Handler(){        @Override        public void handleMessage(Message msg) {            switch (msg.what){                case SCROLL:                    updateText();                    break;                default:                    break;            }        }    };    public TextSwitchView(Context context){        this(context,null);    }    public TextSwitchView(Context context, AttributeSet attrs){        super(context,attrs);        init(context,attrs);    }    /**     * 初始化     * @param context     * @param attrs     */    private void init(Context context,AttributeSet attrs){        mContext = context;        if (mTimer == null){            mTimer = new Timer();        }        TypedArray array = mContext.obtainStyledAttributes(attrs,R.styleable.TextSwitchView);        mTextSize = array.getDimensionPixelSize(R.styleable.TextSwitchView_textSize,16);//默认16sp        mTextColor = array.getColor(R.styleable.TextSwitchView_textColor, Color.BLACK);//默认黑色        array.recycle();        //获取自定义属性应该在setFactory()之前        setFactory(this);        setInAnimation(AnimationUtils.loadAnimation(context, R.anim.in_animation));        setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.out_animation));    }    @Override    public View makeView() {        TextView textView = new TextView(mContext);        final float fontScale = getResources().getDisplayMetrics().scaledDensity;        Log.d(TAG,"mTextSize:" + (int)(mTextSize / fontScale + 0.5f));        textView.setTextSize((int)(mTextSize / fontScale + 0.5f));        textView.setTextColor(mTextColor);        return textView;    }    /**     * 设置资源     * @param res     */    public void setResource(List<String> res){        mResource = res;    }    /**     * 设置文字停留时间     * @param time     */    public void setTextStayTime(long time){        if (mTimer == null){            mTimer = new Timer();        }else {            mTimer.scheduleAtFixedRate(new TimerTask() {                @Override                public void run() {                    mHandler.sendEmptyMessage(SCROLL);                }            },0,time);        }    }    /**     * 更新Text     * @return     */    private void updateText(){        mIndex++;        if (mIndex > mResource.size() - 1){            mIndex = 0;        }        setText(mResource.get(mIndex));    }    /**     * 这里防止内存泄漏     */    @Override    protected void onDetachedFromWindow() {        mTimer.cancel();//取消定时任务        mHandler.removeMessages(SCROLL);//取消改Message的队列循环        super.onDetachedFromWindow();    }}

比较简单,就不再分析了

0 0