Code example for ImageView

Methods: getDrawablesetImageBitmap, startAnimation

0
        return true; 
    } 
 
    private BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
        if (imageView != null) {
            final Drawable drawable = imageView.getDrawable();
            if (drawable instanceof SpiceArrayAdapter.AsyncDrawable) {
                @SuppressWarnings("unchecked") 
                final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
                return asyncDrawable.getBitmapWorkerTask();
            } 
        } 
        return null; 
    } 
 
    private static Bitmap decodeSampledBitmapFromResource(String fileName,
        int reqWidth, int reqHeight) {
        // First decode with inJustDecodeBounds=true to check dimensions 
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(fileName, options);
 
        // Calculate inSampleSize 
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);
 
        // Decode bitmap with inSampleSize set 
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(fileName, options);
    } 
 
    private static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
        // Raw height and width of image 
        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;
    } 
 
    // ---------------------------- 
    // --- INNER CLASSES 
    // ---------------------------- 
 
    private class OctoImageRequestListener implements 
        RequestListener<InputStream> {
 
        private SpiceListItemView<T> spiceListItemView;
        private T octo;
        private ImageView thumbImageView;
        private String imageFileName;
 
        public OctoImageRequestListener(T octo,
            SpiceListItemView<T> spiceListItemView, String imageFileName) {
            this.octo = octo;
            this.spiceListItemView = spiceListItemView;
            this.thumbImageView = spiceListItemView.getImageView();
            this.imageFileName = imageFileName;
        } 
 
        @Override 
        public void onRequestFailure(SpiceException spiceException) {
            Ln.e(SpiceListItemView.class.getName(), "Unable to retrive image",
                spiceException);
            thumbImageView.setImageDrawable(defaultDrawable);
        } 
 
        @Override 
        public void onRequestSuccess(InputStream result) {
            freshDrawableSet.add(this.octo);
            if (this.octo.equals(spiceListItemView.getData())) {
                loadBitmapAsynchronously(octo, thumbImageView, imageFileName);
            } 
        } 
    } 
 
    protected class ThumbnailAsynTask extends AsyncTask<Object, Void, Boolean> {
 
        private T octo;
        private SpiceListItemView<T> spiceListItemView;
        private String tempThumbnailImageFileName = "";
        private BigBinaryRequest request;
 
        public ThumbnailAsynTask(BigBinaryRequest request) {
            this.request = request;
        } 
 
        @SuppressWarnings("unchecked") 
        @Override 
        protected Boolean doInBackground(Object... params) {
            octo = (T) params[0];
            spiceListItemView = (SpiceListItemView<T>) params[1];
 
            File tempThumbnailImageFile = request.getCacheFile();
            tempThumbnailImageFileName = tempThumbnailImageFile
                .getAbsolutePath();
            Ln.d("Filename : " + tempThumbnailImageFileName);
 
            if (!tempThumbnailImageFile.exists()) {
                if (isNetworkFetchingAllowed) {
                    OctoImageRequestListener octoImageRequestListener = new OctoImageRequestListener(
                        octo, spiceListItemView, tempThumbnailImageFileName);
                    spiceManagerBinary.execute(request,
                        "THUMB_IMAGE_" + octo.hashCode(),
                        DurationInMillis.NEVER, octoImageRequestListener);
                } 
                return false; 
            } 
            return true; 
        } 
 
        @Override 
        protected void onPostExecute(Boolean isImageAvailableInCache) {
            if (isImageAvailableInCache) {
                loadBitmapAsynchronously(octo,
                    spiceListItemView.getImageView(),
                    tempThumbnailImageFileName);
            } else { 
                spiceListItemView.getImageView().setImageDrawable(
                    defaultDrawable);
            } 
        } 
    } 
 
    private class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
        private final WeakReference<ImageView> imageViewReference;
        String fileName = "";
        private T data;
        private Animation animation;
 
        public BitmapWorkerTask(ImageView imageView, T data) {
            // Use a WeakReference to ensure the ImageView can be garbage 
            // collected 
            imageViewReference = new WeakReference<ImageView>(imageView);
            this.data = data;
        } 
 
        // Decode image in background. 
        @Override 
        protected Bitmap doInBackground(String... params) {
            animation = AnimationUtils.loadAnimation(getContext(),
                android.R.anim.fade_in);
            animation.setDuration(getContext().getResources().getInteger(
                android.R.integer.config_mediumAnimTime));
            fileName = params[0];
            int width = Integer.valueOf(params[1]);
            int height = Integer.valueOf(params[2]);
            return decodeSampledBitmapFromResource(fileName, width, height);
        } 
 
        // Once complete, see if ImageView is still around and set bitmap. 
        @Override 
        protected void onPostExecute(Bitmap bitmap) {
            if (isCancelled()) {
                if (data.toString().equals("JFA")) {
                    Ln.d(data.toString() + " : cancel decoding bitmap");
                } 
                bitmap = null;
            } 
 
            if (imageViewReference != null && bitmap != null) {
                if (data.toString().equals("JFA")) {
                    Ln.d(data.toString() + " : bitmapworkertask");
                } 
                final ImageView imageView = imageViewReference.get();
                final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
                if (this == bitmapWorkerTask && imageView != null) {
                    if (freshDrawableSet.contains(data)) {
                        freshDrawableSet.remove(data);
                        imageView.startAnimation(animation);
                    } 
                    imageView.setImageBitmap(bitmap);
                    // no used anymore. 
                    // imageView.setTag( null ); 
                } 
            } 
        }