一.Gallery和ImageView视图:
Gallery是一种用固定在中间位置的水平滚动列表显示列表项的视图。
我们用到了ImageView.ScaleType属性,各类值得区别:
CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示
CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)
CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽
FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY 把图片不按比例扩大/缩小到View的大小显示
MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请浏览图片" />
<Gallery
android:id="@+id/gallery1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image"
android:layout_width="320dp"
android:layout_height="250dp"
android:scaleType="fitXY"
/>
</LinearLayout>
自定义属性(attrs.xml):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground"/>
</declare-styleable>
</resources>
主要代码:
public class MainActivity extends Activity {
private ImageAdapter ad;
private Gallery gallery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ad = new ImageAdapter(this);
gallery = (Gallery)findViewById(R.id.gallery1);
//绑定适配器
gallery.setAdapter(ad);
//设置监听器。
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
toastPrint("img"+(arg2+1));
ImageView i = (ImageView)findViewById(R.id.image);
i.setImageResource(ad.imgId[arg2]);
}
});
}
public void toastPrint(String str){
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
自定义适配器(Adapter):
public class ImageAdapter extends BaseAdapter {
//显示的图片
Integer[] imgId = {R.drawable.img1,
R.drawable.img2,R.drawable.img3,
R.drawable.img4,R.drawable.img5,
R.drawable.img8,R.drawable.img9,};
Context context;
int item;
public ImageAdapter(Context c){
context = c;
//使用在res/value/attrs.xml中的Gallery1属性
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
// 取得Gallery1属性
item = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
//让对象的styleable属性能够反复使用
a.recycle();
}
//返回要显示的图片的总数
@Override
public int getCount() {
// TODO Auto-generated method stub
return imgId.length;
}
//获取图片在库中的位置
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
//获取图片在库中的位置
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView image;
if(convertView == null){
image = new ImageView(context);
//给ImageView设置资源
image.setImageResource(imgId[position]);
//设置图片不按比例大小缩放。
image.setScaleType(ImageView.ScaleType.FIT_XY);
//设置图片大小。
image.setLayoutParams(new Gallery.LayoutParams(200, 150));
}else{
image = (ImageView)convertView;
}
//添加背景框架
image.setBackgroundResource(3);
return image;
}
}
运行图片: