gpt4 book ai didi

android - 使用平滑滚动将图像从 Assets 加载到 GridView

转载 作者:行者123 更新时间:2023-11-29 21:15:39 27 4
gpt4 key购买 nike

我正在尝试使用以下适配器从 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;
}

最佳答案

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

AssetManager assetManager = getResources().getAssets();

ArrayList 列表路径;

        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;
}
}

将此方法放在adapter中,以便顺利加载图片

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) {
}
});
}

关于android - 使用平滑滚动将图像从 Assets 加载到 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21515022/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com