本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!
Android的Gallery控件是个很不错的看图控件,大大减轻了开发者对于看图功能的开发,而且效果也比较美观。本文介绍Gallery的用法,用反射机制来动态读取资源中的图片。
main.xml源码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Gallery android:id="@+id/gallery" android:layout_height="fill_parent" android:layout_width="fill_parent"></Gallery>
</LinearLayout>
程序源码:
package com.testImageView;
import java.lang.reflect.Field;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class testImageView extends Activity {
private Gallery mGallery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGallery = (Gallery)findViewById(R.id.gallery);
try {
mGallery.setAdapter(new ImageAdapter(this));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mGallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
testImageView.this.setTitle(String.valueOf(position));
}
});
}
/*
* class ImageAdapter is used to control gallery source and operation.
*/
private class ImageAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<Integer> imgList=new ArrayList<Integer>();
private ArrayList<Object> imgSizes=new ArrayList<Object>();
public ImageAdapter(Context c) throws IllegalArgumentException, IllegalAccessException{
mContext = c;
//用反射机制来获取资源中的图片ID和尺寸
Field[] fields = R.drawable.class.getDeclaredFields();
for (Field field : fields)
{
if (!"icon".equals(field.getName()))//除了icon之外的图片
{
int index=field.getInt(R.drawable.class);
//保存图片ID
imgList.add(index);
//保存图片大小
int size[]=new int[2];
Bitmap bmImg=BitmapFactory.decodeResource(getResources(),index);
size[0]=bmImg.getWidth();size[1]=bmImg.getHeight();
imgSizes.add(size);
}
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imgList.size();
}
@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 i = new ImageView (mContext);
//从imgList取得图片ID
i.setImageResource(imgList.get(position).intValue());
i.setScaleType(ImageView.ScaleType.FIT_XY);
//从imgSizes取得图片大小
int size[]= new int[2];
size=(int[]) imgSizes.get(position);
i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));
return i;
}
};
}
1)定义 ViewStub
<ViewStub android:id = "@+id/stub_import"
android:inflatedId="@+id/panel_import"
android:layout="@layout/progress_overlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
2)展开视图
findViewById(R.id.stub_import).setVisibility(View.VISIBLE);
// 或者 获取视图
View importPanel = ((ViewStub)
findViewById(R.id.stub_import)).inflate();
有一点需要记住的是:当ViewStub inflate后,这个ViewStub就从View层次中移除了。因此,没有必要保留一个对ViewStub的引用(如在类的字段里)。
ViewStub是快捷编程与高效编程之间的产物。与其手动的inflate View并在运行时添加到View层次上,不如简单的使用ViewStub。它相当“廉价”且易于使用。ViewStub唯一的缺点是现在不支持<merge />标签。
用ViewStub类和在XML文件里面指定的布局资源文件关联起来,让布局资源文件在需要使用的时候再加载上去。主要作用是性能优化,什么时候用什么时候加载,不用在开始启动的时候一次加载,既可以加快程序的启动速度,又可以节省内存资源。
public View getView(int pos, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder.text = (TextView) convertView.findViewById( R.id.text));
holder.icon = (ImageView) convertView.findViewButId( R.id.icon));
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(DATA[pos]);
holder.icon.setImageBitmap((pos & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
// holder = new ViewHolder();
}
// ViewHolder 模式, 效率提高 50%
static class ViewHolder {
TextView text;
ImageView icon;
}
// 内存分配 不要创建 Java 对象 在性能敏感的代码里, 尽量避免创建 Java 对象
测量: 布局: onMeasure() onLayout() 绘图:
事件处理: dispatchTouchEvent(), onTouchEvent()
Adapter: getView(), bindView()
GC, 垃圾回收
整个程序会暂停 慢 (大约几百个毫秒)