效果图
自定义view
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import com.panghu.R;
/**
*
* 公告新闻切换
*/
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);
}
};
}
二、MainActivity
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(1000L); // 左右滚动 tvLeftRightTextSwitcher = findViewById(R.id.tvLeftRightTextSwitcher); tvLeftRightTextSwitcher.setInAnimation(android.R.anim.slide_in_left) .setOutAnimation(android.R.anim.slide_out_right) .bindData(data) .startSwitch(1000L); } }
三、 布局文件
<?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/shape_rect_bg"
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="wrap_content"
android:layout_height="wrap_content" />
<com.panghu.view.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/shape_rect_bg"
android:id="@+id/tvNotify2"
android:textSize="14sp"
android:text="通知"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.panghu.view.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>
四、需要动画
①、animation_down_up_in_animation.xml
<?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
<?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.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> </set>
animation_left_right_out_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
</set>