Gallery和IamgeSwitcher协作幻灯片效果

  今天学习了Android的Gallery,根据自己的喜好做了一个NBA球星的幻灯片,也算是一边学习,一边自娱自乐吧。。。

package com.test.gallery;

import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class TestGallleryActivity extends Activity {
	int[] imgIds = new int[]{R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,
			R.drawable.a5,R.drawable.a6,R.drawable.a7,R.drawable.a8,
			R.drawable.a9,R.drawable.a10,R.drawable.a11,R.drawable.a12,
			R.drawable.a13,R.drawable.a14,R.drawable.a15,R.drawable.a16};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final ImageSwitcher switcher = (ImageSwitcher)findViewById(R.id.switcher);
        switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        switcher.setFactory(new ViewFactory() {
			
	public View makeView() {
	ImageView img1 = new ImageView(TestGallleryActivity.this);
	img1.setBackgroundColor(0xff0000);	         
	img1.setScaleType(ImageView.ScaleType.FIT_CENTER);
	img1.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				
			return img1;
			}
		});
       
        BaseAdapter adapter = new BaseAdapter(){

	public int getCount() {
				// TODO Auto-generated method stub
		return imgIds.length;
			}

      public Object getItem(int position) {
				// TODO Auto-generated method stub
		return position;
			}

	public long getItemId(int position) {
				// TODO Auto-generated method stub
		return position;
			}

	public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
		ImageView img2 = new ImageView(TestGallleryActivity.this);
	img2.setImageResource(imgIds[position]);
	img2.setScaleType(ImageView.ScaleType.FIT_XY);
		img2.setLayoutParams(new Gallery.LayoutParams(75,100));
		TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
		img2.setBackgroundResource(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground,0));
			return img2;
			}};
			
			Gallery gallery = (Gallery)findViewById(R.id.gallery);
			gallery.setAdapter(adapter);
			gallery.setOnItemClickListener(new OnItemClickListener(){

	public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
	
		switcher.setImageResource(imgIds[arg2]);
				}});
    }
}

  新人,第一次发博,代码弄的太难看,以后小段发,见谅了。

 

  

posted @ 2012-03-24 19:58  Skton  阅读(1149)  评论(0编辑  收藏  举报