seattle-java-401d1

CF Async Tasks and Downloading Images

Reading

References

This class is all about creating images, picking images, and displaying images. We’ll use the camera, the gallery, and get images from the internet. Our ultimate goal is to display images in a ListView so we can scroll through them smoothly and efficiently.

Topics

ImageView ScaleTypes

This app shows off all the different possible values for the scaleType attribute on an ImageView.

Notice that setting adjustViewBounds to true eliminates extra padding that appears above or below images for certain scale types. (Note: the app actually restarts when adjustViewBounds is toggled to false because it must reset the ImageView to a clean slate.)

For a quick visual reference refer to this webpage which shows off the scale types similarly: https://robots.thoughtbot.com/android-imageview-scaletype-a-visual-guide

And, here’s a screenshot of that website:

examples of scale types

Here’s how the Android docs describe the scale types: https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

Enum values

ImageView.ScaleType  CENTER

Center the image in the view, but perform no scaling. 

ImageView.ScaleType  CENTER_CROP

Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). 

ImageView.ScaleType  CENTER_INSIDE

Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). 

ImageView.ScaleType  FIT_CENTER

Scale the image using CENTER

ImageView.ScaleType  FIT_END

Scale the image using END

ImageView.ScaleType  FIT_START

Scale the image using START

ImageView.ScaleType  FIT_XY

Scale the image using FILL

ImageView.ScaleType  MATRIX

Scale using the image matrix when drawing. 

Here’s what the app looks like

square image in app tall image in app wide image in app

Download Image Task

public class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> {
    private static Map<String, Bitmap> cache = new HashMap<>();

    private Context mContext;
    private String mUrl;
    private ImageView mView;

    public DownloadImageTask(Context context, String url, ImageView view) {
        mContext = context;
        mUrl = url;
        mView = view;
    }

    @Override
    protected Bitmap doInBackground(Void... voids) {
        try {
            if (cache.containsKey(mUrl)) {
                return cache.get(mUrl);
            }
            InputStream stream = new URL(mUrl).openConnection().getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(stream);

            Thread.sleep(1500);
            cache.put(mUrl, bitmap);

            return bitmap;
        } catch (IOException e) {
            return null;
        } catch (InterruptedException e) {

        }

        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (bitmap != null) {
            super.onPostExecute(bitmap);
            mView.setImageBitmap(bitmap);
        }
    }
}