我有GridView
加载了大约15张图片,而不是那么多,但是当我测试它时,滚动速度非常慢,我无法弄明白为什么。
我从array.xml
文件中获取所有图片。
array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="chuck_ids">
<item>@drawable/chuck_1</item>
<item>@drawable/chuck_2</item>
<item>@drawable/chuck_3</item>
<item>@drawable/chuck_4</item>
<item>@drawable/chuck_5</item>
<item>@drawable/chuck_6</item>
<item>@drawable/chuck_7</item>
<item>@drawable/chuck_8</item>
<item>@drawable/chuck_9</item>
<item>@drawable/chuck_10</item>
<item>@drawable/chuck_11</item>
<item>@drawable/chuck_12</item>
<item>@drawable/chuck_13</item>
<item>@drawable/chuck_14</item>
</string-array>
</resources>
这是我的代码:
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Fragment;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import com.italiandevteam.chuck.adapter.GridViewAdapter;
import com.italiandevteam.chuck.model.ImageItem;
public class GalleriaPersonaggio extends Fragment{
Integer personaggio = null;
public GalleriaPersonaggio( int personaggio ){
this.personaggio = personaggio;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int id = getIdPersonaggio();
View rootView = null;
try {
rootView = inflater.inflate(R.layout.gallery_grid, container, false);
}
catch (InflateException e) {
}
final GridView gridView = (GridView) rootView.findViewById(R.id.gridView);
GridViewAdapter customGridAdapter = new GridViewAdapter(getActivity(), R.layout.gallery_row, getData(personaggio));
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// HashMap<String, Object> hm = gridView.getAdapter().getPosition(position);
//
// String imgPath = (String) hm.get("flag"); //get downloaded image path
// Intent i = new Intent(getActivity(), MostraImmagine.class); //start new Intent to another Activity.
// i.putExtra("ClickedImagePath", imgPath ); //put image link in intent.
// startActivity(i);
}
});
return rootView;
}
public int getIdPersonaggio(){
return this.personaggio;
}
private ArrayList getData(int personaggio) {
final ArrayList imageItems = new ArrayList();
// retrieve String drawable array
TypedArray imgs = null;
switch( personaggio )
{
case 1:{
imgs = getResources().obtainTypedArray(R.array.chuck_ids);
break;
}
case 2:{
imgs = getResources().obtainTypedArray(R.array.sarah_ids);
break;
}
case 3:{
//To do
break;
}
case 4:{
//To do
break;
}
case 5:{
//To do
break;
}
case 6:{
//To do
break;
}
case 7:{
//To do
break;
}
case 8:{
//To do
break;
}
default:{
imgs = getResources().obtainTypedArray(R.array.chuck_ids);
}
}
for (int i = 0; i < imgs.length(); i++) {
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), imgs.getResourceId(i, -1));
imageItems.add(new ImageItem(bitmap, "Image#" + i));
}
return imageItems;
}
}
GridViewAdapter.java
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.italiandevteam.chuck.R;
import com.italiandevteam.chuck.model.ImageItem;
public class GridViewAdapter extends ArrayAdapter<ImageItem> {
private Context context;
private int layoutResourceId;
private ArrayList<ImageItem> data = new ArrayList<ImageItem>();
public GridViewAdapter(Context context, int layoutResourceId,
ArrayList<ImageItem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
ImageItem item = data.get(position);
holder.imageTitle.setText(item.getTitle());
holder.image.setImageBitmap(item.getImage());
return row;
}
static class ViewHolder {
TextView imageTitle;
ImageView image;
}
}
ImageItem.java
import android.graphics.Bitmap;
public class ImageItem {
private Bitmap image;
private String title;
public ImageItem(Bitmap image, String title) {
super();
this.image = image;
this.title = title;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
基本上我根据java文件中的开关中的drawable
加载array ID
资源。
如何改进滚动?
答案 0 :(得分:0)
请尝试Universal Image Downloader从服务器或SD卡加载图像。它就像魅力一样!
答案 1 :(得分:0)
是的,你应该创建拇指并缓存内存。它肯定会解决你的问题。
这是android中缓存实现的链接。
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html