通过平滑滚动将图像从资源加载到GridView

时间:2014-02-02 19:19:42

标签: android android-gridview

我正在尝试使用以下适配器从assets文件夹加载GridView中的图像,但gridview滚动不顺畅。有人可以建议我如何改进我的代码?

还有一件事由于这个原因导致我在某些设备上出现内存错误。我该如何优化这个程序?

public class GridViewAdapter extends BaseAdapter {

    Context mContext;

    public GridViewAdapter(Context mContext){
        this.mContext = mContext;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            LayoutInflater inflater1 = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater1.inflate(R.layout.gv_item, null);
            holder.imageView = (GridImageView) convertView.findViewById(R.id.imageView);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        holder.imageView.setImageBitmap(Helper.getBitmapFromAssets((position+1)+"_thum", mContext));
        return convertView;
    }

    private static class Holder {
        GridImageView imageView;
    }


    @Override
    public int getCount() {
        return Constants.TOTAL_IMAGES;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}

我的Helper.getBitmapFromAssets正在关注。

   /**
     * Reads Images from Assets Folder
     * @param fileName
     * @return
     */
    public static Bitmap getBitmapFromAssets(String fileID, Context mContext) {
        AssetManager assetManager = mContext.getAssets();

        InputStream istr;
        Bitmap bitmap = null;
        try {
            istr = assetManager.open("images/"+fileID+".encrypted");
            bitmap = BitmapFactory.decodeStream(istr);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return bitmap;
    }

1 个答案:

答案 0 :(得分:2)

  • 创建ArrayList包含资产图像的路径(例如 文件夹名称/ 1.png)

AssetManager assetManager = getResources()。getAssets();

ArrayList listpath;

        try {

                String[] files = assetManager.list("foldername");
                listpath==new  ArrayList<String> ();
                for (String strImageName : files) {
                    pathAssets = "foldername" + File.separator+ strImageName;
                    listpath.add(pathAssets );

            }

- 呼叫适配器

    try {
            if (listBitmap!= null) {
            adp = new GridViewAdapter(this,R.layout.grid_item,listBitmap);
            gridView.setAdapter(adp);
           }
        } catch (Exception e) {
                    e.printStackTrace();
        }

适配器代码(使用UniversalImageLoder加载图像) Download jar file from here

import java.io.File;
    import java.util.ArrayList;

    import android.app.Service;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

    import com.nostra13.universalimageloader.core.DisplayImageOptions;
    import com.nostra13.universalimageloader.core.ImageLoader;
    import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

    public class  GridViewAdapter extends BaseAdapter {
        Context context;
        int layoutResourceId;
        private ArrayList<String> griRowItems;
        LayoutInflater vi;
        ImageLoader imageLoader;
        String[] imageUrls;
        ArrayList<String> imagePath;
        DisplayImageOptions options;
        ViewHolder holder = null;

        public GridViewAdapter(Context context,
                int layoutResourceId, ArrayList<String> mApps) {
            this.context = context;
            this.griRowItems = mApps;
            this.layoutResourceId = layoutResourceId;
            vi = (LayoutInflater) context
                    .getSystemService(Service.LAYOUT_INFLATER_SERVICE);

            imageLoader = ImageLoader.getInstance();
            this.imageLoader.init(ImageLoaderConfiguration.createDefault(context));

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = vi.inflate(R.layout.grid_item, parent,
                        false);
                holder = new ViewHolder();
            }

            holder.imageView = (ImageView) convertView.findViewById(R.id.ivImage);
            // holder.progressBar = (ProgressBar) convertView
            // .findViewById(R.id.progress);

            String tempStr = "assets://" +  griRowItems.get(position);
            lazyLoading(imageLoader, tempStr, holder.imageView,
                    options);
            return convertView;
        }


        @Override
        public int getCount() {
            return griRowItems.size();
        }

        @Override
        public Object getItem(int position) {
            return griRowItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        static class ViewHolder {
            ImageView imageView;
            // ProgressBar progressBar;
            TextView tvTitle;
            // DisplayImageOptions options;
        }
    }

将此方法放在适配器中以便顺利加载图像

public static void lazyLoading(ImageLoader imageLoader, String tempStr, ImageView imageView,DisplayImageOptions options) {
        imageLoader.displayImage(tempStr, imageView, options,
                new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String imageUri, View view) {
                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view,FailReason failReason) {
                    }

                    @Override
                    public void onLoadingComplete(String imageUri, View view,Bitmap loadedImage) {
                    }
                }, new ImageLoadingProgressListener() {
                    @Override
                    public void onProgressUpdate(String imageUri, View view,int current, int total) {
                    }
                });
    }