Android 控件之十二:Gallery画廊

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
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;
import android.widget.Toast;

public class MainHelloGallery extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 定义UI组件
		final ImageView iv = (ImageView) findViewById(R.id.ImageView01);
		Gallery g = (Gallery) findViewById(R.id.Gallery01);
		// 设置图片匹配器
		g.setAdapter(new ImageAdapter(this));
		// 设置AdapterView点击监听器,Gallery是AdapterView的子类
		g.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 显示点击的是第几张图片
				Toast.makeText(MainHelloGallery.this, "" + position,
						Toast.LENGTH_LONG).show();
				// 设置背景部分的ImageView显示当前Item的图片
				iv.setImageResource(((ImageView) view).getId());
			}
		});
	}

	// 定义继承BaseAdapter的匹配器
	public class ImageAdapter extends BaseAdapter {
		// Item的修饰背景
		int mGalleryItemBackground;

		// 上下文对象
		private Context mContext;
		// 图片数组
		private Integer[] mImageIds = { R.drawable.wallpaper_0,
				R.drawable.wallpaper_1, R.drawable.wallpaper_2,
				R.drawable.wallpaper_3, R.drawable.wallpaper_4,
				R.drawable.wallpaper_5, R.drawable.wallpaper_6,
				R.drawable.wallpaper_7, R.drawable.wallpaper_8,
				R.drawable.wallpaper_9 };

		// 构造方法
		public ImageAdapter(Context c) {
			mContext = c;
			// 读取styleable资源
			TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
			mGalleryItemBackground = a.getResourceId(
					R.styleable.HelloGallery_android_galleryItemBackground, 0);
			a.recycle();
		}

		// 返回项目数量
		@Override
		public int getCount() {
			return mImageIds.length;
		}

		// 返回项目
		@Override
		public Object getItem(int position) {
			return position;
		}

		// 返回项目Id
		@Override
		public long getItemId(int position) {
			return position;
		}

		// 返回视图
		@Override
		public View getView(int position, View convertVieww, ViewGroup parent) {
			ImageView iv = new ImageView(mContext);
			iv.setImageResource(mImageIds[position]);
			// 给生成的ImageView设置Id,不设置的话Id都是-1
			iv.setId(mImageIds[position]);
			iv.setLayoutParams(new Gallery.LayoutParams(1200, 160));
			iv.setScaleType(ImageView.ScaleType.FIT_XY);
			iv.setBackgroundResource(mGalleryItemBackgroundd);
			return iv;
		}
	}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FrameLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/wallpaper_0" >
    </ImageView>

    <Gallery
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Gallery01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:spacing="5dp" >
    </Gallery>

</FrameLayout>
在 res/values/目录中新建一个 attrs.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<resources>


    <declare-styleable name="HelloGallery" >


        <attr name="android:galleryItemBackground" >
        </attr>
    </declare-styleable>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值