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.android.contacts.common.ContactPhotoManager.java

@Override
public void removePhoto(ImageView view) {
    view.setImageDrawable(null);
    mPendingRequests.remove(view);
}

From source file:com.haipeng.libraryforandroid.cacheormemory.imageload.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 set using {@link ImageWorker#setImageCache(ImageCache)}. 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.
 * /* ww w.j  av  a  2s  .co m*/
 * @param data
 *            The URL of the image to download.
 * @param imageView
 *            The ImageView to bind the downloaded image to.
 * @param loadImgViaMobile 
 *             ?true
 */
public void loadImage(Object data, ImageView imageView, LoadingIndicatorView indicatorView,
        boolean loadImgViaMobile) {

    if (data == null || TextUtils.isEmpty(data.toString().trim())) {
        if (mLoadingBitmap != null && imageView != null)
            imageView.setImageBitmap(mLoadingBitmap);
        return;
    }

    Bitmap bitmap = null;

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

    if (bitmap != null) {
        // Bitmap found in memory cache
        notifyLoadComplete(String.valueOf(data), bitmap, imageView);
        imageView.setImageBitmap(bitmap);
        indicatorView.onProgressUpdate(100);
        indicatorView.setAsyncIndicator(null);
    } else if (cancelPotentialWork(data, imageView)) {
        final BitmapWorkerTask task = new BitmapWorkerTask(imageView, indicatorView, loadImgViaMobile);
        final AsyncDrawable asyncDrawable = new AsyncDrawable(mResources, mLoadingBitmap, task);
        imageView.setImageDrawable(asyncDrawable);

        final AsyncIndicator asyncIndicator = new AsyncIndicator(task);
        indicatorView.setAsyncIndicator(asyncIndicator);

        // 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, data);
    }
}

From source file:com.nttec.everychan.ui.gallery.GalleryActivity.java

private void setGif(final GalleryItemViewTag tag, final File file) {
    if (!settings.useNativeGif()) {
        setWebView(tag, file);//from  w w w. ja  v  a  2 s .  com
        return;
    }
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            ImageView iv = Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO
                    ? new ImageView(GalleryActivity.this)
                    : new TouchGifView(GalleryActivity.this);
            try {
                GifDrawable drawable = new GifDrawable(file);
                iv.setTag(drawable);
                iv.setImageDrawable(drawable);
            } catch (Throwable e) {
                System.gc();
                Logger.e(TAG, "cannot init GifDrawable", e);
                if (tag.downloadingTask.isCancelled())
                    return;
                setWebView(tag, file);
                return;
            }

            if (tag.downloadingTask.isCancelled())
                return;

            tag.thumbnailView.setVisibility(View.GONE);
            tag.loadingView.setVisibility(View.GONE);

            tag.layout.setVisibility(View.VISIBLE);
            tag.layout.addView(iv);
        }
    });
}

From source file:com.google.android.imageloader.ImageLoader.java

/**
 * Binds a URL to an {@link ImageView} within an {@link android.widget.AdapterView}.
 *
 * @param adapter the adapter for the {@link android.widget.AdapterView}.
 * @param view the {@link ImageView}.// w w w .j  ava 2s.  c  om
 * @param url the image URL.
 * @return a {@link BindResult}.
 * @throws NullPointerException if any of the arguments are {@code null}.
 */
public BindResult bind(BaseAdapter adapter, ImageView view, String url) {
    if (adapter == null) {
        throw new NullPointerException("Adapter is null");
    }
    if (view == null) {
        throw new NullPointerException("ImageView is null");
    }
    if (url == null) {
        throw new NullPointerException("URL is null");
    }
    Bitmap bitmap = getBitmap(url);
    ImageError error = getError(url);
    if (bitmap != null) {
        view.setImageBitmap(bitmap);
        return BindResult.OK;
    } else {
        // Clear the ImageView by default.
        // The caller can set their own placeholder
        // based on the return value.
        view.setImageDrawable(null);

        if (error != null) {
            return BindResult.ERROR;
        } else {
            ImageRequest request = new ImageRequest(adapter, url);

            // For adapters, post the latest requests
            // at the front of the queue in case the user
            // has already scrolled past most of the images
            // that are currently in the queue.
            insertRequestAtFrontOfQueue(request);

            return BindResult.LOADING;
        }
    }
}

From source file:com.google.android.imageloader.ImageLoader.java

