8 Android gallery

package com.wjl.test;



import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GallentTestActivity extends Activity {
    /** Called when the activity is first created. */
	
//	int[] ids={R.drawable.a1,
//			R.drawable.a2,
//			R.drawable.a3,
//			R.drawable.a4,
//			R.drawable.a5};
	private Gallery gallery;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        gallery=(Gallery)findViewById(R.id.gallery1);
        
//        BaseAdapter ba=new BaseAdapter() {
//			
//			public View getView(int position, View convertView, ViewGroup parent) {
//				ImageView imageView=new ImageView(GallentTestActivity.this);
//				imageView.setImageResource(ids[position]);
//				imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//				imageView.setLayoutParams(new Gallery.LayoutParams(200, 300));
//				imageView.setTag(1000);
//				return imageView;
//			}
//			
//			public long getItemId(int position) {
//				// TODO Auto-generated method stub
//				return position;
//			}
//			
//			public Object getItem(int position) {
//				// TODO Auto-generated method stub
//				return null;
//			}
//			
//			public int getCount() {
//				// TODO Auto-generated method stub
//				return ids.length;
//			}
//		};
        
		gallery.setAdapter(new ImageAdapter(this));
		
		gallery.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Gallery gallery=(Gallery)findViewById(R.id.gallery1);
				gallery.setSelection(arg2);
				
				
			}
		});
    }
}

class ImageAdapter extends BaseAdapter
{
	private Context context;
	//图片源数组
	private Integer[] ids={R.drawable.a1,
			R.drawable.a2,
			R.drawable.a3,
			R.drawable.a4,
			R.drawable.a5};
	
	public ImageAdapter(Context c)
	{
		this.context=c;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return ids.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 imageView=new ImageView(context);
		imageView.setImageResource(ids[position]);
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		imageView.setLayoutParams(new Gallery.LayoutParams(200, 300));
		return imageView;
	}
	}

你可能感兴趣的:(8 Android gallery)