实现
- 兼容动画
compile 'com.nineoldandroids:library:2.4.0+' - 布局文件
<LinearLayout
android:id="@+id/lln"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_centerInParent="true"
android:layout_weight="5"
android:background="#ff82ab"
android:gravity="center_vertical"
android:orientation="vertical">
<neacy.upmarqueer.UpDownTextView
android:id="@+id/marquee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:singleLine="true"
android:text="·"
android:layout_marginTop="3dp"
android:textColor="@android:color/black" />
<neacy.upmarqueer.UpDownTextView
android:id="@+id/marquee1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:singleLine="true"
android:layout_marginTop="2dp"
android:text="·"
android:textColor="@android:color/black" />
</LinearLayout>
- UpDownTextView的具体实现
public class UpDownTextView extends TextView implements Animator.AnimatorListener {
private static final String TAG = "UpDownTextView";
private static final int ANIMATION_DURATION = 200;
private float height;
private AnimatorSet mAnimatorStartSet;
private AnimatorSet mAnimatorEndSet;
private String mText;
public UpDownTextView(Context context) {
super(context);
}
public UpDownTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 此方法就是获取view的高度
*
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
height = getHeight();
}
/**
* --- 初始化向上脱离屏幕的动画效果 ---
*/
private void initStartAnimation() {
ObjectAnimator translate = ObjectAnimator.ofFloat(this, "translationY", 0, -height);
ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", 1f, 0f);
mAnimatorStartSet = new AnimatorSet();
mAnimatorStartSet.play(translate).with(alpha);
mAnimatorStartSet.setDuration(ANIMATION_DURATION);
mAnimatorStartSet.addListener(this);
}
/**
* --- 初始化从屏幕下面向上的动画效果 ---
*/
private void initEndAnimation() {
//translationY 属性肯定要归为0,不然显示的内容就有偏移了;alpha 也是一样
ObjectAnimator translate = ObjectAnimator.ofFloat(this, "translationY", height, 0);
ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", 0f, 1f);
mAnimatorEndSet = new AnimatorSet();
mAnimatorEndSet.play(translate).with(alpha);
mAnimatorEndSet.setDuration(ANIMATION_DURATION);
mAnimatorStartSet.addListener(this);
}
/**
* --- 设置内容 ---
**/
public void setText(String text) {
if (TextUtils.isEmpty(text)) {
Log.e(TAG, "--- 请确保text不为空 ---");
return;
}
mText = text;
if (null == mAnimatorStartSet) {
initStartAnimation();
}
mAnimatorStartSet.start();
}
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
super.setText(mText);
if (null == mAnimatorEndSet) {
initEndAnimation();
}
mAnimatorEndSet.start();
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
}
说明:
- 这里用到了属性动画,应该依赖 nineoldandroids:library,以兼容3.0以下的机型。
- 对于 translationY 属性是表示内容的偏移。如果要显示内容,属性值肯定应该归为0 ,否则就会有偏移量。
项目地址
请戳我的github:UpMarqueerTextView-master