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;
		}
		
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值