你的位置:首页 > 操作系统

[操作系统]ImageSwitcher的简单使用


 

测试代码:

activity_main.

<??><LinearLayout ="http://schemas.android.com/apk/res/android"  android:id="@+id/MyLayout"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical" >  <ImageSwitcher    android:id="@+id/imageSwitcher"    android:layout_gravity="center"    android:layout_width="wrap_content"    android:layout_height="wrap_content" />  <LinearLayout    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:orientation="horizontal" >    <Button      android:id="@+id/btnPrevious"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_weight="1"      android:enabled="false"      android:text="上一张" />    <Button      android:id="@+id/btnNext"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_weight="1"      android:enabled="true"      android:text="下一张" />  </LinearLayout></LinearLayout>

 

MainActivity.java:

package com.example.zz;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;public class MainActivity extends Activity {  private ImageSwitcher imageSwitcher;  private Button btnPrevious;  private Button btnNext;  private int foot=0;  private int[] imgRes=new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,};    public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState); // 生命周期方法    super.setContentView(R.layout.activity_main); // 设置要使用的布局管理器  imageSwitcher=(ImageSwitcher)findViewById(R.id.imageSwitcher);  btnPrevious=(Button)findViewById(R.id.btnPrevious);  btnNext=(Button)findViewById(R.id.btnNext);    imageSwitcher.setFactory(new ViewFactory() {//设置转化工厂        @Override    public View makeView() {      ImageView imageView=new ImageView(MainActivity.this);      imageView.setBackgroundColor(0xFFFFFFFF);      imageView.setScaleType(ImageView.ScaleType.CENTER);//居中显示      imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));//定义组件      return imageView;    }  });  imageSwitcher.setImageResource(imgRes[foot++]);//初始化时显示,必须放在工厂后面,否则会报NullPointerException  imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//设置动画  imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));//设置动画  btnPrevious.setOnClickListener(new OnClickListener() {        public void onClick(View v) {      MainActivity.this.imageSwitcher.setImageResource(imgRes[foot--]);      MainActivity.this.checkBtnEnable();          }  });  btnNext.setOnClickListener(new OnClickListener() {        public void onClick(View v) {      MainActivity.this.imageSwitcher.setImageResource(imgRes[foot++]);      MainActivity.this.checkBtnEnable();          }  });  }  protected void checkBtnEnable() {//判断按钮可用状态    if(this.foot<this.imgRes.length-1){      this.btnNext.setEnabled(true);    }else{      this.btnNext.setEnabled(false);    }    if(this.foot==0){      this.btnPrevious.setEnabled(false);    }else {      this.btnPrevious.setEnabled(true);    }      }}