Example usage for android.widget ImageView setImageDrawable

List of usage examples for android.widget ImageView setImageDrawable

Introduction

In this page you can find the example usage for android.widget ImageView setImageDrawable.

Prototype

public void setImageDrawable(@Nullable Drawable drawable) 

Source Link

Document

Sets a drawable as the content of this ImageView.

Usage

From source file:com.dm.wallpaper.board.adapters.LatestAdapter.java

private void setFavorite(@NonNull ImageView imageView, @ColorInt int color, int position, boolean animate) {
    if (position < 0 || position > mWallpapers.size())
        return;/*from   w ww . j a va  2 s.c  o  m*/

    boolean isFavorite = mWallpapers.get(position).isFavorite();

    if (animate) {
        AnimationHelper.show(imageView).interpolator(new LinearOutSlowInInterpolator())
                .callback(new AnimationHelper.Callback() {
                    @Override
                    public void onAnimationStart() {
                        imageView.setImageDrawable(DrawableHelper.getTintedDrawable(mContext,
                                isFavorite ? R.drawable.ic_toolbar_love : R.drawable.ic_toolbar_unlove, color));
                    }

                    @Override
                    public void onAnimationEnd() {

                    }
                }).start();
        return;
    }

    imageView.setImageDrawable(DrawableHelper.getTintedDrawable(mContext,
            isFavorite ? R.drawable.ic_toolbar_love : R.drawable.ic_toolbar_unlove, color));
}

From source file:com.krava.vkmessenger.domain.image.ImageWorker.java

/**
 * Load an image specified by the data parameter into an ImageView (override
 * {@link ImageWorker#processBitmap(Object)} to define the processing logic). A memory and
 * disk cache will be used if an {@link ImageCache} has been added using
 * {@link ImageWorker#addImageCache(android.support.v4.app.FragmentManager, ImageCache.ImageCacheParams)}. If the
 * image is found in the memory cache, it is set immediately, otherwise an {@link AsyncTask}
 * will be created to asynchronously load the bitmap.
 *
 * @param data The URL of the image to download.
 * @param imageView The ImageView to bind the downloaded image to.
 * @param listener A listener that will be called back once the image has been loaded.
 *//* w w  w.ja  v  a 2 s. co m*/
public void loadImage(Object data, ImageView imageView, OnImageLoadedListener listener) {
    if (data == null) {
        return;
    }

    BitmapDrawable value = null;

    if (mImageCache != null) {
        value = mImageCache.getBitmapFromMemCache(String.valueOf(data));
    }

    if (value != null) {
        // Bitmap found in memory cache
        imageView.setImageDrawable(value);
        if (listener != null) {
            listener.onImageLoaded(true);
        }
    } else if (cancelPotentialWork(data, imageView)) {
        final BitmapWorkerTask task = new BitmapWorkerTask(data, imageView, listener);
        final AsyncDrawable asyncDrawable = new AsyncDrawable(mResources, mLoadingBitmap, task);
        imageView.setImageDrawable(asyncDrawable);

        // NOTE: This uses a custom version of AsyncTask that has been pulled from the
        // framework and slightly modified. Refer to the docs at the top of the class
        // for more info on what was changed.
        task.executeOnExecutor(DUAL_THREAD_EXECUTOR);
    }
}

From source file:cn.edu.szjm.support.images.remote.RemoteImageLoader.java

/**
 * Triggers the image loader for the given image and view. The image loading will be performed
 * concurrently to the UI main thread, using a fixed size thread pool. The loaded image will be
 * posted back to the given ImageView upon completion. While waiting, the dummyDrawable is
 * shown.// ww  w .  j a v  a 2 s .  c o m
 * 
 * @param imageUrl
 *            the URL of the image to download
 * @param imageView
 *            the ImageView which should be updated with the new image
 * @param dummyDrawable
 *            the Drawable to be shown while the image is being downloaded.
 * @param handler
 *            the handler that will process the bitmap after completion
 */
