【发布时间】:2014-12-13 14:36:40
【问题描述】:
我正在使用显示从assets 文件夹加载的图像的gridView。
gridview 显示来自 assets 的 50 多张图像,但该 gridview 的性能很差,并且存在滚动时滞。
示例代码为:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ameme);
GridView myGallery = (GridView) findViewById(R.id.gridView1);
mBitArray = new Bitmap[5];
//This is just a sample code
try
{
//these images are stored in the root of "assets"
mBitArray[0] = getBitmapFromAsset("AM0.jpg");
mBitArray[1] = getBitmapFromAsset("AM1.jpg");
mBitArray[2] = getBitmapFromAsset("AM2.jpg");
mBitArray[3] = getBitmapFromAsset("AM3.jpg");
mBitArray[4] = getBitmapFromAsset("AM4.jpg");
}
catch (IOException e)
{
e.printStackTrace();
}
myGallery.setAdapter(new GalleryAdapter(this, mBitArray));
}
public class GalleryAdapter extends BaseAdapter
{
private Context mContext;
private Bitmap[] mImageArray;
public GalleryAdapter(Context context, Bitmap[] imgArray)
{
mContext = context;
mImageArray = imgArray;
}
public int getCount()
{
return mImageArray.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
final ImageView imgView = new ImageView(mContext);
imgView.setImageBitmap(mImageArray[position]);
final RelativeLayout borderImg = new RelativeLayout(mContext);
borderImg.setPadding(8, 8, 8, 8);
borderImg.setGravity(Gravity.CENTER | Gravity.BOTTOM);
borderImg.setBackgroundColor(Color.rgb(7,35,63));
borderImg.addView(imgView);
imgView.setLayoutParams(new RelativeLayout.LayoutParams(100,100));
return borderImg;
}
}
private Bitmap getBitmapFromAsset(String strName) throws IOException
{
AssetManager assetManager = getAssets();
InputStream istr = assetManager.open(strName);
Bitmap bitmap = BitmapFactory.decodeStream(istr);
istr.close();
return bitmap;
}
}
如何使gridView() 的滚动更顺畅?
【问题讨论】:
-
从回收你的细胞开始,不要忽略
convertViewingetView()。除此之外,使用 Traceview 来确定您的问题所在。 -
我是初学者,怎么做?
标签: android performance gridview android-gridview android-gallery