gpt4 book ai didi

android - StaggeredGridView 在滚动时卡在中间

转载 作者:行者123 更新时间:2023-11-30 02:04:23 25 4
gpt4 key购买 nike

我正在我的应用程序中显示设备存储中的所有图像。我遇到的问题是当我滚动时它不流畅。它卡在中间。我正在使用 etsy/AndroidStaggeredGrid并使用下面的代码。我尝试将任务放在异步后台任务中,但我仍然遇到这个问题。为什么卡在中间,我该如何解决?

画廊 fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

final View rootView = inflater.inflate(R.layout.fragment_gallery, container, false);
galleries=new ArrayList<Gallery>();
((ActionBarActivity) getActivity()).getSupportActionBar().show();
gridview = (StaggeredGridView)rootView.findViewById(R.id.gridcamera);
progressBar=(ProgressBar)rootView.findViewById(R.id.progressBar);
gallerylist=new CameraGalleryAdapter(getActivity().getApplicationContext(), R.layout.gallery_grid,galleries);
gridview.setAdapter(gallerylist);

AsyncTaskRunner runner = new AsyncTaskRunner();

runner.execute();

return rootView;
}

private class AsyncTaskRunner extends AsyncTask<String, String, Void> {

private String resp;

@Override
protected Void doInBackground(String... params)
{

int count,count2,count3,count4;

((CameraActivity)getActivity()).setOnBackPressedListener(new BaseBackPressedListener(getActivity(),true));

final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID,MediaStore.Video.Media.DATA,
};
final String orderBy = MediaStore.Images.Media._ID;
Cursor cursor = getActivity().getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);

count = cursor.getCount();
Cursor cursor2 = getActivity().getContentResolver().query(
MediaStore.Images.Media.INTERNAL_CONTENT_URI, columns, null,
null, orderBy);
count2 = cursor2.getCount();

Cursor cursor3 = getActivity().getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
count3 = cursor3.getCount();
Cursor cursor4 = getActivity().getContentResolver().query(
MediaStore.Video.Media.INTERNAL_CONTENT_URI, columns, null,
null, orderBy);
count4 = cursor4.getCount();


for (int i = 0; i < count; i++)
{
cursor.moveToPosition(i);
int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);

Gallery ga=new Gallery();
ga.setImageUrl(cursor.getString(dataColumnIndex));
ga.setIsImage(true);
galleries.add(ga);
}
for (int i = 0; i < count2; i++)
{
cursor2.moveToPosition(i);
int dataColumnIndex = cursor2.getColumnIndex(MediaStore.Images.Media.DATA);
Gallery ga=new Gallery();;
ga.setImageUrl(cursor2.getString(dataColumnIndex));
ga.setIsImage(true);
galleries.add(ga);

}

for (int i = 0; i < count3; i++)
{


cursor3.moveToPosition(i);

int dataColumnIndex = cursor3.getColumnIndex(MediaStore.Video.Media.DATA);

Bitmap thumb = ThumbnailUtils.createVideoThumbnail(cursor3.getString(dataColumnIndex),
MediaStore.Images.Thumbnails.MINI_KIND);

Gallery ga=new Gallery();

ga.setVideoThumbnail(thumb);

ga.setVideoUrl(cursor3.getString(dataColumnIndex));

ga.setIsImage(false);
galleries.add(ga);


}
for (int i = 0; i < count4; i++)
{


cursor4.moveToPosition(i);

int dataColumnIndex = cursor4.getColumnIndex(MediaStore.Video.Media.DATA);

Bitmap thumb = ThumbnailUtils.createVideoThumbnail(cursor4.getString(dataColumnIndex),MediaStore.Images.Thumbnails.MINI_KIND);

Gallery ga = new Gallery();

ga.setVideoThumbnail(thumb);

ga.setVideoUrl(cursor4.getString(dataColumnIndex));

ga.setIsImage(false);
galleries.add(ga);


}
return null;
}



@Override
protected void onPreExecute() {

}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
gallerylist.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
}

@Override
protected void onProgressUpdate(String... text) {

}
}

相机画廊适配器

public class CameraGalleryAdapter extends BaseAdapter {
ArrayList<Gallery> itemList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
Bitmap bm;
public CameraGalleryAdapter(Context context, int resource, ArrayList<Gallery> objects) {

vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
itemList = objects;

}


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

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

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

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

View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imageview = (ImageView) v.findViewById(R.id.ivImageg);
holder.playicon= (ImageView) v.findViewById(R.id.galleryplay);

v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}

if(itemList.get(position).getIsImage())
{

holder.playicon.setVisibility(View.INVISIBLE);

AsyncTaskRunner runner = new AsyncTaskRunner();

runner.execute(itemList.get(position).getImageUrl());
bm= null ;
try {
bm = runner.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
holder.imageview.setImageBitmap(bm);
}
else
{
holder.playicon.setVisibility(View.VISIBLE);
holder.imageview.setImageBitmap(itemList.get(position).getVideoThumbnail());
}
return v;

}

static class ViewHolder {
public ImageView imageview;
public ImageView playicon;

}
private class AsyncTaskRunner extends AsyncTask<String, String,Bitmap>
{

private String resp;

@Override
protected Bitmap doInBackground(String... params)
{
resp=params[0];
Bitmap bm = decodeSampledBitmapFromUri(resp, 220, 220);
return bm;
}



@Override
protected void onPreExecute() {

}

@Override
protected void onPostExecute(Bitmap aVoid) {
super.onPostExecute(aVoid);


}

@Override
protected void onProgressUpdate(String... text) {

}
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {

Bitmap bm = null;

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);


options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);


options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);

return bm;
}

public int calculateInSampleSize(

BitmapFactory.Options options, int reqWidth, int reqHeight)
{

final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}

return inSampleSize;
}

}

最佳答案

这是一个常见问题 bcoz 每次滚动时都会在 View 中加载大图像大小....要解决此问题,您可以尝试延迟加载 imgae 技术.. http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription&aid=112&aaid=134

关于android - StaggeredGridView 在滚动时卡在中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30937423/

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