[Android学习笔记3]ImageSwitcher


        

今天实现一个比较简单的图片切换效果的小程序。

利用ImageSwitcher类,通过设置一个ViewFactory工厂,实现其makeView()方法来创建显示图片的View。

方法setImageResource用来显示指定的图片资源。

 

 

1. 将8张图片放入/res/drawable目录下,同时命名为sample_0,sample_1,....sample_7等;

2. 在代码中定义资源id数组;

  1. static final Integer[] imagelist =   
  2.         {  
  3.             R.drawable.sample_0,  
  4.             R.drawable.sample_1,  
  5.             R.drawable.sample_2,  
  6.             R.drawable.sample_3,  
  7.             R.drawable.sample_4,  
  8.             R.drawable.sample_5,  
  9.             R.drawable.sample_6,  
  10.             R.drawable.sample_7,  
  11.         };  
static final Integer[] imagelist = 
		{
			R.drawable.sample_0,
			R.drawable.sample_1,
			R.drawable.sample_2,
			R.drawable.sample_3,
			R.drawable.sample_4,
			R.drawable.sample_5,
			R.drawable.sample_6,
			R.drawable.sample_7,
		};

 

 

Activity.java

  1. package com.luoye.allview;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.ImageSwitcher;  
  9. import android.widget.ImageView;  
  10. import android.widget.ViewSwitcher.ViewFactory;  
  11.   
  12. public class SecondActivity extends Activity implements OnClickListener, ViewFactory{  
  13.   
  14.       
  15.     ImageSwitcher imageswitcher;  
  16.     private Button button_pre;  
  17.     private Button button_next;  
  18.       
  19.     static final Integer[] imagelist =   
  20.         {  
  21.             R.drawable.sample_0,  
  22.             R.drawable.sample_1,  
  23.             R.drawable.sample_2,  
  24.             R.drawable.sample_3,  
  25.             R.drawable.sample_4,  
  26.             R.drawable.sample_5,  
  27.             R.drawable.sample_6,  
  28.             R.drawable.sample_7,  
  29.         };  
  30.     private static int index = 0;  
  31.       
  32.       
  33.       
  34.     @Override  
  35.     protected void onCreate(Bundle savedInstanceState) {  
  36.         // TODO Auto-generated method stub  
  37.         super.onCreate(savedInstanceState);  
  38.         setContentView(R.layout.secondactivity);  
  39.           
  40.         imageswitcher = (ImageSwitcher)findViewById(R.id.imageswitch);  
  41.         imageswitcher.setFactory(this);  
  42.         imageswitcher.setImageResource(imagelist[index]);  
  43.           
  44.         button_pre = (Button)findViewById(R.id.button_pre);  
  45.         button_next = (Button)findViewById(R.id.button_next);  
  46.           
  47.         button_pre.setOnClickListener(this);  
  48.         button_next.setOnClickListener(this);  
  49.     }  
  50.   
  51.     @Override  
  52.     public View makeView() {  
  53.         // TODO Auto-generated method stub  
  54.         return new ImageView(this);  
  55.     }  
  56.   
  57.     @Override  
  58.     public void onClick(View v) {                      //实现button的onClick方法  
  59.         // TODO Auto-generated method stub  
  60.           
  61.         switch(v.getId())  
  62.         {  
  63.             case R.id.button_next:  
  64.                 index++;  
  65.                 if(index == imagelist.length)  
  66.                 {  
  67.                     index = 0;  
  68.                 }  
  69.                 imageswitcher.setImageResource(imagelist[index]);  
  70.                 break;  
  71.             case R.id.button_pre:  
  72.                 index--;  
  73.                 if(index < 0)  
  74.                 {  
  75.                     index =  imagelist.length - 1;  
  76.                 }  
  77.                 imageswitcher.setImageResource(imagelist[index]);  
  78.                 break;  
  79.             default:  
  80.                 break;  
  81.         }  
  82.           
  83.     }  
  84.   
  85. }  
package com.luoye.allview;

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

public class SecondActivity extends Activity implements OnClickListener, ViewFactory{

	
	ImageSwitcher imageswitcher;
	private Button button_pre;
	private Button button_next;
	
	static final Integer[] imagelist = 
		{
			R.drawable.sample_0,
			R.drawable.sample_1,
			R.drawable.sample_2,
			R.drawable.sample_3,
			R.drawable.sample_4,
			R.drawable.sample_5,
			R.drawable.sample_6,
			R.drawable.sample_7,
		};
	private static int index = 0;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.secondactivity);
		
		imageswitcher = (ImageSwitcher)findViewById(R.id.imageswitch);
		imageswitcher.setFactory(this);
		imageswitcher.setImageResource(imagelist[index]);
		
		button_pre = (Button)findViewById(R.id.button_pre);
		button_next = (Button)findViewById(R.id.button_next);
		
		button_pre.setOnClickListener(this);
		button_next.setOnClickListener(this);
	}

	@Override
	public View makeView() {
		// TODO Auto-generated method stub
		return new ImageView(this);
	}

	@Override
	public void onClick(View v) {                      //实现button的onClick方法
		// TODO Auto-generated method stub
		
		switch(v.getId())
		{
			case R.id.button_next:
				index++;
				if(index == imagelist.length)
				{
					index = 0;
				}
				imageswitcher.setImageResource(imagelist[index]);
				break;
			case R.id.button_pre:
				index--;
				if(index < 0)
				{
					index =  imagelist.length - 1;
				}
				imageswitcher.setImageResource(imagelist[index]);
				break;
			default:
				break;
		}
		
	}

}

布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <ImageSwitcher   
  8.         android:id="@+id/imageswitch"  
  9.   android:layout_width="wrap_content"  
  10.   android:layout_height="100dp"  
  11.         ></ImageSwitcher>  
  12.       
  13.     <Button   
  14.         android:id="@+id/button_next"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="上一张"  
  18.         />  
  19.       
  20.     <Button   
  21.         android:id="@+id/button_pre"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="下一张"  
  25.         />  
  26.   
  27. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <ImageSwitcher 
        android:id="@+id/imageswitch"
  android:layout_width="wrap_content"
  android:layout_height="100dp"
        ></ImageSwitcher>
    
    <Button 
        android:id="@+id/button_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上一张"
        />
    
    <Button 
        android:id="@+id/button_pre"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一张"
        />

</LinearLayout>



效果:

点击下一张后,实现了切换

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值