public void loadImage(String imageUrl, ImageView imageView, Drawable dummyDrawable,
        RemoteImageLoaderHandler handler) {
    this.imageUrl = imageUrl;
    this.handler = handler;
    if (imageView != null) {
        if (imageUrl == null) {
            // In a ListView views are reused, so we must be sure to remove the tag that could
            // have been set to the ImageView to prevent that the wrong image is set.
            imageView.setTag(null);
            if (dummyDrawable != null) {
                imageView.setImageDrawable(dummyDrawable);
            }
            return;
        }
        String oldImageUrl = (String) imageView.getTag();
        if (imageUrl.equals(oldImageUrl)) {
            // nothing to do
            return;
        } else {
            if (dummyDrawable != null) {
                // Set the dummy image while waiting for the actual image to be downloaded.
                imageView.setImageDrawable(dummyDrawable);
            }
            imageView.setTag(imageUrl);
        }
    }

    ImageCache cache = SharedImageCache.getSharedImageCache();
    if (usingCache) {
        if (cache != null && cache.containsKeyInMemory(imageUrl)) {
            // do not go through message passing, handle directly instead
            handler.handleImageLoaded(cache.getBitmap(imageUrl), null);
            return;
        }
    }
    task.execute();
}

From source file:com.android.aft.AFCoreTools.ImageDownloader.java

/**
 * Same as download but the image is always downloaded and the cache is not
 * used. Kept private at the moment as its interest is not clear.
 *///w w w.ja v  a  2s .com
private void forceDownload(String url, ImageView imageView, int reqWidth, int reqHeight,
        ImageDownloaderListener listener, Animation animation) {
    // State sanity: url is guaranteed to never be null in
    // DownloadedDrawable and cache keys.
    if (url == null) {
        imageView.setImageDrawable(null);
        return;
    }

    if (cancelPotentialDownload(url, imageView)) {
        BitmapDownloaderTask task = new BitmapDownloaderTask(imageView, listener, reqWidth, reqHeight);

        Bitmap bg = null;
        if (imageView.getDrawable() instanceof BitmapDrawable)
            bg = ((BitmapDrawable) imageView.getDrawable()).getBitmap();

        DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task, bg);

        imageView.setImageDrawable(downloadedDrawable);
        if (animation != null) {
            imageView.setAnimation(animation);
        }
        imageView.setMinimumHeight(156);

        task.execute(Mode.Parallel, url);
    }
}

From source file:ar.com.lapotoca.resiliencia.gallery.util.ImageWorker.java

/**
 * Load an image specified by the data parameter into an ImageView (override
 * {@link ImageWorker#processBitmap(Object)} to define the processing logic). A memory and
 * disk cache will be used if an {@link ImageCache} has been added using
 * {@link ImageWorker#addImageCache(android.support.v4.app.FragmentManager, ImageCache.ImageCacheParams)}. If the
 * image is found in the memory cache, it is set immediately, otherwise an {@link AsyncTask}
 * will be created to asynchronously load the bitmap.
 *
 * @param data The URL of the image to download.
 * @param imageView The ImageView to bind the downloaded image to.
 * @param listener A listener that will be called back once the image has been loaded.
 *///from   w  ww  .j  a  v  a  2s . c  o m
public void loadImage(Object data, boolean localSource, ImageView imageView, OnImageLoadedListener listener) {
    if (data == null) {
        return;
    }

    BitmapDrawable value = null;

    if (mImageCache != null) {
        value = mImageCache.getBitmapFromMemCache(String.valueOf(data));
    }

    if (value != null) {
        // Bitmap found in memory cache
        imageView.setImageDrawable(value);
        if (listener != null) {
            listener.onImageLoaded(true);
        }
    } else {
        if (localSource) {
            Drawable drawable = null;
            try {
                drawable = Drawable.createFromStream(mAssets.open(data.toString()), null);
                imageView.setImageDrawable(drawable);
            } catch (Exception e) {
                Log.e(TAG, "No se pudo cargar la imagen", e);
            }
        } else if (cancelPotentialWork(data, imageView)) {
            //BEGIN_INCLUDE(execute_background_task)
            final BitmapWorkerTask task = new BitmapWorkerTask(data, imageView, listener);
            final AsyncDrawable asyncDrawable = new AsyncDrawable(mResources, mLoadingBitmap, task);
            imageView.setImageDrawable(asyncDrawable);

            // NOTE: This uses a custom version of AsyncTask that has been pulled from the
            // framework and slightly modified. Refer to the docs at the top of the class
            // for more info on what was changed.
            task.executeOnExecutor(AsyncTask.DUAL_THREAD_EXECUTOR);
            //END_INCLUDE(execute_background_task)
        }
    }
}

