鸟语天空
基本视图——Gallery
post by:追风剑情 2015-9-16 22:49

一、准备图片

g1.png

二、配置attrs.xml

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

三、创建活动

视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Image of San Francisco" />
    
    <Gallery 
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
    <ImageView 
        android:id="@+id/image1"
        android:layout_width="320dp"
        android:layout_height="250dp"
        android:scaleType="fitXY"/>
</LinearLayout>

代码

package com.example.androidtest;

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 GalleryActivity extends Activity {

	Integer[] imageIDs = {
		R.drawable.pic1,
		R.drawable.pic2,
		R.drawable.pic3,
		R.drawable.pic4,
		R.drawable.pic5,
		R.drawable.pic6,
		R.drawable.pic7
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gallery);
		
		Gallery gallery = (Gallery) findViewById(R.id.gallery1);
		gallery.setAdapter(new ImageAdapter(this));
		gallery.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getBaseContext(), "pic"+(position+1)+" selected",
						Toast.LENGTH_SHORT).show();
				
				ImageView imageView = (ImageView) findViewById(R.id.image1);
				imageView.setImageResource(imageIDs[position]);
			}
			
		});
	}
	
	public class ImageAdapter extends BaseAdapter
	{
		Context context;
		int itemBackground;
		
		public ImageAdapter(Context c)
		{
			context = c;
			//设置样式
			TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
			itemBackground = a.getResourceId(
					R.styleable.Gallery1_android_galleryItemBackground, 0);
			a.recycle();//回收a对象
		}

		@Override
		public int getCount() {
			return imageIDs.length;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView;
			if(convertView == null) {
				imageView = new ImageView(context);
				imageView.setImageResource(imageIDs[position]);
				imageView.setScaleType(ImageView.ScaleType.FIT_XY);
				imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
			}else{
				imageView = (ImageView) convertView;
			}
			imageView.setBackgroundResource(itemBackground);
			return imageView;
		}
		
	}
}

运行效果

g2.png


示例说明:

      创建的ImageAdapter类扩展了BaseAdapter类,可以用来将一系列ImageView视图绑定到Gallery视图上。BaseAdapter类作为联系AdapterView和为它提供数据的数据源之间的桥梁。下面列出了AdapterView的一些示例:

在Android中,BaseAdapter类有以下几个子类:

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容