我在我的应用程序中使用Gridview来显示Sdcard上的文件夹中的图像... 我的问题是GridView中的滚动,它不像Gallery App那样流畅。 这是适配器代码 -
public class GridViewAdapter extends BaseAdapter {
// Declare variables
private Activity activity;
private String[] filepath;
private String[] filename;
private static LayoutInflater inflater = null;
public GridViewAdapter(Activity a, String[] fpath, String[] fname) {
activity = a;
filepath = fpath;
filename = fname;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return filepath.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.gridview_item, null);
// Locate the ImageView in gridview_item.xml
ImageView img = (ImageView) vi.findViewById(R.id.image);
// Set file name to the TextView followed by the position
TextView txt = (TextView) vi.findViewById(R.id.name);
// Decode the filepath with BitmapFactory followed by the position
Bitmap bmp = BitmapFactory.decodeFile(filepath[position]);
// Set the decoded bitmap into ImageView
img.setImageBitmap(bmp);
txt.setText(filename[position]);
return vi;
}
}
如何解决此问题并使滚动顺利进行?
答案 0 :(得分:1)
您需要在单独的线程中执行BitmapFactory.decodeFile
,例如本教程:
https://developer.android.com/training/displaying-bitmaps/process-bitmap.html
我建议您使用缓存,每次用户滚动时都不重新加载图像: https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
答案 1 :(得分:1)
你可以这样做。
RecyclerView
代替GridLayoutManager
代替GridView
,这是更优化的控制。例如:http://www.android-examples.com/android-recyclerview-with-gridview-gridlayoutmanager/ 其他参考:http://www.technotalkative.com/android-select-multiple-photos-from-gallery/
答案 2 :(得分:0)
BitmapFactory.decodeFile是一次又一次在ui线程上执行的繁重操作。你使用了一些缓存库。
您可以使用某些图片加载库,例如,Universal image loader或Glide。
请使用持有人试试。
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);