TextSwitcher与ImageSwitcher实现界面转换平滑过渡
不废话 直接上xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.androidbase.two.TextAnimationActivity">
<Button
android:id="@+id/change"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="change" />
<TextSwitcher
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/change"
android:layout_centerInParent="true" />
</RelativeLayout>
textSwitcher = (TextSwitcher)findViewById(R.id.text);
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView tv =new TextView(TextAnimationActivity.this);
tv.setTextSize(16);
tv.setTextColor(Color.GREEN);
//这里会执行两次,创建两个view,注意:两次text内容必需一致。
tv.setText("文本变化前");
return tv;
}
});
textSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.text_in));
textSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.text_out));
然后就是触发内容的变换:
findViewById(R.id.change).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(idx%2==0)
textSwitcher.setText("文本变化后"+idx);
else
textSwitcher.setText("文字要变化"+idx);
idx++;
}
});