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:Main.java

public static void startVDAnimation(ImageView imageView, @DrawableRes int inactiveResId,
        @DrawableRes int activeResId, int duration) {
    int drawableResId = imageView.isSelected() ? activeResId : inactiveResId;
    Drawable drawable = ContextCompat.getDrawable(imageView.getContext(), drawableResId);
    imageView.setImageDrawable(drawable);

    if (drawable instanceof Animatable) {
        Animatable animatable = (Animatable) drawable;
        if (animatable.isRunning()) {
            animatable.stop();/*from  w  w  w  .ja  v a2s  . c  o m*/
        }

        animatable.start();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            imageView.setSelected(!imageView.isSelected());
        } else {
            imageView.postDelayed(() -> {
                imageView.setSelected(!imageView.isSelected());
                int nextDrawableResId = imageView.isSelected() ? activeResId : inactiveResId;
                Drawable nextDrawable = ContextCompat.getDrawable(imageView.getContext(), nextDrawableResId);
                imageView.setImageDrawable(nextDrawable);
            }, duration);
        }
    }
}

From source file:com.ahmadrosid.lib.baseapp.helper.ImageViewHelper.java

public static void createRounded(Context context, int imgRes, ImageView imageView) {
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), imgRes);
    RoundedBitmapDrawable rounded = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
    rounded.setCornerRadius(bitmap.getWidth());
    imageView.setImageDrawable(rounded);
}

From source file:arun.com.chromer.settings.widgets.PreferenceIconLayoutHelper.java

/**
 * Finds the icon view and attempts to tint it based on the checked state of the preference
 *
 * @param imageView The image view of icon
 * @param checked   Whether the preference is enabled
 *///from  www  .j  ava 2  s.c  o m
private static void applyIconTint(@NonNull ImageView imageView, boolean checked) {
    final Drawable drawable = imageView.getDrawable();
    if (drawable != null) {
        if (drawable instanceof IconicsDrawable) {
            // Just redraw with the correct color
            imageView.setImageDrawable(
                    ((IconicsDrawable) drawable).color(checked ? CHECKED_COLOR : UNCHECKED_COLOR));
        } else {
            final Drawable wrap = DrawableCompat.wrap(drawable);
            DrawableCompat.setTint(wrap, checked ? CHECKED_COLOR : UNCHECKED_COLOR);
            imageView.setImageDrawable(drawable);
        }
    }
}

From source file:com.doctoror.fuckoffmusicplayer.presentation.util.BindingAdapters.java

@BindingAdapter({ "src", "tintNormal", "useActivatedSrcTint" })
public static void setUseActivatedSrcTint(@NonNull final ImageView imageView, @Nullable final Drawable src,
        @ColorInt final int tintNormal, final boolean useActivatedSrcTint) {
    if (!useActivatedSrcTint || src == null) {
        imageView.setImageDrawable(src);
    } else {/*from w  w  w .  ja va 2s .  c  o m*/
        imageView.setImageDrawable(
                DrawableUtils.getTintedDrawable(src, activatedTint(imageView.getContext(), tintNormal)));
    }
}

From source file:com.android.contacts.editor.EditorUiUtils.java

/** Binds the default avatar to the given ImageView and tints it to match QuickContacts. */
public static void setDefaultPhoto(ImageView imageView, Resources resources, MaterialPalette materialPalette) {
    // Use the default avatar drawable
    imageView.setImageDrawable(ContactPhotoManager.getDefaultAvatarDrawableForContact(resources,
            /* hires =*/ false, /* defaultImageRequest =*/ null));

    // Tint it to match the quick contacts
    if (imageView instanceof QuickContactImageView) {
        ((QuickContactImageView) imageView)
                .setTint(materialPalette == null ? getDefaultPrimaryAndSecondaryColors(resources).mPrimaryColor
                        : materialPalette.mPrimaryColor);
    }/*from  w w  w . j  a v  a2s  . com*/
}

From source file:com.doctoror.fuckoffmusicplayer.presentation.util.BindingAdapters.java

@BindingAdapter({ "src", "tintAttr" })
public static void setSrcTintedFromAttr(@NonNull final ImageView imageView, @Nullable Drawable src,
        @AttrRes final int tintAttr) {
    if (src != null) {
        src = DrawableUtils.getTintedDrawableFromAttrTint(imageView.getContext(), src, tintAttr);
    }/*from w  w  w .j  a  v  a2s .c om*/
    imageView.setImageDrawable(src);
}

From source file:cn.bingoogolapple.scaffolding.util.DrawableUtil.java

public static void tintPressedIndicator(ImageView imageView, @DrawableRes int normalResId,
        @DrawableRes int pressedResId, @ColorRes int colorResId) {
    Drawable normal = imageView.getResources().getDrawable(normalResId);
    Drawable pressed = imageView.getResources().getDrawable(pressedResId);
    pressed = tintDrawable(imageView.getContext(), pressed, colorResId);
    imageView.setImageDrawable(getPressedSelectorDrawable(normal, pressed));
}

From source file:Main.java

