textswitcher
1,布局中使用
<TextSwitcher
android:id="@+id/switcher"
android:layout_width="match_parent"
//控制进入动画 根据需求自顶更改
android:inAnimation="@anim/enter_animation"
//控制退出的动画
android:outAnimation="@anim/outer_animation"
android:background="@color/cardview_dark_background"
android:layout_height="wrap_content">
</TextSwitcher>
//动画 enter_animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fromYDelta="100%p"
android:interpolator="@android:anim/accelerate_interpolator"
android:toYDelta="0%p" />
</set>
//退出动画 outer_animation
public class MainActivity extends FragmentActivity {
int index;
String [] str={"fahkfahkfha","窗前明月光"};
private Handler mHandler =new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
index++;
mTextSwitcher.setText(str[index%2]);
mHandler.sendEmptyMessageDelayed(0,2000);
}
};
private TextSwitcher mTextSwitcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextSwitcher = (TextSwitcher) findViewById(R.id.switcher);
mTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView textView=new TextView(MainActivity.this);
FrameLayout.LayoutParams layoutParams= new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textView.setPadding(0,40,0,40);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
textView.setLayoutParams(layoutParams);
return textView;
}
});
mTextSwitcher.setText("hahahha");
mHandler.sendEmptyMessageDelayed(0,2000);
}