Gallery 画廊效果

http://www.cnblogs.com/nokiaguy/archive/2010/08/23/1806870.html
http://smallnopoint.iteye.com/blog/725518
1.首先创建实现OnItemSelectedListener,ViewFactory类。
2.创建ImageAdapter类。
3.Activity类。
MyOnItemSelectedListener:当Gallery被选择的时候,把被选择的图片放到ImageSwitch里面去。
MyImageAdapter:实现Gallery显示的图片。
ViewFactory:ImageSwitch的显示工厂。

main.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" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_marginTop="30dp"/>
    
    <ImageSwitcher android:id="@+id/imageswitcher1"  
     android:layout_height="fill_parent"  
    android:layout_width="fill_parent"  
    android:layout_alignParentTop="true"  
    android:layout_alignParentLeft="true">  
    </ImageSwitcher>
</LinearLayout>


MyOnItemSelectedListener.java
-----------------------
package com.gallery;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageSwitcher;

public class MyOnItemSelectedListener implements OnItemSelectedListener{
	private ImageSwitcher imageswitcher1;
	private int[] images;
	
	public MyOnItemSelectedListener(ImageSwitcher imageswitcher1,int[] images){
		this.imageswitcher1 = imageswitcher1;
		this.images = images;
		
	}
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		// 第2点改进,通过取余来循环取得images数组中的图像资源ID
		imageswitcher1.setImageResource(images[position % images.length]);  
		
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
}


MyImageAdapter.java
----------------------
package com.gallery;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class MyImageAdapter extends BaseAdapter {

	public Context context;
	private int[] images;
	public MyImageAdapter(Context context,int[]images){
		this.context = context;
		this.images = images;
	}
	@Override
	public int getCount() {
		//获得图片的总数,这里设定为最大值,是为了模拟循环显示
		return Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView = new ImageView(context);
		// 第2点改进,通过取余来循环取得images数组中的图像资源ID
		imageView.setImageResource(images[position % images.length]);
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
		//imageView.setBackgroundResource(mGalleryItemBackground);
		return imageView;
	}

}



ViewFactory类
-----------------------
package com.gallery;

import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class MyViewFactory implements ViewFactory {
	
	private Context context;

	public MyViewFactory(Context context){
		this.context = context;
	}
	@Override
	public View makeView() {
		ImageView i = new ImageView(context);  
        i.setBackgroundColor(0xFF000000);  
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);  
        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
        return i;
	}

}


Activity类
---------------------------
package com.gallery;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.AnimationUtils;
import android.widget.Gallery;
import android.widget.ImageSwitcher;

public class MyGalleryActivity extends Activity{
	private Gallery gallery1;
	private ImageSwitcher imageswitcher1;
	private int[] images = new int[] { R.drawable.android0001, R.drawable.android0002, R.drawable.android0003, R.drawable.android0004, R.drawable.android0005, R.drawable.android0006, R.drawable.android0007, R.drawable.android0008 };
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        imageswitcher1 = (ImageSwitcher)findViewById(R.id.imageswitcher1);
        imageswitcher1.setFactory(new MyViewFactory(MyGalleryActivity.this));
        imageswitcher1.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));  
        imageswitcher1.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); 
        
        gallery1 = (Gallery)findViewById(R.id.gallery1);
        gallery1.setAdapter(new MyImageAdapter(this,images));
        gallery1.setOnItemSelectedListener(new MyOnItemSelectedListener(imageswitcher1,images));
    }

}


你可能感兴趣的:(Gallery 画廊效果)