垂直滚动 和 水平滚动
创建一个工具类
CustomTextSwitcher
/**
*
* 公告新闻切换
*
* Wetchat : ljphhj
* Github : https://github.com/xiaoyaomeng
* Autor : lijiangping
*/
public class CustomTextSwitcher extends TextSwitcher implements ViewSwitcher.ViewFactory {
private Context mContext;
private String[] mData;
private final long DEFAULT_TIME_SWITCH_INTERVAL = 1000;//1s
private long mTimeInterval = DEFAULT_TIME_SWITCH_INTERVAL;
private int mCurrentIndex = 0;
public CustomTextSwitcher(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
setFactory(this);
}
public CustomTextSwitcher setInAnimation(int animationResId){
Animation animation = AnimationUtils.loadAnimation(this.mContext, animationResId);
setInAnimation(animation);
return this;
}
public CustomTextSwitcher setOutAnimation(int animationResId){
Animation animation = AnimationUtils.loadAnimation(this.mContext, animationResId);
setOutAnimation(animation);
return this;
}
/**
* 通知/公告数据绑定
* @param data
* @return
*/
public CustomTextSwitcher bindData(String[] data){
this.mData = data;
return this;
}
public void startSwitch(long timeInterval){
this.mTimeInterval = timeInterval;
if (mData != null && mData.length != 0) {
mSwitchHandler.sendEmptyMessage(0);
}else{
throw new RuntimeException("data is empty");
}
}
/**
* 工厂类中创建TextView供给TextSwitcher使用
* @return
*/
@Override
public View makeView() {
TextView view = new TextView(this.mContext);
return view;
}
private Handler mSwitchHandler = new Handler(){
@Override
public void dispatchMessage(Message msg) {
super.dispatchMessage(msg);
int index = mCurrentIndex % mData.length;
mCurrentIndex++;
setText(mData[index]);
sendEmptyMessageDelayed(0, mTimeInterval);
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:background="@drawable/ic_launcher_background"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:id="@+id/tvNotify1"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp"
android:textSize="14sp"
android:text="通知"
android:textColor="@android:color/white"
android:layout_width="40dp"
android:layout_height="25dp" />
<com.xxx.xxx.CustomTextSwitcher
android:id="@+id/tvDownUpTextSwitcher"
android:layout_marginLeft="5dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBaseline_toBaselineOf="@id/tvNotify1"
app:layout_constraintLeft_toRightOf="@id/tvNotify1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
app:layout_constraintTop_toBottomOf="@id/tvDownUpTextSwitcher"
app:layout_constraintLeft_toLeftOf="@id/tvNotify1"
android:layout_marginTop="50dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:background="@drawable/ic_launcher_background"
android:id="@+id/tvNotify2"
android:textSize="14sp"
android:text="通知"
android:textColor="@android:color/white"
android:layout_width="40dp"
android:layout_height="25dp" />
<com.xxx.xxx.CustomTextSwitcher
android:layout_marginLeft="5dp"
app:layout_constraintBaseline_toBaselineOf="@id/tvNotify2"
app:layout_constraintLeft_toRightOf="@id/tvNotify2"
android:layout_marginTop="30dp"
android:id="@+id/tvLeftRightTextSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</androidx.constraintlayout.widget.ConstraintLayout>
/**
* Wetchat : ljphhj
* Github : https://github.com/xiaoyaomeng
* Autor : lijiangping
*/
public class MainActivity extends AppCompatActivity {
CustomTextSwitcher tvDownUpTextSwitcher;
CustomTextSwitcher tvLeftRightTextSwitcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 数据源
String[] data = new String[]{
"解放军于近日在东南沿海等海空域组织演习。",
"北京市公布了生活垃圾分类治理三年行动计划。",
"17日凌晨我国上空将出现一次月偏食。",
"中国辛鑫夺得世锦赛公开水域游泳的金牌。",
};
// 上下滚动
tvDownUpTextSwitcher = findViewById(R.id.tvDownUpTextSwitcher);
tvDownUpTextSwitcher.setInAnimation(R.anim.animation_down_up_in_animation)
.setOutAnimation(R.anim.animation_down_up_out_animation)
.bindData(data)
.startSwitch(2000L);
// 左右滚动
tvLeftRightTextSwitcher = findViewById(R.id.tvLeftRightTextSwitcher);
tvLeftRightTextSwitcher.setInAnimation(R.anim.animation_left_right_in_animation)
.setOutAnimation(R.anim.animation_left_right_out_animation)
.bindData(data)
.startSwitch(2000L);
}
}
animation_down_up_in_animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:zAdjustment="top">
<translate
android:duration="400"
android:fromYDelta="100%"
android:interpolator="@android:anim/accelerate_interpolator"
android:toYDelta="0" />
<alpha
android:duration="400"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
animation_down_up_out_animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:zAdjustment="bottom">
<translate
android:duration="400"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toYDelta="-100%" />
<alpha
android:duration="400"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
animation_left_right_in_animation
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-50%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
animation_left_right_out_animation
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="50%p"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>