android ImageSwitcher的用法

来源:互联网 发布:激光去黑眼圈 知乎 编辑:程序博客网 时间:2024/05/29 15:52

本文详细讲解了图片切换器ImageSwitcher的用法

imageswitcher.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"      android:orientation="vertical" >    <ImageSwitcher     android:id="@+id/imageswitcher"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    ></ImageSwitcher><LinearLayout     android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal">    "<Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:id="@+id/previous"    android:text="上一张"    android:enabled="false"    /><Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:id="@+id/next"    android:text="下一张"    android:enabled="true"    /></LinearLayout></LinearLayout>

 

ImageSwitcherDemo.java:

package com.example.wenandroid;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.FrameLayout.LayoutParams;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;public class ImageSwitcherDemo extends Activity {private ImageSwitcher imageswitcher;private Button previous;private Button next;private int foot;private int imgRes[]=new int[]{R.drawable.first,R.drawable.second,R.drawable.third}; @Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.imageswitcher);imageswitcher=(ImageSwitcher)findViewById(R.id.imageswitcher);previous=(Button)findViewById(R.id.previous);next=(Button)findViewById(R.id.next);//实现图片动画切换imageswitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));imageswitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));imageswitcher.setFactory(new MyViewFactory());previous.setOnClickListener(new MyOnClickPrevious());next.setOnClickListener(new MyOnClickNext());}private class MyOnClickPrevious implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubimageswitcher.setImageResource(imgRes[foot--]);checkButton();}}private class MyOnClickNext implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubimageswitcher.setImageResource(imgRes[foot++]);checkButton();}}private void checkButton(){if(foot<imgRes.length-1){next.setEnabled(true);}else{next.setEnabled(false);}if(foot==0){previous.setEnabled(false);}else{previous.setEnabled(true);}}private class MyViewFactory implements ViewFactory{@Overridepublic View makeView() {// TODO Auto-generated method stubImageView imageview=new ImageView(ImageSwitcherDemo.this);imageview.setScaleType(ImageView.ScaleType.CENTER);imageview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));return imageview;}}}