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.airad.zhonghan.ui.components.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.
 * //from  w  w  w .j a  va2 s. c om
 * @param data
 *            The URL of the image to download.
 * @param imageView
 *            The ImageView to bind the downloaded image to.
 */
public void loadImage(Object data, ImageView imageView) {
    if (data == null) {
        return;
    }

    Bitmap bitmap = null;

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

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

From source file:com.hakkum.eby.garageImageFetcher.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.
 * //from  ww  w. j a v a2 s . c o m
 * @param data
 *            The URL of the image to download.
 * @param imageView
 *            The ImageView to bind the downloaded image to.
 */
public void loadImage(Object data, ImageView imageView) {
    if (data == null) {
        return;
    }

    Bitmap bitmap = null;

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

    if (bitmap != null) {
        // Bitmap found in memory cache
        imageView.setImageBitmap(Utility.getBitmapWithReflection(bitmap));
    } else if (cancelPotentialWork(data, imageView)) {
        final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
        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, data);
    }
}

From source file:com.faayda.imageloader.ImageLoader.java

/**
 * Called when the processing is complete and the final bitmap should be set on the ImageView.
 *
 * @param imageView The ImageView to set the bitmap to.
 * @param bitmap The new bitmap to set.//from   w  w  w . j av  a  2 s  .  co  m
 */
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private void setImageBitmap(ImageView imageView, Bitmap bitmap) {
    if (mFadeInBitmap) {
        // Transition drawable to fade from loading bitmap to final bitmap
        final TransitionDrawable td = new TransitionDrawable(
                new Drawable[] { new ColorDrawable(mResources.getColor(R.color.transparent)),
                        new BitmapDrawable(mResources, bitmap) });
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            imageView.setBackground(imageView.getDrawable());
        } else {
            imageView.setBackgroundDrawable(imageView.getDrawable());
        }
        imageView.setImageDrawable(td);
        td.startTransition(FADE_IN_TIME);
    } else {
        imageView.setImageBitmap(bitmap);
    }
}

From source file:com.android.project.imagefetcher.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.
 * /*from w  ww . j a v a  2 s  .  c o  m*/
 * @param data
 *            The URL of the image to download.
 * @param imageView
 *            The ImageView to bind the downloaded image to.
 */
public void loadImage(Object data, ImageView imageView) {
    if (data == null) {
        return;
    }

    Bitmap bitmap = null;

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

    if (bitmap != null) {
        // Bitmap found in memory cache
        imageView.setImageBitmap(bitmap);
    } else if (cancelPotentialWork(data, imageView)) {
        final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
        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, data);
    }
}

From source file:com.cleanwiz.applock.ui.adapter.AppPagerAdapter.java

private View buildAppView(final CommLockInfo lockInfo) {

    View convertView = mInflater.inflate(R.layout.old_item_applock, null);
    ImageView ivLogo = (ImageView) convertView.findViewById(R.id.iv_app_logo);
    final ImageView ivTag = (ImageView) convertView.findViewById(R.id.iv_tag);
    View itemView = convertView.findViewById(R.id.rl_item);
    TextView tvName = (TextView) convertView.findViewById(R.id.tv_app_name);

    if (itemHeight > 0 && itemWidth > 0) {
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) itemView.getLayoutParams();
        lp.height = itemHeight;/*from   w  ww.  ja  va 2 s.  co m*/
        lp.width = itemWidth;
    }

    ApplicationInfo appInfo = null;
    try {
        appInfo = pkgMgr.getApplicationInfo(lockInfo.getPackageName(), PackageManager.GET_UNINSTALLED_PACKAGES);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    if (appInfo != null) {
        ivLogo.setImageDrawable(pkgMgr.getApplicationIcon(appInfo));
        tvName.setText(pkgMgr.getApplicationLabel(appInfo));
    }
    ivLogo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (lockInfo.getIsLocked()) {
                appLocker.unlockApp(lockInfo.getPackageName());
                lockInfo.setIsLocked(false);
            } else {
                appLocker.lockApp(lockInfo.getPackageName());
                lockInfo.setIsLocked(true);
            }
            LogUtil.d("demo3", "lock:" + lockInfo.getIsLocked());
            if (lockInfo.getIsLocked()) {
                ivTag.setVisibility(View.VISIBLE);
            } else {
                ivTag.setVisibility(View.INVISIBLE);
            }
        }
    });

    if (lockInfo.getIsLocked()) {
        ivTag.setVisibility(View.VISIBLE);
    } else {
        ivTag.setVisibility(View.INVISIBLE);
    }

    return convertView;
}