public static void traverseAndRecolor(View root, int color, boolean withStates,
        boolean setClickableItemBackgrounds) {
    Context context = root.getContext();

    if (setClickableItemBackgrounds && root.isClickable()) {
        StateListDrawable selectableItemBackground = new StateListDrawable();
        selectableItemBackground.addState(new int[] { android.R.attr.state_pressed },
                new ColorDrawable((color & 0xffffff) | 0x33000000));
        selectableItemBackground.addState(new int[] { android.R.attr.state_focused },
                new ColorDrawable((color & 0xffffff) | 0x44000000));
        selectableItemBackground.addState(new int[] {}, null);
        root.setBackground(selectableItemBackground);
    }//from  w  w  w  . j av  a  2  s .c o m

    if (root instanceof ViewGroup) {
        ViewGroup parent = (ViewGroup) root;
        for (int i = 0; i < parent.getChildCount(); i++) {
            traverseAndRecolor(parent.getChildAt(i), color, withStates, setClickableItemBackgrounds);
        }

    } else if (root instanceof ImageView) {
        ImageView imageView = (ImageView) root;
        Drawable sourceDrawable = imageView.getDrawable();
        if (withStates && sourceDrawable != null && sourceDrawable instanceof BitmapDrawable) {
            imageView.setImageDrawable(
                    makeRecoloredDrawable(context, (BitmapDrawable) sourceDrawable, color, true));
        } else {
            imageView.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
        }

    } else if (root instanceof TextView) {
        TextView textView = (TextView) root;
        if (withStates) {
            int sourceColor = textView.getCurrentTextColor();
            ColorStateList colorStateList = new ColorStateList(
                    new int[][] { new int[] { android.R.attr.state_pressed },
                            new int[] { android.R.attr.state_focused }, new int[] {} },
                    new int[] { sourceColor, sourceColor, color });
            textView.setTextColor(colorStateList);
        } else {
            textView.setTextColor(color);
        }

    } else if (root instanceof AnalogClock) {
        AnalogClock analogClock = (AnalogClock) root;
        try {
            Field hourHandField = AnalogClock.class.getDeclaredField("mHourHand");
            hourHandField.setAccessible(true);
            Field minuteHandField = AnalogClock.class.getDeclaredField("mMinuteHand");
            minuteHandField.setAccessible(true);
            Field dialField = AnalogClock.class.getDeclaredField("mDial");
            dialField.setAccessible(true);
            BitmapDrawable hourHand = (BitmapDrawable) hourHandField.get(analogClock);
            if (hourHand != null) {
                Drawable d = makeRecoloredDrawable(context, hourHand, color, withStates);
                d.setCallback(analogClock);
                hourHandField.set(analogClock, d);
            }
            BitmapDrawable minuteHand = (BitmapDrawable) minuteHandField.get(analogClock);
            if (minuteHand != null) {
                Drawable d = makeRecoloredDrawable(context, minuteHand, color, withStates);
                d.setCallback(analogClock);
                minuteHandField.set(analogClock, d);
            }
            BitmapDrawable dial = (BitmapDrawable) dialField.get(analogClock);
            if (dial != null) {
                Drawable d = makeRecoloredDrawable(context, dial, color, withStates);
                d.setCallback(analogClock);
                dialField.set(analogClock, d);
            }
        } catch (NoSuchFieldException ignored) {
        } catch (IllegalAccessException ignored) {
        } catch (ClassCastException ignored) {
        } // TODO: catch all exceptions?
    }
}

From source file:it.cdpaf.helper.DrawableManager.java

public static void fetchDrawableOnThread(final Category cat, final ImageView imageView, final Context ctx) {
    final String urlString = Const.IMAGE_URL + cat.getNomeImmagine();
    if (drawableMap.containsKey(urlString)) {
        imageView.setImageDrawable(drawableMap.get(urlString));
        //product.setImmagine(drawableMap.get(urlString));
    }//  w w w.ja v a  2s  . co m

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            Drawable dr = (Drawable) message.obj;
            imageView.setImageDrawable(dr);
            cat.setImmagine(dr);
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            //TODO : set imageView to a "pending" image
            Drawable d = ctx.getResources().getDrawable(R.drawable.ic_launcher);
            Message messagea = handler.obtainMessage(1, d);
            handler.sendMessage(messagea);
            Drawable drawable = fetchDrawable(urlString, ctx);
            Message messageb = handler.obtainMessage(1, drawable);
            handler.sendMessage(messageb);
        }
    };
    thread.start();
}

From source file:com.ahmadrosid.lib.baseapp.helper.ImageViewHelper.java

public static void createRounded(Context context, File file, ImageView imageView) {
    Glide.with(context).load(file).asBitmap().centerCrop().into(new BitmapImageViewTarget(imageView) {
        @Override//from w  w w .  j av a  2  s.com
        protected void setResource(Bitmap resource) {
            RoundedBitmapDrawable rounded = RoundedBitmapDrawableFactory.create(context.getResources(),
                    resource);
            rounded.setCircular(true);
            imageView.setImageDrawable(rounded);
            Log.d(TAG, "setResource: " + file.getAbsoluteFile());
        }
    });
}