/**
 * Binds a URL to an {@link ImageView} within an {@link android.widget.ExpandableListView}.
 *
 * @param adapter the adapter for the {@link android.widget.ExpandableListView}.
 * @param view the {@link ImageView}./*from  w  ww  .ja v  a  2s  .c o m*/
 * @param url the image URL.
 * @return a {@link BindResult}.
 * @throws NullPointerException if any of the arguments are {@code null}.
 */
public BindResult bind(BaseExpandableListAdapter adapter, ImageView view, String url) {
    if (adapter == null) {
        throw new NullPointerException("Adapter is null");
    }
    if (view == null) {
        throw new NullPointerException("ImageView is null");
    }
    if (url == null) {
        throw new NullPointerException("URL is null");
    }
    Bitmap bitmap = getBitmap(url);
    ImageError error = getError(url);
    if (bitmap != null) {
        view.setImageBitmap(bitmap);
        return BindResult.OK;
    } else {
        // Clear the ImageView by default.
        // The caller can set their own placeholder
        // based on the return value.
        view.setImageDrawable(null);

        if (error != null) {
            return BindResult.ERROR;
        } else {
            ImageRequest request = new ImageRequest(adapter, url);

            // For adapters, post the latest requests
            // at the front of the queue in case the user
            // has already scrolled past most of the images
            // that are currently in the queue.
            insertRequestAtFrontOfQueue(request);

            return BindResult.LOADING;
        }
    }
}

From source file:com.mappn.gfan.ui.HomeTabActivity.java

private void drawUpdateCount(Activity context, Resources res, ImageView view, boolean flag) {
    DisplayMetrics dm = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(dm);
    Bitmap cornerRes = BitmapFactory.decodeResource(res, R.drawable.notify_update);
    Bitmap appBitmapNormal = BitmapFactory.decodeResource(res, R.drawable.main_tab_app_unselect);
    Bitmap appBitmapPressed = BitmapFactory.decodeResource(res, R.drawable.main_tab_app_select);

    StateListDrawable stateDrawable = new StateListDrawable();
    int stateSelected = android.R.attr.state_selected;
    if (flag) {/*from   ww  w .j  a  va 2s .c  o m*/
        Bitmap cornerBitmap = drawText(dm, res, cornerRes, mSession.getUpgradeNumber());
        Bitmap newBitmapNormal = drawBitmap(dm, appBitmapNormal, cornerBitmap);
        Bitmap newBitmapPressed = drawBitmap(dm, appBitmapPressed, cornerBitmap);

        stateDrawable.addState(new int[] { -stateSelected }, new BitmapDrawable(res, newBitmapNormal));
        stateDrawable.addState(new int[] { stateSelected }, new BitmapDrawable(res, newBitmapPressed));

        view.setImageDrawable(stateDrawable);
    } else {

        view.setImageResource(R.drawable.main_tab_app_manager_selector);
    }
}

From source file:com.arlib.floatingsearchview.FloatingSearchView.java

private void changeIcon(ImageView imageView, Drawable newIcon, boolean withAnim) {

    imageView.setImageDrawable(newIcon);
    if (withAnim) {
        ObjectAnimator fadeInVoiceInputOrClear = new ObjectAnimator().ofFloat(imageView, "alpha", 0.0f, 1.0f);
        fadeInVoiceInputOrClear.start();
    } else {/*from   w  w  w . ja  v a 2 s .c  o  m*/
        imageView.setAlpha(1.0f);
    }
}

From source file:com.freeme.filemanager.view.FileViewFragment.java