From source file:com.gokuai.yunkuandroidsdk.imageutils.ImageWorker.java

public void loadImage(Object data, ImageView imageView, boolean isRound) {
    if (data == null) {
        return;/*from ww w .  ja v  a 2 s .  com*/
    }

    isRoundDrawable = isRound;

    BitmapDrawable value = null;

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

    if (value != null) {
        // Bitmap found in memory cache
        //            Util.makeRoundDrawable(value, GKApplication.getInstance(),false);
        if (isRound) {
            imageView.setImageBitmap(Util.makeRoundDrawable(value, GKApplication.getInstance(), false));

        } else {
            imageView.setImageDrawable(value);
        }
    } else if (cancelPotentialWork(data, imageView)) {
        final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
        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, data);
    }
}

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

/**
 * Cancels an asynchronous request to bind an image URL to an
 * {@link ImageView} and clears the {@link ImageView}.
 *
 * @see #bind(ImageView, String, Callback)
 *///from  ww w .j a  v a 2 s .com
public void unbind(ImageView view) {
    mImageViewBinding.remove(view);
    view.setImageDrawable(null);
}

From source file:com.baseproject.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 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.
 * //  w  w w.  ja  va  2 s  .c o m
 * @param data
 *            The URL of the image to download.
 * @param imageView
 *            The ImageView to bind the downloaded image to.
 */
public void loadImage(Object data, ImageView imageView) {
    Bitmap bitmap = null;

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

    if (bitmap != null) {
        // Bitmap found in memory cache
        if (null != imageView) {
            imageView.setImageBitmap(bitmap);
        }
    } else if (cancelPotentialWork(data, imageView)) {
        final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
        final AsyncDrawable asyncDrawable = new AsyncDrawable(mContext.getResources(), mLoadingBitmap, task);
        if (null != imageView) {
            imageView.setImageDrawable(asyncDrawable);
        }
        task.execute(data);
    }
}

From source file:com.jrummyapps.android.colorpicker.ColorPickerDialog.java

void createColorShades(@ColorInt final int color) {
    final int[] colorShades = getColorShades(color);

    if (shadesLayout.getChildCount() != 0) {
        for (int i = 0; i < shadesLayout.getChildCount(); i++) {
            FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
            final ColorPanelView cpv = layout.findViewById(R.id.cpv_color_panel_view);
            ImageView iv = layout.findViewById(R.id.cpv_color_image_view);
            cpv.setColor(colorShades[i]);
            cpv.setTag(false);// w  w w.  j  a  va 2  s  .com
            iv.setImageDrawable(null);
        }
        return;
    }

    final int horizontalPadding = getResources().getDimensionPixelSize(R.dimen.cpv_item_horizontal_padding);

    for (final int colorShade : colorShades) {
        int layoutResId;
        if (colorShape == ColorShape.SQUARE) {
            layoutResId = R.layout.cpv_color_item_square;
        } else {
            layoutResId = R.layout.cpv_color_item_circle;
        }

        final View view = View.inflate(getActivity(), layoutResId, null);
        final ColorPanelView colorPanelView = view.findViewById(R.id.cpv_color_panel_view);

        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) colorPanelView.getLayoutParams();
        params.leftMargin = params.rightMargin = horizontalPadding;
        colorPanelView.setLayoutParams(params);
        colorPanelView.setColor(colorShade);
        shadesLayout.addView(view);

        colorPanelView.post(new Runnable() {
            @Override
            public void run() {
                // The color is black when rotating the dialog. This is a dirty fix. WTF!?
                colorPanelView.setColor(colorShade);
            }
        });

        colorPanelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getTag() instanceof Boolean && (Boolean) v.getTag()) {
                    colorPickerDialogListener.onColorSelected(dialogId, ColorPickerDialog.this.color);
                    dismiss();
                    return; // already selected
                }
                ColorPickerDialog.this.color = colorPanelView.getColor();
                adapter.selectNone();
                for (int i = 0; i < shadesLayout.getChildCount(); i++) {
                    FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
                    ColorPanelView cpv = layout.findViewById(R.id.cpv_color_panel_view);
                    ImageView iv = layout.findViewById(R.id.cpv_color_image_view);
                    iv.setImageResource(cpv == v ? R.drawable.cpv_preset_checked : 0);
                    if (cpv == v && ColorUtils.calculateLuminance(cpv.getColor()) >= 0.65
                            || Color.alpha(cpv.getColor()) <= ALPHA_THRESHOLD) {
                        iv.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
                    } else {
                        iv.setColorFilter(null);
                    }
                    cpv.setTag(cpv == v);
                }
            }
        });
        colorPanelView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                colorPanelView.showHint();
                return true;
            }
        });
    }
}

