android ImageSwitcher的用法

本文詳細講解了圖片切換器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}; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.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{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			imageswitcher.setImageResource(imgRes[foot--]);
			checkButton();
		}
		
	}
	
	private class MyOnClickNext implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			imageswitcher.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{

		@Override
		public View makeView() {
			// TODO Auto-generated method stub
			ImageView imageview=new ImageView(ImageSwitcherDemo.this);
			imageview.setScaleType(ImageView.ScaleType.CENTER);
			imageview.setLayoutParams(new LayoutParams(
					LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
			return imageview;
		}
		
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章