主要用的是android自带的TextSwitcher,很好用,结合着动画效果。
首先是布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:background="#aaafff"
android:gravity="center"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="40dp">
<ImageView
android:src="@mipmap/home_sign"
android:id="@+id/imag"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextSwitcher
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/imag"
android:id="@+id/tv_switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextSwitcher>
</RelativeLayout>
</LinearLayout>
然后是activity
package com.example.myapplication.custView;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import com.example.myapplication.R;
import java.util.ArrayList;
/**
* Created by pw on 2019/4/2 13:46
* E-Mail Address: pw163.com
*/
public class Custom_Activity extends AppCompatActivity {
private TextSwitcher tv_switcher;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cus);
initView();
}
private void initView() {
tv_switcher = findViewById(R.id.tv_switcher);
tv_switcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
return new TextView(Custom_Activity.this);
}
});
ArrayList<String> alist = new ArrayList<>();
alist.clear();
for (int i = 0; i < 10; i++) {
alist.add("我是"+i);
}
new TextSwitcherAnimation(tv_switcher,alist).create();
}
}
然后是工具类(实现动画效果)
package com.example.myapplication.custView;
import android.os.Handler;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.TextSwitcher;
import java.util.List;
/**
* Created by pw on 2019/4/2 14:08
* E-Mail Address: pw163.com
*/
public class TextSwitcherAnimation {
private static final int DURATION = 1000;
private TextSwitcher textSwitcher;
private List<String> texts;
private int marker;
private AnimationSet InAnimationSet;
private AnimationSet OutAnimationSet;
private int delayTime = 2000;
private Handler handler = new Handler();
private Runnable task = new Runnable() {
@Override
public void run() {
nextView();
handler.postDelayed(task, delayTime * 2);
}
};
public TextSwitcherAnimation(TextSwitcher textSwitcher, List<String> texts) {
this.textSwitcher = textSwitcher;
this.texts = texts;
}
public void start() {
stop();
handler.postDelayed(task, delayTime);
}
public void stop(){
handler.removeCallbacks(task);
}
public int getMarker() {
return marker;
}
public TextSwitcherAnimation setTexts(List<String> texts) {
this.texts = texts;
return this;
}
public void setDelayTime(int delayTime) {
this.delayTime = delayTime;
}
public void create() {
marker = 0;
if (texts == null){
Log.w("TextSwitcherAnimation", "texts is null");
return;
}
if (textSwitcher == null) {
Log.w("TextSwitcherAnimation", "textSwitcher is null");
return;
}
textSwitcher.setText(texts.get(0));
createAnimation();
textSwitcher.setInAnimation(InAnimationSet);
textSwitcher.setOutAnimation(OutAnimationSet);
start();
}
private void createAnimation() {
AlphaAnimation alphaAnimation;
TranslateAnimation translateAnimation;
int h = textSwitcher.getHeight();
if (h <= 0) {
textSwitcher.measure(0,0);
h = textSwitcher.getMeasuredHeight();
}
InAnimationSet = new AnimationSet(true);
OutAnimationSet = new AnimationSet(true);
alphaAnimation = new AlphaAnimation(0,1);
translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, h, Animation.ABSOLUTE, 0);
InAnimationSet.addAnimation(alphaAnimation);
InAnimationSet.addAnimation(translateAnimation);
InAnimationSet.setDuration(DURATION);
alphaAnimation = new AlphaAnimation(1,0);
translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -h);
OutAnimationSet.addAnimation(alphaAnimation);
OutAnimationSet.addAnimation(translateAnimation);
OutAnimationSet.setDuration(DURATION);
}
private void nextView() {
marker = ++marker % texts.size();
textSwitcher.setText(texts.get(marker));
}
}