Android中Gallery来显示图片

public class MainActivity extends Activity {

	private Gallery gallery;

	private LayoutInflater inflater;

	private ImageSwitcher imageSwitcher;

	private int res[] = new int[] { R.drawable.ic_launcher, R.drawable.an01,
			R.drawable.an02, R.drawable.an03, R.drawable.an01, R.drawable.an02,
			R.drawable.an03 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	//	inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

		/**
		 * 系统中有没有合适的适配器
		 */
		gallery = (Gallery) findViewById(R.id.gallery1);

		// 获取ImageView控件
		imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);

		// 初始化一次工厂即可
		imageSwitcher.setFactory(new ImageSwitcher.ViewFactory() {

			// 设置图片
			@Override
			public View makeView() {
				ImageView imageView = new ImageView(MainActivity.this);

				imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
				return imageView;
			}

		});

		// 加入动画
		imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
				MainActivity.this, android.R.anim.fade_in));
		imageSwitcher.setOutAnimation(MainActivity.this,
				android.R.anim.fade_out);

		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

		for (int i = 0; i < res.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("imageView", res[i]);

			data.add(map);
		}

		SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,
				R.layout.activity_cell, new String[] { "imageView" },
				new int[] { R.id.imageView1 });

		gallery.setAdapter(simpleAdapter);

		// 注册事件
		gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				// 获取图片资源
				int imageResId = res[position % res.length];

				// 设置imageSwithcer组件资源
				imageSwitcher.setImageResource(imageResId);

			}
		});
	}

	/*public class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return Integer.MAX_VALUE;
		}

		@Override
		public Object getItem(int position) {
			return res[position % res.length];
		}

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = inflater.inflate(R.layout.activity_gallery, null);
			ImageView iv = (ImageView) v.findViewById(R.id.imageView1);
			int imageResId = res[position % res.length];
			System.out.println(position + "-------" + imageResId);
			iv.setImageResource(imageResId);
			return v;
		}

	}*/

}


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp" />

    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

    </ImageSwitcher>

</LinearLayout>


activity_cell.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    
</LinearLayout>


activity_gallery.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/an01" />

</LinearLayout>


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值