From source file:com.fastbootmobile.encore.app.fragments.AlbumViewFragment.java

private void setupHeader(LayoutInflater inflater) {
    // Load the header
    View headerView = inflater.inflate(R.layout.header_listview_songs, mListView, false);
    mIvHero = (ImageView) headerView.findViewById(R.id.ivHero);
    mTvAlbumName = (TextView) headerView.findViewById(R.id.tvAlbumName);
    mTvAlbumName.setBackgroundColor(0xBBFFFFFF & mBackgroundColor);

    if (mAlbum != null) {
        mTvAlbumName.setText(mAlbum.getName());
    }/*w  w w.  j av a2s . c  o  m*/

    if (!Utils.hasLollipop()) {
        // Since we don't have the transition animation, make sure everything is visible
        mTvAlbumName.setVisibility(View.VISIBLE);
        mTvAlbumName.setAlpha(0.0f);
        mTvAlbumName.animate().alpha(1.0f).setDuration(AlbumActivity.BACK_DELAY).start();
    }

    // Hide download button by default
    headerView.findViewById(R.id.cpbOffline).setVisibility(View.GONE);

    mPlayFab = (FloatingActionButton) headerView.findViewById(R.id.fabPlay);

    // Set source logo
    ImageView ivSource = (ImageView) headerView.findViewById(R.id.ivSourceLogo);
    if (mAlbum != null) {
        mLogoBitmap = PluginsLookup.getDefault().getCachedLogo(getResources(), mAlbum);
        ivSource.setImageDrawable(mLogoBitmap);
    }

    // Set the FAB animated drawable
    mFabDrawable = new PlayPauseDrawable(getResources(), 1);
    mFabDrawable.setShape(PlayPauseDrawable.SHAPE_PLAY);
    mFabDrawable.setYOffset(6);

    mPlayFab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mFabDrawable.getCurrentShape() == PlayPauseDrawable.SHAPE_PLAY) {
                if (mFabShouldResume) {
                    PlaybackProxy.play();
                    mFabDrawable.setShape(PlayPauseDrawable.SHAPE_PAUSE);
                    mBarDrawable.setShape(PlayPauseDrawable.SHAPE_PAUSE);
                } else {
                    PlaybackProxy.playAlbum(mAlbum);
                }

                if (Utils.hasLollipop()) {
                    // No Lollipop, no cool animation!
                    showMaterialReelBar(mPlayFab);
                }
            } else {
                mFabShouldResume = true;
                PlaybackProxy.pause();
                mFabDrawable.setShape(PlayPauseDrawable.SHAPE_PLAY);
                mBarDrawable.setShape(PlayPauseDrawable.SHAPE_PLAY);
                hideMaterialReelBar(mPlayFab);
            }
        }
    });
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            showFab(false, false);
        }
    });
    mPlayFab.setImageDrawable(mFabDrawable);

    if (mHeroImage != null) {
        mIvHero.setImageBitmap(mHeroImage);
    } else {
        mIvHero.setImageResource(R.drawable.album_placeholder);
    }

    // Set the header view and adapter
    mListView.addParallaxedHeaderView(headerView);
    mListView.setAdapter(mAdapter);

    AlphaAnimation anim = new AlphaAnimation(0.f, 1.f);
    anim.setDuration(200);
    mListView.setLayoutAnimation(new LayoutAnimationController(anim));

    setupMaterialReelBar(headerView, mReelFabClickListener);
}

From source file:com.example.gtuandroid.component.ImageDownloader.java