From source file:com.jaredrummler.android.colorpicker.ColorPickerDialog.java

void createColorShades(@ColorInt final int color) {
    final int[] colorShades = getColorShades(color);

    if (shadesLayout.getChildCount() != 0) {
        for (int i = 0; i < shadesLayout.getChildCount(); i++) {
            FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
            final ColorPanelView cpv = (ColorPanelView) layout.findViewById(R.id.cpv_color_panel_view);
            ImageView iv = (ImageView) layout.findViewById(R.id.cpv_color_image_view);
            cpv.setColor(colorShades[i]);
            cpv.setTag(false);/*  w  w  w  .j a va  2  s  .c o  m*/
            iv.setImageDrawable(null);
        }
        return;
    }

    final int horizontalPadding = getResources().getDimensionPixelSize(R.dimen.cpv_item_horizontal_padding);

    for (final int colorShade : colorShades) {
        int layoutResId;
        if (colorShape == ColorShape.SQUARE) {
            layoutResId = R.layout.cpv_color_item_square;
        } else {
            layoutResId = R.layout.cpv_color_item_circle;
        }

        final View view = View.inflate(getActivity(), layoutResId, null);
        final ColorPanelView colorPanelView = (ColorPanelView) view.findViewById(R.id.cpv_color_panel_view);

        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) colorPanelView.getLayoutParams();
        params.leftMargin = params.rightMargin = horizontalPadding;
        colorPanelView.setLayoutParams(params);
        colorPanelView.setColor(colorShade);
        shadesLayout.addView(view);

        colorPanelView.post(new Runnable() {
            @Override
            public void run() {
                // The color is black when rotating the dialog. This is a dirty fix. WTF!?
                colorPanelView.setColor(colorShade);
            }
        });

        colorPanelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getTag() instanceof Boolean && (Boolean) v.getTag()) {
                    colorPickerDialogListener.onColorSelected(dialogId, ColorPickerDialog.this.color);
                    dismiss();
                    return; // already selected
                }
                ColorPickerDialog.this.color = colorPanelView.getColor();
                adapter.selectNone();
                for (int i = 0; i < shadesLayout.getChildCount(); i++) {
                    FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
                    ColorPanelView cpv = (ColorPanelView) layout.findViewById(R.id.cpv_color_panel_view);
                    ImageView iv = (ImageView) layout.findViewById(R.id.cpv_color_image_view);
                    iv.setImageResource(cpv == v ? R.drawable.cpv_preset_checked : 0);
                    if (cpv == v && ColorUtils.calculateLuminance(cpv.getColor()) >= 0.65
                            || Color.alpha(cpv.getColor()) <= ALPHA_THRESHOLD) {
                        iv.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
                    } else {
                        iv.setColorFilter(null);
                    }
                    cpv.setTag(cpv == v);
                }
            }
        });
        colorPanelView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                colorPanelView.showHint();
                return true;
            }
        });
    }
}