废话区:每次写动画,都是奇葩的需求。这次是一个公告的动画,按理说就是一条条的往上走呗,但是,我们的公告“长”着呢,屏幕展示不完,咋办,要先往左滚动,展示完了这一条信息之后再往上滚动。
需求:一条信息,如果超出屏幕,就要是跑马灯的效果,这个直接上原生的,没有超出不滚动。对于多条信息,如果超出屏幕,就要先往左移动,直到展示完毕,在往上面移动。
这边加上一个代码里面获取焦点的代码,这个页面已经非常复杂了,然后你无法获取到焦点,那这个跑马灯的效果是实现不了的,我这边就是在代码里面手动加上获取焦点的代码:
tv.setText(mNoticeBeanList.get(0).getTitle());
tv.setFocusable(true);
tv.setFocusableInTouchMode(true);
tv.requestFocus();
package com.example.retrofit.studyretrofit;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;
/**
* Created by on 2017/3/16.
*/
public class MyNoticeLayout extends LinearLayout {
private ViewFlipper viewSwitcher;
private int indexSel = 0;
private Display mDisplay;
private WindowManager mWindowManager;
private String[] tags;
private int mMW;
private TextView mTv;
private Context mContext;
private TextView mStudy_tv;
public MyNoticeLayout(@NonNull Context context) {
this(context,null);
}
public MyNoticeLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
initView(mContext);
}
private void initView(Context context) {
mTv = (TextView) findViewById(R.id.tv);
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mDisplay = mWindowManager.getDefaultDisplay();
viewSwitcher = (ViewFlipper) findViewById(R.id.viewswitcher);
tags = new String[3];
tags[0] = "公告信息:xxx门店开业,满5000元减500元,热烈欢迎welcome to hahahha";
tags[1] = "公告信息:xxx门店开业,满5000元减500元";
tags[2] = "公告信息:xxx门店开业,满5000元减500元,热烈欢迎1111222333";
mTv.setVisibility(tags.length > 1 ? View.GONE: View.VISIBLE);
viewSwitcher.setVisibility(tags.length > 1 ? View.VISIBLE : View.GONE);
if(tags.length > 1){
for(int i= 0; i<tags.length; i++){
View view = View.inflate(mContext, R.layout.item_viewswitch,null);
viewSwitcher.addView(view);
}
limitTiem();
}else{
mTv.setText(tags[0]);
}
}
private void limitTiem() {
//设置切入动画
TranslateAnimation mAnimationTop = new TranslateAnimation(0,0,200,0);
mAnimationTop.setDuration(2000);
mAnimationTop.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
width();
ObjectAnimator objectAnimatorTranslate = ObjectAnimator.ofFloat(mStudy_tv, "translationX", -mMW, 0);
objectAnimatorTranslate.setDuration(1);
objectAnimatorTranslate.start();
Message message = Message.obtain();
message.what = 1;
mHandler.sendMessageDelayed(message, 3000);
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
//设置切出动画
TranslateAnimation mAnimationBottom = new TranslateAnimation(0,0,0,-200);
mAnimationBottom.setDuration(2000);
viewSwitcher.setInAnimation(mAnimationTop);
viewSwitcher.setOutAnimation(mAnimationBottom);
width();
if(mMW<0){
animation();
}else{
Message message = Message.obtain();
message.what = 2;
mHandler.sendMessageDelayed(message, 2000);
}
}
public void width(){
mStudy_tv = (TextView)viewSwitcher.getCurrentView().findViewById(R.id.viewswitcher_tv_one);
String tag=tags[indexSel%tags.length];
mStudy_tv.setText(tag);
TextPaint paint = mStudy_tv.getPaint();
int width = computeMaxStringWidth(tag, paint);
mMW = mDisplay.getWidth()- width - UICommonUtil.dip2px(mContext, 20);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(UICommonUtil.dip2px(mContext,width+200),
UICommonUtil.dip2px(mContext, 40));
mStudy_tv.setLayoutParams(layoutParams);
}
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(1 == msg.what){
if(mMW<0){
animation();
}else{
viewSwitcher.showNext();
indexSel = indexSel+1;
}
}else if(2 == msg.what){
viewSwitcher.showNext();
indexSel = indexSel+1;
}
}
};
/**
* 当公告信息超出屏幕的时候,需要先往左移动
*/
public void animation(){
TextView mStudy_tv = (TextView)viewSwitcher.getCurrentView().findViewById(R.id.viewswitcher_tv_one);
//超出屏幕向左移动
ObjectAnimator objectAnimatorTranslate = ObjectAnimator.ofFloat(mStudy_tv, "translationX", 0, mMW);
objectAnimatorTranslate.setDuration(2000);
objectAnimatorTranslate.start();
objectAnimatorTranslate.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
viewSwitcher.showNext();
indexSel = indexSel+1;
}
});
}
/**
* 计算textview的宽度
* @param strings
* @param p 这个paint 必须是 textview.getPaint(), 不能自己new出来,才能保证准确性
* @return
*/
private int computeMaxStringWidth(String strings, Paint p) {
float maxWidthF = 0.0f;
float width = p.measureText(strings);
maxWidthF = Math.max(width, maxWidthF);
int maxWidth = (int) (maxWidthF + 0.5);
return maxWidth;
}
}
<?xml version="1.0" encoding="utf-8"?>
<com.example.retrofit.studyretrofit.MyNoticeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
/>
<ViewFlipper
android:id="@+id/viewswitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"/>
</com.example.retrofit.studyretrofit.MyNoticeLayout>
布局:item_viewswitch
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewswitcher_ll"
android:layout_width="wrap_content"
android:layout_height="40dp">
<TextView
android:id="@+id/viewswitcher_tv_one"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="14sp"
android:gravity="center_vertical"
/>
</LinearLayout>
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include
android:id="@+id/notice_layout"
layout="@layout/notice_layout"/>
</LinearLayout>