private View createStorageVolumeItem(final String volumPath, String volumDescription) {

    View listItem = LayoutInflater.from(mActivity).inflate(R.layout.dropdown_item, null);
    View listContent = listItem.findViewById(R.id.list_item);
    ImageView img = (ImageView) listItem.findViewById(R.id.item_icon);
    TextView text = (TextView) listItem.findViewById(R.id.path_name);
    text.setText(volumDescription);//from  w  ww . j  ava 2  s  .c  o  m

    //*/ freeme.liuhaoran , 20160728 , volumeItem image
    /*/
    img.setImageResource(getStorageVolumeIconByDescription(volumDescription));
    //*/
    Log.i("liuhaoran3", "storageVolume.getPath() = " + storageVolume.getPath());
    Log.i("liuhaoran3", "internalPath = " + internalPath);
    if (storageVolume.getPath().equals(internalPath)) {
        img.setImageDrawable((getResources().getDrawable(R.drawable.storage_internal_n)));
    } else if ((storageVolume.getDescription(mActivity).toString()).contains("SD")) {
        img.setImageDrawable((getResources().getDrawable(R.drawable.storage_sd_card_n)));
    } else if (storageVolume.getDescription(mActivity).toString().contains("usbotg")) {
        img.setImageDrawable((getResources().getDrawable(R.drawable.storage_usb_n)));
    }
    //*/

    //modigy by droi heqianqian if the stroage device is not phone memeory, then set the storage could be unmoumt
    ImageView unmount_btn = (ImageView) listItem.findViewById(R.id.unmount_btn);
    if (volumPath.equals(Util.SD_DIR)) {
        //*/ freeme.liuhaoran , 20160802 , judge whether there is a SD card operation permissions
        if (ContextCompat.checkSelfPermission(mActivity,
                "android.permission.MOUNT_UNMOUNT_FILESYSTEMS") == PackageManager.PERMISSION_GRANTED) {
            unmount_btn.setVisibility(View.VISIBLE);
            unmount_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    MountHelper.getInstance(mActivity).unMount(volumPath);
                    showVolumesList(false);
                    mVolumeSwitch.setVisibility(View.GONE);
                    int mounedCount = StorageHelper.getInstance(mActivity).getMountedVolumeCount();
                }
            });
        } else {
            unmount_btn.setVisibility(View.INVISIBLE);
        }
        //*/
    }
    listItem.setOnClickListener(mStorageVolumeClick);
    listItem.setTag(new Pair(volumPath, volumDescription));
    return listItem;
}

From source file:com.ifeng.util.imagecache.ImageWorker.java

/**
 * Called when the processing is complete and the final drawable should be
 * set on the ImageView./*from  w  w w  .  jav a2  s. c o  m*/
 * 
 * @param imageView
 * @param drawable
 */
private void setImageDrawable(ImageView imageView, Drawable drawable) {
    // Bug fix by XuWei 2013-09-09
    // Drawable?bug?ViewDrawable??Drawable
    Drawable copyDrawable = new BitmapDrawable(mResources, ((BitmapDrawable) drawable).getBitmap());

    if (mFadeInBitmap) {
        // Transition drawable with a transparent drawable and the final
        // drawable
        final TransitionDrawable td = new TransitionDrawable(
                new Drawable[] { new ColorDrawable(android.R.color.transparent), copyDrawable });
        // Set background to loading bitmap
        imageView.setImageDrawable(new BitmapDrawable(mResources, mLoadingBitmap));

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

From source file:com.google.android.imageloader.ImageLoader.java

/**
 * Binds an image at the given URL to an {@link ImageView}.
 * <p>//from w ww.  j a v  a2 s .  co m
 * If the image needs to be loaded asynchronously, it will be assigned at a
 * later time, replacing any existing {@link Drawable} unless
 * {@link #unbind(ImageView)} is called or
 * {@link #bind(ImageView, String, Callback)} is called with the same
 * {@link ImageView}, but a different URL.
 * <p>
 * Use {@link #bind(BaseAdapter, ImageView, String)} instead of this method
 * when the {@link ImageView} is in an {@link android.widget.AdapterView} so
 * that the image will be bound correctly in the case where it has been
 * assigned to a different position since the asynchronous request was
 * started.
 *
 * @param view the {@link ImageView} to bind.
 * @param url the image URL.s
 * @param callback invoked after the image has finished loading or after an
 *            error. The callback may be executed before this method returns
 *            when the result is cached. This parameter can be {@code null}
 *            if a callback is not required.
 * @return a {@link BindResult}.
 * @throws NullPointerException if a required argument is {@code null}
 */
public BindResult bind(ImageView view, String url, Callback callback) {
    if (view == null) {
        throw new NullPointerException("ImageView is null");
    }
    if (url == null) {
        throw new NullPointerException("URL is null");
    }
    mImageViewBinding.put(view, url);
    Bitmap bitmap = getBitmap(url);
    ImageError error = getError(url);
    if (bitmap != null) {
        view.setImageBitmap(bitmap);
        if (callback != null) {
            callback.onImageLoaded(view, url);
        }
        return BindResult.OK;
    } else {
        // Clear the ImageView by default.
        // The caller can set their own placeholder
        // based on the return value.
        view.setImageDrawable(null);

        if (error != null) {
            if (callback != null) {
                callback.onImageError(view, url, error.getCause());
            }
            return BindResult.ERROR;
        } else {
            ImageRequest request = new ImageRequest(view, url, callback);
            enqueueRequest(request);
            return BindResult.LOADING;
        }
    }
}