XML中一个ViewFlipper
<?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" >
<ViewFlipper
android:id="@+id/viewFlipper1"
android:flipInterval="1000"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ViewFlipper>
</LinearLayout>
java代码
一个MyViewFlipper类
public class MyViewFlipper extends Activity{
private int[] imageIds = new int[]{
R.drawable.a,
R.drawable.a1,
R.drawable.a2,
R.drawable.a3,
R.drawable.a4,
R.drawable.a5,
R.drawable.a6,
R.drawable.a7,
R.drawable.a8,
R.drawable.a9,
};
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.myviewflipper);
ViewFlipper viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
//向ViewFlipper里面添加ImageView
for(int index=0;index<imageIds.length;index++){
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageIds[index]);
viewFlipper.addView(imageView);
}
//添加特效
//渐退
viewFlipper.setInAnimation(this,android.R.anim.fade_in);
viewFlipper.setOutAnimation(this,android.R.anim.fade_out);
//自左向右
viewFlipper.setInAnimation(AnimationUtils.makeInAnimation(this,true));
viewFlipper.setOutAnimation(AnimationUtils.makeInAnimation(this,true));
//自动切换图片
viewFlipper.startFlipping();
}
}
<?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" >
<ViewFlipper
android:id="@+id/viewFlipper1"
android:flipInterval="1000"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ViewFlipper>
</LinearLayout>
java代码
一个MyViewFlipper类
public class MyViewFlipper extends Activity{
private int[] imageIds = new int[]{
R.drawable.a,
R.drawable.a1,
R.drawable.a2,
R.drawable.a3,
R.drawable.a4,
R.drawable.a5,
R.drawable.a6,
R.drawable.a7,
R.drawable.a8,
R.drawable.a9,
};
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.myviewflipper);
ViewFlipper viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
//向ViewFlipper里面添加ImageView
for(int index=0;index<imageIds.length;index++){
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageIds[index]);
viewFlipper.addView(imageView);
}
//添加特效
//渐退
viewFlipper.setInAnimation(this,android.R.anim.fade_in);
viewFlipper.setOutAnimation(this,android.R.anim.fade_out);
//自左向右
viewFlipper.setInAnimation(AnimationUtils.makeInAnimation(this,true));
viewFlipper.setOutAnimation(AnimationUtils.makeInAnimation(this,true));
//自动切换图片
viewFlipper.startFlipping();
}
}