在Asynctask中加载GridView位图

时间:2018-07-10 10:20:46

标签: android performance video gridview android-asynctask

是否可以在Asynctask中加载GridView的缩略图,以防止在滚动GridView时出现滞后?

那是我的Asynctask的代码:

@SuppressLint("StaticFieldLeak")
public class AsyncTaskLoadFiles2 extends AsyncTask<Void, String, Void> {

    File targetDirector;


    public AsyncTaskLoadFiles2(ImageAdapter2 adapter) {
        myTaskAdapter2 = adapter;
    }

    @Override
    protected void onPreExecute() {


        String targetPath = "/sdcard/Android/data/de.myapps.gridtest/files/Download/.Videos";
        targetDirector = new File(targetPath);
        myTaskAdapter2.clear2();

        progressDialog = new ProgressDialog(getContext());
        progressDialog.setMessage("Loading Videos, please wait...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
        progressDialog.show();


        super.onPreExecute();
    }

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

        File[] files = targetDirector.listFiles();
        for (File file : files) {
            publishProgress(file.getAbsolutePath());

            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (isCancelled()) break;
        }

        return null;
    }

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

        myTaskAdapter2.add2(values[0]);


        super.onProgressUpdate(values);
    }


    @Override
    protected void onPostExecute(Void result) {

        myTaskAdapter2.notifyDataSetChanged();

        progressDialog.dismiss();

        super.onPostExecute(result);
    }

}

这是我的ImageAdapter的代码:

public class ImageAdapter2 extends BaseAdapter {

    private Context mContext;
    ArrayList<String> itemList = new ArrayList<String>();

    public ImageAdapter2(Context c) {
        mContext = c;
    }

    void add2(String path) {
        itemList.add(path);
    }

    void clear2() {
        itemList.clear();
    }

    void remove2(int index) {
        itemList.remove(index);
    }


    public String getPath(int position) {
        return itemList.get(position);
    }

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

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @SuppressLint("ResourceType")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) { 
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 420));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imageView = (ImageView) convertView;
        }

        thumbnail = ThumbnailUtils.createVideoThumbnail(getPath(position),
                MediaStore.Images.Thumbnails.MICRO_KIND);

            // Set the decoded bitmap into ImageView
            imageView.setImageBitmap(thumbnail);



        return imageView;
    }

}

所以我想知道的是是否有可能在Asynctask中加载thumbnail位图,因为使用此代码,应用程序会滞后。 (我正在使用TabbedActivity,所以所有这些代码都是Fragment的一部分)

1 个答案:

答案 0 :(得分:1)

看看

https://developer.android.com/topic/performance/graphics/cache-bitmap

BitmapWorkerTask task = new BitmapWorkerTask(mImageView);
task.execute(resId);

您可以创建一个AsyncTask,将图像加载到ImageView中

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
    ...
    // Decode image in background.
    @Override
    protected Bitmap doInBackground(Integer... params) {
        final Bitmap bitmap = decodeSampledBitmapFromResource(getResources(), params[0], 100, 100));
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
         if(result != null) {
             myImgView.setImageBitmap(result);
         }
    }
}

您只需在适配器的BitmapWorkerTask.Execute方法中使用构造函数中的参数调用getView()

BitmapWorkerTask task = new BitmapWorkerTask(imageView, filePath);

构造器

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
    private ImageView imageView;
    private String filePath;

    public BitmapWorkerTask(ImageView imageView, String filePath) {
        this.imageView = imageView;
        this.filePath = filePath;
    }
    ....
    //Get your bitmap
    //Set Bitmap onPostExecute