Android Open Source - droidling Bitmap Loader Task






From Project

Back to project page droidling.

License

The source code is released under:

Copyright (c) 2012 Keith Trnka Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Softwa...

If you think the Android project droidling listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.ktrnka.droidling.helpers;
// w w  w.j ava  2s. com
import java.io.IOException;
import java.lang.ref.WeakReference;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.widget.ImageView;

import com.github.ktrnka.droidling.ExtendedApplication;

/**
 * Helper class load and scale images async. Based on
 * http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
 */
public class BitmapLoaderTask extends AsyncTask<Uri, Void, Bitmap> {
    private static final String RESOURCE_SCHEME = "droidlingresources";
    private final WeakReference<ImageView> imageViewReference;
    private Uri imageUri;
    private int widthPixels;
    private int heightPixels;

    // TODO: Try to remove this circular dep between packages
    private ExtendedApplication application;

    public BitmapLoaderTask(ImageView imageView, int widthPixels, int heightPixels,
            ExtendedApplication application) {
        imageViewReference = new WeakReference<ImageView>(imageView);
        this.widthPixels = widthPixels;
        this.heightPixels = heightPixels;
        this.application = application;
    }

    @Override
    protected Bitmap doInBackground(Uri... params) {
        imageUri = params[0];

        if (imageViewReference != null && application != null) {
            ImageView imageView = imageViewReference.get();
            try {
                if (imageUri.getScheme().equals(RESOURCE_SCHEME)) {
                    return application.loadBitmapFromResources(imageView.getContext(),
                            Integer.parseInt(imageUri.getSchemeSpecificPart()), widthPixels,
                            heightPixels);
                }
                else {
                    return application.loadBitmapFromUri(imageView.getContext(), imageUri,
                            widthPixels, heightPixels);
                }
            } catch (IOException e) {
                return null;
            }
        }

        return null;
    }

    /**
     * Check if we should avoid potential work. This function should be run on
     * the UI thread for synchronization.
     * 
     * @param imageView the ImageView we're going to populate
     * @param imageUri the Uri of the image we're going to populate
     * @return true if the potential work should be avoided, false otherwise
     */
    public static boolean cancelPotentialWork(ImageView imageView, Uri imageUri) {
        if (imageView == null)
            return false;

        AsyncDrawable drawable;
        Drawable plainDrawable = imageView.getDrawable();
        if (plainDrawable instanceof AsyncDrawable)
            drawable = (AsyncDrawable) plainDrawable;
        else
            return false;

        BitmapLoaderTask task = drawable.getBitmapLoaderTask();
        if (task == null)
            return false;

        // already loading this data
        if (task.imageUri != null && task.imageUri.equals(imageUri)) {
            return true;
        }
        // loading something else - kill it
        else {
            task.cancel(true);
            return false;
        }
    }

    public static Uri packIntoUri(int drawableId) {
        return Uri.fromParts(RESOURCE_SCHEME, String.valueOf(drawableId), null);
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (!isCancelled() && imageViewReference != null && bitmap != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null)
                imageView.setImageBitmap(bitmap);
        }
    }

}




Java Source Code List

com.github.ktrnka.droidling.AboutActivity.java
com.github.ktrnka.droidling.AboutInterpersonalActivity.java
com.github.ktrnka.droidling.AboutLangIDActivity.java
com.github.ktrnka.droidling.AboutPersonalActivity.java
com.github.ktrnka.droidling.CorpusStats.java
com.github.ktrnka.droidling.DateDistribution.java
com.github.ktrnka.droidling.DiagnosticActivity.java
com.github.ktrnka.droidling.ExtendedApplication.java
com.github.ktrnka.droidling.GraphCard.java
com.github.ktrnka.droidling.ImageAdapter.java
com.github.ktrnka.droidling.InterpersonalActivity.java
com.github.ktrnka.droidling.InterpersonalCard.java
com.github.ktrnka.droidling.InterpersonalSingleStats.java
com.github.ktrnka.droidling.InterpersonalStats.java
com.github.ktrnka.droidling.LIDStats.java
com.github.ktrnka.droidling.LanguageIdentificationActivity.java
com.github.ktrnka.droidling.LanguageIdentifier.java
com.github.ktrnka.droidling.MainActivity.java
com.github.ktrnka.droidling.PersonalActivity.java
com.github.ktrnka.droidling.PersonalStats.java
com.github.ktrnka.droidling.RefreshableActivity.java
com.github.ktrnka.droidling.ShareableCard.java
com.github.ktrnka.droidling.Sms.java
com.github.ktrnka.droidling.Tokenizer.java
com.github.ktrnka.droidling.WordDistribution.java
com.github.ktrnka.droidling.helpers.AsyncDrawable.java
com.github.ktrnka.droidling.helpers.BitmapLoaderTask.java
com.github.ktrnka.droidling.helpers.Util.java