Gallery与衍生BaseAdapter
本范例的重点在于如何设置图片的宽高以及防止图片Layout的大小,在此我们改写一个继承自BaseAdapter的ImageAdapter容器来存放图片,通过ImageView.setScaleTyper()的方法来改变图片的显示,再通过setLayoutParams()方法来改变Layout的宽高。
主程序有两大重点:一是ImageAdapter继承BaseAdapter class的未实现方法的重写构造;二是Gallery的OnItemClick()方法与图片及Layout宽高设置。
下面是程序完整代码:
package com.example.test01;
import android.app.Activity;
importandroid.content.Context;
importandroid.content.res.TypedArray;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
importandroid.view.ViewGroup;
importandroid.widget.AdapterView.OnItemClickListener;
importandroid.widget.AdapterView;
importandroid.widget.BaseAdapter;
importandroid.widget.Gallery;
importandroid.widget.ImageView;
import android.widget.Toast;
public class MainActivityextends Activity {
Gallery gallery01;
//构建Integer array并取得预加载Drawable的图片id
private Integer[] myImageIds =
{
R.drawable.a11,
R.drawable.a12,
R.drawable.a5,
R.drawable.a7,
R.drawable.a9,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery01 = (Gallery)findViewById(R.id.gallery01);
//添加ImageAdapter并设置给Gallery对象
gallery01.setAdapter(new ImageAdapter(this));
//设置一个itemClickListener并Toast被单击图片的位置
gallery01.setOnItemClickListener(newOnItemClickListener() {
public void onItemClick(AdapterView<?>arg0, View arg1, int arg2,long arg3) {
Toast.makeText(getApplicationContext(),getString(R.string.my_gallery_text_pre) + arg2 +getString(R.string.my_gallery_text_post), Toast.LENGTH_LONG).show();
}
});
}
//改写BaseAdapter自定义ImageAdapter class
public class ImageAdapter extends BaseAdapter
{
//声明变量
int mGalleryItemBackground;
private Context context;
public ImageAdapter(Context c) {
context = c;
//使用在res/values/attrs.xml中的<declare-styleable>定义*Gallery属性
TypedArray a =obtainStyledAttributes(R.styleable.Gallery);
//取得Gallery属性的Index id
mGalleryItemBackground =a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
//让对象的styleable属性能够反复使用
a.recycle();
}
public int getCount() {
return myImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//重写的方法getView,返回一个View对象
public View getView(int position, View convertView,ViewGroup parent) {
//产生ImageView对象
ImageView i = new ImageView(context);
//设置图片给imageView对象
i.setImageResource(myImageIds[position]);
//重新设置图片的高
i.setScaleType(ImageView.ScaleType.FIT_XY);
//重新设置Layout的宽高
i.setLayoutParams(new Gallery.LayoutParams(136,88));
//设置Gallery的背景图
i.setBackgroundResource(mGalleryItemBackground);
//返回imageView对象
return i;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action barif it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
扩展学习:
在Android:ScaleType中定义了下列常数可供使用,通过”ObjectView.ScaleType常数名称”的方式,就可以改变图片的显示方式。
常数名称 | 值 | 常数名称 | 值 |
Matrix | 0 | fitEnd | 4 |
FitXY | 1 | Center | 5 |
fitStart | 2 | centerCrop | 6 |
fitCenter | 3 | centerInside | 7 |
另外,在主程序中,我们使用了下面这一段写法:
TypeArray a =obtainStyledAttributes(R.styleable.Gallery);
这是一个引用自制Layout元素的用法,必须在res/values下面添加一个attrs.xml,并在其中定义<declare-styleable>标签TAG,目的是自定义Layout的北京风格,并且通过TypeArray的特性,让相同的Layout元素可以重复用于每一张图片。