/**
 * Same as download but the image is always downloaded and the cache is not
 * used. Kept private at the moment as its interest is not clear.
 *///from   w w  w . jav a 2 s  .co  m
private void forceDownload(String url, final ImageView imageView) {
    // State sanity: url is guaranteed to never be null in
    // DownloadedDrawable and cache keys.
    if (url == null) {
        // imageView.setImageDrawable(null);
        handler.post(new Runnable() {
            @Override
            public void run() {
                imageView.setImageDrawable(null);
            }
        });
        return;
    }

    if (cancelPotentialDownload(url, imageView)) {
        switch (mode) {
        case NO_ASYNC_TASK:
            final Bitmap bitmap = downloadBitmap(url);
            addBitmapToCache(url, bitmap);
            // imageView.setImageBitmap(bitmap);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    imageView.setImageBitmap(bitmap);
                }
            });
            break;

        case NO_DOWNLOADED_DRAWABLE:
            // imageView.setMinimumHeight(156);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    imageView.setMinimumHeight(156);
                }
            });
            BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
            task.execute(url);
            break;

        case CORRECT:
            task = new BitmapDownloaderTask(imageView);
            final DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
            // imageView.setImageDrawable(downloadedDrawable);
            // imageView.setMinimumHeight(156);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    imageView.setImageDrawable(downloadedDrawable);
                    imageView.setMinimumHeight(156);
                }
            });
            task.execute(url);
            break;
        }
    }
}

From source file:com.atgc.cotton.util.ImageWorker.java

/**
 * Called when the processing is complete and the final drawable should be
 * set on the ImageView.//  w w  w .ja v  a 2s  .c om
 *
 * @param imageView
 * @param drawable
 */
private void setImageDrawable(ImageView imageView, Drawable drawable) {
    if (mFadeInBitmap) {
        // Transition drawable with a transparent drawable and the final drawable
        final TransitionDrawable td = new TransitionDrawable(
                new Drawable[] { new ColorDrawable(mResources.getColor(R.color.transparent)), drawable });
        // Set background to loading bitmap
        imageView.setBackgroundDrawable(new BitmapDrawable(mResources, mLoadingBitmap));

        imageView.setImageDrawable(td);
        td.startTransition(FADE_IN_TIME);
    } else {
        imageView.setImageDrawable(drawable);
    }
}

From source file:android.support.v7.widget.SuggestionsAdapter.java

/**
 * Sets the drawable in an image view, makes sure the view is only visible if there
 * is a drawable./*www.j ava  2  s . com*/
 */
private void setViewDrawable(ImageView v, Drawable drawable, int nullVisibility) {
    // Set the icon even if the drawable is null, since we need to clear any
    // previous icon.
    v.setImageDrawable(drawable);

    if (drawable == null) {
        v.setVisibility(nullVisibility);
    } else {
        v.setVisibility(View.VISIBLE);

        // This is a hack to get any animated drawables (like a 'working' spinner)
        // to animate. You have to setVisible true on an AnimationDrawable to get
        // it to start animating, but it must first have been false or else the
        // call to setVisible will be ineffective. We need to clear up the story
        // about animated drawables in the future, see http://b/1878430.
        drawable.setVisible(false, false);
        drawable.setVisible(true, false);
    }
}

From source file:com.fivehundredpxdemo.android.storage.ImageWorker.java

/**
 * Called when the processing is complete and the final bitmap should be set on the ImageView.
 *
 * @param imageView//w w  w.  j  av a2 s  .  c  o m
 * @param bitmap
 */
private void setImageBitmap(ImageView imageView, Bitmap bitmap) {
    if (mFadeInBitmap) {
        // Use TransitionDrawable to fade in
        final TransitionDrawable td = new TransitionDrawable(new Drawable[] {
                new ColorDrawable(android.R.color.transparent), new BitmapDrawable(mResources, bitmap) });
        //noinspection deprecation
        imageView.setBackgroundDrawable(imageView.getDrawable());
        imageView.setImageDrawable(td);
        td.startTransition(FADE_IN_TIME);
    } else {
        imageView.setImageBitmap(bitmap);
    }
}