Android学习笔记之ImageSwitcher(2)(上一张,下一张按钮)

这篇博客详细介绍了如何在Android中使用ImageSwitcher组件实现图片上下切换的功能,提供了对应的main.xml布局代码,并提供了源代码下载链接。
摘要由CSDN通过智能技术生成

package xiaosi.imageswitcher;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class ImageSwitcherActivity extends Activity implements ViewFactory {
	
	private ImageSwitcher is_imageSwitcher;

	//存放图片id的int数组
	private int[] images={
		 R.drawable.a,
		 R.drawable.b,
		 R.drawable.c,
		 R.drawable.d,
		 R.drawable.e,
		 R.drawable.f,
		 R.drawable.g,
		 R.drawable.h,};

	//下一张和上一张按钮
	private ImageButton next;
	private ImageButton last;
	private int index=0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        is_imageSwitcher=(ImageSwitcher)findViewById(R.id.is_imageswitch);
        last=(ImageButton)findViewById(R.id.last);
        next=(ImageButton)findViewById(R.id.next);
        
        //imageSwticher必须设置一个viewfactory后才可以查看图片
        is_imageSwitcher.setFactory(this);

        //设置图片资源id
        is_imageSwitcher.setBackgroundResource(images[index]);
     }
    
     public View makeView() {
    	   //定义每个图像的显示大小
    	   ImageView imageView = new ImageView(this);
    	   imageView.setLayoutParams(new ImageSwitcher.LayoutParams(300, 300));
    	   
    	   return imageView;
 	}
    

    //上一张的按钮事件
    public void onClickLast(View v)
    {
    	if(index == 0){
    		index = images.length-1;
    	}
    	else{
    		index--;
    	}
		is_imageSwitcher.setBackgroundResource(images[index%images.length]);
    }
    

    //下一张的按钮事件
    public void onClickNext(View v)
    {
    	index++;
		is_imageSwitcher.setBackgroundResource(images[index%images.length]);
    }
}


 

mian.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
 	android:layout_height="fill_parent">
    
 	<ImageSwitcher android:id="@+id/is_imageswitch"
  	    android:layout_width="fill_parent" 
  	    android:layout_height="fill_parent">
  	</ImageSwitcher>
    
 	
 	<LinearLayout
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
 	android:layout_height="wrap_content">
    <ImageButton
        android:src="@drawable/previous" 
  	    android:id="@+id/last" 
  	    android:layout_width="40dp"
   		android:layout_height="40dp" 
   		android:layout_marginRight="10dp"
   		android:onClick="onClickLast"/>
 	 <ImageButton 
 	     android:src="@drawable/next" 
 	     android:id="@+id/next" 
 	     android:layout_width="40dp"
   		 android:layout_height="40dp"
   		  android:onClick="onClickNext"
   		 />
    </LinearLayout>
</FrameLayout>


 

 

源代码下载:点击下载

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值