一、结构
public class ViewAnimator extends FrameLayout android.view.ViewGroup android.view.view java.lang.object
(viewanimaor不常用,常用其直接子类ViewFlipper, ViewSwitcher或间接子类ImageSwitcher, TextSwitcher)
一、ViewFlipper,它可以在任意视图和视图组合之间切换。在学习之前我们有必要看一下 ViewAnimator 对象。 针对是layout
main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<ViewAnimator
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/va"
>
<include
layout="@layout/layout1"/>
<include
layout="@layout/layout2"/>
</ViewAnimator>
</LinearLayout>
layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="layout1"
/>
</LinearLayout>
layout2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="layout2"
/>
</LinearLayout>
布局文件也可写成
LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ViewFlipper android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:persistentDrawingCache="animation"
android:flipInterval="1000"
android:inAnimation="@anim/push_left_in"
android:outAnimation="@anim/push_right_out"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Next"
android:id="@+id/Button_next1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<ImageView
android:id="@+id/image1"
android:src="@drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Next"
android:id="@+id/Button_next2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<ImageView
android:id="@+id/image2"
android:src="@drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Next"
android:id="@+id/Button_next3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<ImageView
android:id="@+id/image3"
android:src="@drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
推荐使用第一种方式
main.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewAnimator va=(ViewAnimator) findViewById(R.id.va);
va.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
va.showNext();
}
});
}
二、textswithcer
1.得到 TextSwitcher 实例对象
TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
2.为switcher指定ViewFactory工厂(接口需实现makeview方法),该工厂会产生出转换时需要的View
switcher.setFactory(this);
3.为switcher设定显示的内容,该方法执行,就会切换到下个View
switcher.setText(String.valueOf(new Random().nextInt()));
main.java
package com.example.textswitcher;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity implements ViewFactory {
TextSwitcher ts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ts=(TextSwitcher)findViewById(R.id.ts);
ts.setFactory(this);
ts.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ts.setText(String.valueOf(new Random().nextInt(100)));
}
});
}
@Override
public View makeView() {
// TODO Auto-generated method stub
return new TextView(this);
}
}
main.xml
<LinearLayout 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"
tools:context=".MainActivity" >
<TextSwitcher
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ts"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right"
>
</TextSwitcher>
</LinearLayout>
三、imageswithcer 与textswitcher
main.java
public class MainActivity extends Activity implements ViewFactory{
ImageSwitcher is;
Integer[] da=new Integer[]{R.drawable.aa,R.drawable.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
is=(ImageSwitcher) findViewById(R.id.is);
Button b=(Button) findViewById(R.id.b);
is.setFactory(this);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int i=new Random().nextInt(2);
// is.setBackgroundResource(da[i]); 全黑了
is.setImageResource(da[i]);
}
});
}
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView i=new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return i;
}
main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
android:id="@+id/b"
/>
<ImageSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/is"
/>
</LinearLayout>