Gallery:android:galleryItemBackground 什么效果

简介: 源码下载地址:http://download.csdn.net/detail/flyingsir_zw/9658434 设置 android:galleryItemBackground 后的效果 不设置的效果如下: 设置 android:galleryItemBackground 的方法如下: 1.项目目录res/values/a




源码下载地址http://download.csdn.net/detail/flyingsir_zw/9658434






设置

android:galleryItemBackground

后的效果



不设置的效果如下:



设置

android:galleryItemBackground

的方法如下:

1.项目目录res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    
    <declare-styleable name="HelloGallery">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>
    
</resources>

2.Gallery  自定义适配器中   构造方法中初始化 引用即可如下代码

public class MygalleryAdapter extends BaseAdapter {

	private Context mContext;
	private Integer[] data;
	private int mGalleryItemBackground;

	public MygalleryAdapter(Context context, Integer[] data) {
		this.mContext = context;
		this.data = data;
		// 获取自定义的属性值,res/values/attrs.xml
		TypedArray attr = mContext
				.obtainStyledAttributes(R.styleable.HelloGallery);
		mGalleryItemBackground = attr.getResourceId(
				R.styleable.HelloGallery_android_galleryItemBackground, 0);
		attr.recycle();
	}
}




源码下载地址:http://download.csdn.net/detail/flyingsir_zw/9658434




完整适配器代码

public class MygalleryAdapter extends BaseAdapter {

	private Context mContext;
	private Integer[] data;
	private int mGalleryItemBackground;

	public MygalleryAdapter(Context context, Integer[] data) {
		this.mContext = context;
		this.data = data;
		// 获取自定义的属性值,res/values/attrs.xml
		TypedArray attr = mContext
				.obtainStyledAttributes(R.styleable.HelloGallery);
		mGalleryItemBackground = attr.getResourceId(
				R.styleable.HelloGallery_android_galleryItemBackground, 0);
		attr.recycle();
	}

	@Override
	public int getCount() {

		return data.length;
	}

	@Override
	public Object getItem(int position) {

		return null;
	}

	@Override
	public long getItemId(int position) {

		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder mViewHolder;
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.gv_list_item, null);
			// 创建一个绑定对象
			mViewHolder = new ViewHolder();
			// 初始化控件
			mViewHolder.img = (ImageView) convertView.findViewById(R.id.img);
			mViewHolder.img.setBackgroundResource(mGalleryItemBackground);
			// 绑定
			convertView.setTag(mViewHolder);
		} else {
			// 从绑定对戏里重新获取一个View
			mViewHolder = (ViewHolder) convertView.getTag();
		}
		// 赋值
		mViewHolder.img.setImageResource(data[position]);
		return convertView;
	}
	static class ViewHolder {
		ImageView img;
	}

}


完整类代码

public class MainActivity extends Activity implements ViewFactory {

	private Gallery mGallery;
	private MygalleryAdapter mygalleryAdapter;

	// references to our images
	private Integer[] mThumbIds = null;

	private ImageSwitcher mImageSwitcher;

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

		initDataSource();

		// 获取控件
		mGallery = (Gallery) this.findViewById(R.id.gallery);
		mGallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mImageSwitcher.setImageResource(mThumbIds[position]);
			}
		});
		// 创建一个视图切换器
		mImageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher);
		// 切换器上面不能直接显示图片
		// 需要实现这个接口,然后实现makeView()创建一个控件
		mImageSwitcher.setFactory(this);

		initAdapter();
	}

	// 1 初始化数据源
	private void initDataSource() {
		mThumbIds = new Integer[] { R.drawable.sample_2, R.drawable.sample_3,
				R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
				R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1,
				R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
				R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7,
				R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
				R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
				R.drawable.sample_6, R.drawable.sample_7 };
	}

	private void initAdapter() {
		mygalleryAdapter = new MygalleryAdapter(this, mThumbIds);
		mGallery.setAdapter(mygalleryAdapter);
		mGallery.setSelection(mThumbIds.length / 2);
	}

	@Override
	public View makeView() {
		// 因为ImageSwitcher控件上面不能直接显示图片,需要创建一个可以显示图片控件
		ImageView img = new ImageView(this);
		// 先new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)
		// 最后在前面添加一个ImageSwitcher
		img.setLayoutParams(new ImageSwitcher.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		img.setScaleType(ImageView.ScaleType.FIT_XY);
		img.setImageResource(mThumbIds[mThumbIds.length / 2]);
		return img;
	}
}


xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="120dip"
        android:layout_alignParentTop="true" />

    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/gallery" />

</RelativeLayout>


gallery  适配器自布局  xml

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

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:scaleType="fitXY"
        android:layout_centerVertical="true"/>

</RelativeLayout>








源码下载地址:http://download.csdn.net/detail/flyingsir_zw/9658434













源码下载地址:http://download.csdn.net/detail/flyingsir_zw/9658434



源码下载地址:http://download.csdn.net/detail/flyingsir_zw/9658434


目录
相关文章
|
Android开发
Android Coverflow Gallery 的关键源码解析【Android】【OpenGL】
Android Coverflow Gallery 的关键源码解析【Android】【OpenGL】
107 0
Android Coverflow Gallery 的关键源码解析【Android】【OpenGL】
|
API Android开发 容器
|
Android开发