android控件18 ImageSwitcher

来源:互联网 发布:java开发的项目 编辑:程序博客网 时间:2024/05/17 04:40

1)/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><ImageSwitcher  android:id="@+id/imageswitch"    android:layout_width="wrap_content"     android:layout_height="wrap_content"     />    <Button android:id="@+id/button"    android:text="ImageSwitcher"android:layout_width="wrap_content"     android:layout_height="wrap_content"     /></LinearLayout>

2)com.sxt.ImageSwitcherActivity.java

package com.sxt;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;public class ImageSwitcherActivity extends Activity {    /** Called when the activity is first created. */int position = 0;private Integer [] imageIds ={R.drawable.p1 , R.drawable.p2, R.drawable.p3,R.drawable.p4 , R.drawable.p5, R.drawable.p6,};    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //final对象,引用不变。final基本类型,数据不变。         final ImageSwitcher imageSwitch = (ImageSwitcher)this.findViewById(R.id.imageswitch);        Button button = (Button)this.findViewById(R.id.button);                //显示视图,设置工厂类的makeView方法。        imageSwitch.setFactory(new ViewFactory() {@Overridepublic View makeView() {// TODO Auto-generated method stubreturn new ImageView(ImageSwitcherActivity.this);}});        imageSwitch.setImageResource(imageIds[position]);        //设置切入动画         imageSwitch.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));         //设置切出动画         imageSwitch.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));                button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(position < imageIds.length-1 ){position++;}else{position = 0;}imageSwitch.setImageResource(imageIds[position]);}});                  }}

3)如图



原创粉丝点击