前言:最近在Android开发学习中需要用到多行垂直滚动,做好后现将它整理出来。
概述:我这主要是实现两行滚动,如果字符超过两行则滚动,如果少于或等于两行则不滚动,点击按钮停止滚动。
效果图:
image
步骤:
1、新建自定义VerticalMarqueeView java文件。
public class VerticalMarqueeView extends ViewFlipper {
/**
* Alpha animation enable
*/
private boolean isSetAlphaAnim = true;
/**
* Animation interval
*/
private int interval = 5000;
/**
* Animation duration
*/
private int animDuration = 2000;
public VerticalMarqueeView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MarqueeViewUp, defStyleAttr, 0);
isSetAlphaAnim = ta.getBoolean(R.styleable.MarqueeViewUp_isSetAlphaAnim, isSetAlphaAnim);
interval = ta.getInteger(R.styleable.MarqueeViewUp_interval, interval);
animDuration = ta.getInteger(R.styleable.MarqueeViewUp_animDuration, animDuration);
setFlipInterval(interval);
// In or out animation: alpha, continuous
AlphaAnimation animationIn = new AlphaAnimation(0, 1);
animationIn.setDuration(animDuration);
AlphaAnimation animationOut = new AlphaAnimation(1, 0);
animationOut.setDuration(animDuration);
// TranslateAnimation: show
TranslateAnimation translateAnimationIn = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELAT