Example usage for android.widget ImageView getDrawable

List of usage examples for android.widget ImageView getDrawable

Introduction

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

Prototype

public Drawable getDrawable() 

Source Link

Document

Gets the current Drawable, or null if no Drawable has been assigned.

Usage

From source file:cut.ac.cy.my_tour_guide.gallery.ImageDownloader.java

/**
 * @param imageView//from w ww . jav  a2s  . c  o m
 *            Any imageView
 * @return Retrieve the currently active download task (if any) associated
 *         with this imageView. null if there is no such task.
 */
private static BitmapDownloaderTask getBitmapDownloaderTask(ImageView imageView) {
    if (imageView != null) {
        Drawable drawable = imageView.getDrawable();
        if (drawable instanceof DownloadedDrawable) {
            DownloadedDrawable downloadedDrawable = (DownloadedDrawable) drawable;
            return downloadedDrawable.getBitmapWorkerTask();
        }
    }
    return null;
}

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static void tintView(ImageView view, int color) {
    Drawable bg = view.getBackground();//from  w  ww  . j a va2  s .  co m
    if (bg != null) {
        bg = DrawableCompat.wrap(bg);
        DrawableCompat.setTint(bg, color);
        view.setBackground(bg);
    }

    Drawable img = view.getDrawable();
    if (img != null) {
        img = DrawableCompat.wrap(img);
        DrawableCompat.setTint(img, color);
        view.setImageDrawable(img);
    }
}

From source file:com.conferenceengineer.android.iosched.util.ImageLoader.java

/**
 * Sets a {@link android.graphics.Bitmap} to an {@link android.widget.ImageView} using a
 * fade-in animation. If there is a {@link android.graphics.drawable.Drawable} already set on
 * the ImageView then use that as the image to fade from. Otherwise fade in from a transparent
 * Drawable.//from ww w.  j a va 2s .  c om
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private static void setImageBitmap(final ImageView imageView, final Bitmap bitmap, Resources resources,
        boolean fadeIn) {

    // If we're fading in and on HC MR1+
    if (fadeIn && UIUtils.hasHoneycombMR1()) {
        // Use ViewPropertyAnimator to run a simple fade in + fade out animation to update the
        // ImageView
        imageView.animate().scaleY(0.95f).scaleX(0.95f).alpha(0f)
                .setDuration(imageView.getDrawable() == null ? 0 : HALF_FADE_IN_TIME)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        imageView.setImageBitmap(bitmap);
                        imageView.animate().alpha(1f).scaleY(1f).scaleX(1f).setDuration(HALF_FADE_IN_TIME)
                                .setListener(null);
                    }
                });
    } else if (fadeIn) {
        // Otherwise use a TransitionDrawable to fade in
        Drawable initialDrawable;
        if (imageView.getDrawable() != null) {
            initialDrawable = imageView.getDrawable();
        } else {
            initialDrawable = transparentDrawable;
        }
        BitmapDrawable bitmapDrawable = new BitmapDrawable(resources, bitmap);
        // Use TransitionDrawable to fade in
        final TransitionDrawable td = new TransitionDrawable(
                new Drawable[] { initialDrawable, bitmapDrawable });
        imageView.setImageDrawable(td);
        td.startTransition(UIUtils.ANIMATION_FADE_IN_TIME);
    } else {
        // No fade in, just set bitmap directly
        imageView.setImageBitmap(bitmap);
    }
}

From source file:com.fireplace.market.fads.util.ImageWorker.java

/**
 * @param imageView//from   w  ww  . ja  v a  2  s .  co  m
 *            Any imageView
 * @return Retrieve the currently active work task (if any) associated with
 *         this imageView. null if there is no such task.
 */
private static BitmapLocalWorkerTask getBitmapLocalWorkerTask(ImageView imageView) {
    if (imageView != null) {
        final Drawable drawable = imageView.getDrawable();
        if (drawable instanceof AsyncLocalDrawable) {
            final AsyncLocalDrawable asyncDrawable = (AsyncLocalDrawable) drawable;
            return asyncDrawable.getBitmapWorkerTask();
        }
    }
    return null;
}

From source file:com.nttec.everychan.ui.theme.CustomThemeHelper.java

private static void setLollipopMenuOverflowIconColor(final ViewGroup toolbar, final int color) {
    try {//from   w  ww .  j a va 2  s  .c o m
        //for API 23 (Android 6): at this point method Toolbar.setOverflowIcon(Drawable) has no effect
        toolbar.getClass().getMethod("getMenu").invoke(toolbar);
        AppearanceUtils.callWhenLoaded(toolbar, new Runnable() {
            @Override
            public void run() {
                try {
                    final ViewGroup actionMenuView = (ViewGroup) findViewByClassName(toolbar,
                            "android.widget.ActionMenuView");

                    Runnable setOverflowIcon = new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Class<?> toolbarClass = toolbar.getClass();
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                    Drawable overflowIcon = (Drawable) toolbarClass.getMethod("getOverflowIcon")
                                            .invoke(toolbar);
                                    setColorFilter(overflowIcon);
                                    toolbarClass.getMethod("setOverflowIcon", Drawable.class).invoke(toolbar,
                                            overflowIcon);
                                } else {
                                    ImageView overflowButton = (ImageView) findViewByClassName(actionMenuView,
                                            "android.widget.ActionMenuPresenter$OverflowMenuButton");
                                    if (overflowButton != null) {
                                        Drawable overflowIcon = overflowButton.getDrawable();
                                        setColorFilter(overflowIcon);
                                        overflowButton.setImageDrawable(overflowIcon);
                                    }
                                }
                            } catch (Exception e) {
                                Logger.e(TAG, e);
                            }
                        }

                        private void setColorFilter(Drawable overflowIcon) {
                            overflowIcon.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
                        }
                    };

                    if (actionMenuView.getChildCount() == 0) {
                        AppearanceUtils.callWhenLoaded(actionMenuView == null ? toolbar : actionMenuView,
                                setOverflowIcon);
                    } else {
                        setOverflowIcon.run();
                    }
                } catch (Exception e) {
                    Logger.e(TAG, e);
                }
            }

            private View findViewByClassName(ViewGroup group, String className) {
                for (int i = 0, size = group.getChildCount(); i < size; ++i) {
                    View child = group.getChildAt(i);
                    if (child.getClass().getName().equals(className)) {
                        return child;
                    }
                }
                return null;
            }
        });
    } catch (Exception e) {
        Logger.e(TAG, e);
    }
}

From source file:com.owncloud.android.datamodel.ThumbnailsCacheManager.java

private static GetAvatarTask getAvatarWorkerTask(ImageView imageView) {
    if (imageView != null) {
        return getAvatarWorkerTask(imageView.getDrawable());
    }/*from ww w  .  ja  v  a2 s  .c  o m*/
    return null;
}

From source file:com.owncloud.android.datamodel.ThumbnailsCacheManager.java

private static ThumbnailGenerationTask getBitmapWorkerTask(ImageView imageView) {
    if (imageView != null) {
        final Drawable drawable = imageView.getDrawable();
        if (drawable instanceof AsyncThumbnailDrawable) {
            final AsyncThumbnailDrawable asyncDrawable = (AsyncThumbnailDrawable) drawable;
            return asyncDrawable.getBitmapWorkerTask();
        }//from  w w  w  .java  2 s  .c  o  m
    }
    return null;
}

From source file:meizhi.meizhi.malin.utils.DestroyCleanUtil.java

@SuppressLint("ObsoleteSdkInt")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void unBindView(View view) {
    if (view == null)
        return;//from   w  w w .j a  v  a 2  s .  com
    Drawable drawable;
    int i;
    //1.
    try {
        view.setOnClickListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //2.
    try {
        view.setOnCreateContextMenuListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //3.
    try {
        view.setOnFocusChangeListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //4.
    try {
        view.setOnKeyListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //5.
    try {
        view.setOnLongClickListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //6.
    try {
        view.setOnTouchListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //7.
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            view.setOnApplyWindowInsetsListener(null);
        }
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //8.
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            view.setOnContextClickListener(null);
        }
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //9.
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            view.setOnScrollChangeListener(null);
        }
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //10.
    try {
        view.setOnDragListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //11.
    try {
        view.setOnGenericMotionListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //12.
    try {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR2) {//13
            view.setOnHoverListener(null);
        }
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    //13.
    try {
        view.setOnSystemUiVisibilityChangeListener(null);
    } catch (Throwable e) {
        CrashReport.postCatchedException(e);
    }

    /**
     * @see SwipeRefreshLayout#onDetachedFromWindow()
     */
    if (view.getBackground() != null && !view.getClass().getName().equals(CIRCLE_CLASS)) {
        try {
            view.getBackground().setCallback(null);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {//16
                view.setBackgroundDrawable(null);
            } else {
                view.setBackground(null);
            }
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //ImageView
    if (view instanceof ImageView) {
        try {
            ImageView imageView = (ImageView) view;
            drawable = imageView.getDrawable();
            if (drawable != null) {
                drawable.setCallback(null);
            }
            imageView.setImageDrawable(null);
            imageView.setImageBitmap(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //TextView
    if (view instanceof TextView) {
        try {
            TextView textView = (TextView) view;
            textView.setCompoundDrawables(null, null, null, null);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                textView.setCompoundDrawablesRelative(null, null, null, null);
            }
            textView.setCursorVisible(false);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //ImageButton
    if (view instanceof ImageButton) {
        try {
            ImageButton imageButton = (ImageButton) view;
            drawable = imageButton.getDrawable();
            if (drawable != null) {
                drawable.setCallback(null);
            }
            imageButton.setImageDrawable(null);
            imageButton.setImageBitmap(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //ListView
    if (view instanceof ListView) {
        ListView listView = (ListView) view;

        try {
            listView.setAdapter(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }

        try {
            listView.setOnScrollListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }

        try {
            listView.setOnItemClickListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }

        try {
            listView.setOnItemLongClickListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }

        try {
            listView.setOnItemSelectedListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //RecyclerView
    if (view instanceof RecyclerView) {
        try {
            RecyclerView recyclerView = (RecyclerView) view;
            recyclerView.setAdapter(null);
            recyclerView.setChildDrawingOrderCallback(null);
            recyclerView.setOnScrollListener(null);
            recyclerView.addOnScrollListener(null);
            recyclerView.removeOnScrollListener(null);
            recyclerView.setRecyclerListener(null);
        } catch (Throwable e) {
            CrashReport.postCatchedException(e);
        }
    }

    //WebView
    if (view instanceof WebView) {

        WebView webView = (WebView) view;
        try {
            webView.stopLoading();
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            webView.removeAllViews();
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            webView.setWebChromeClient(null);
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            webView.setWebViewClient(null);
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            webView.destroy();
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

        try {
            if (null != view.getParent() && view.getParent() instanceof ViewGroup) {
                ((ViewGroup) view.getParent()).removeView(view);
            }
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }

    }

    //SurfaceView
    if (view instanceof SurfaceView) {
        try {
            SurfaceView surfaceView = (SurfaceView) view;
            SurfaceHolder holder = surfaceView.getHolder();
            if (holder != null) {
                Surface surface = holder.getSurface();
                if (surface != null) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                        surface.release();
                    }
                }
            }
        } catch (Throwable ignored) {
            CrashReport.postCatchedException(ignored);
        }
    }

    view.destroyDrawingCache();
    view.clearAnimation();

    if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) view;
        int childCount = (viewGroup).getChildCount();
        for (i = 0; i < childCount; i++) {
            unBindView((viewGroup).getChildAt(i));
        }
    }
}

From source file:com.musenkishi.atelier.Atelier.java

private static void applyColorToView(final ImageView imageView, int color, boolean fromCache,
        boolean shouldMask) {
    if (fromCache) {
        if (shouldMask) {
            if (imageView.getDrawable() != null) {
                imageView.getDrawable().mutate().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
            } else if (imageView.getBackground() != null) {
                imageView.getBackground().mutate().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
            }/*w  ww . j a  v  a  2  s. c o m*/
        } else {
            imageView.setBackgroundColor(color);
        }
    } else {
        if (shouldMask) {
            Integer colorFrom;
            ValueAnimator.AnimatorUpdateListener imageAnimatorUpdateListener = new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    if (imageView.getDrawable() != null) {
                        imageView.getDrawable().mutate().setColorFilter(
                                (Integer) valueAnimator.getAnimatedValue(), PorterDuff.Mode.MULTIPLY);
                    } else if (imageView.getBackground() != null) {
                        imageView.getBackground().mutate().setColorFilter(
                                (Integer) valueAnimator.getAnimatedValue(), PorterDuff.Mode.MULTIPLY);
                    }
                }
            };
            ValueAnimator.AnimatorUpdateListener animatorUpdateListener;

            PaletteTag paletteTag = (PaletteTag) imageView.getTag(viewTagKey);
            animatorUpdateListener = imageAnimatorUpdateListener;
            colorFrom = paletteTag.getColor();
            imageView.setTag(viewTagKey, new PaletteTag(paletteTag.getId(), color));

            Integer colorTo = color;
            ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
            colorAnimation.addUpdateListener(animatorUpdateListener);
            colorAnimation.setDuration(300);
            colorAnimation.start();
        } else {
            Drawable preDrawable;

            if (imageView.getBackground() == null) {
                preDrawable = new ColorDrawable(Color.TRANSPARENT);
            } else {
                preDrawable = imageView.getBackground();
            }

            TransitionDrawable transitionDrawable = new TransitionDrawable(
                    new Drawable[] { preDrawable, new ColorDrawable(color) });
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                imageView.setBackground(transitionDrawable);
            } else {
                imageView.setBackgroundDrawable(transitionDrawable);
            }
            transitionDrawable.startTransition(300);
        }
    }
}

From source file:my.example.onekeycleaner.imgcache.ImageWorker.java

/**
 * @param imageView Any imageView//from w  w w.  j  a va2 s  .c  o m
 * @return Retrieve the currently active work task (if any) associated with this imageView.
 * null if there is no such task.
 */
private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView, boolean isTag) {
    if (imageView != null) {
        final Drawable drawable;
        if (isTag) {
            drawable = (Drawable) imageView.getTag(R.id.load_bitmap_tag_task);
        } else {
            drawable = imageView.getDrawable();
        }
        if (drawable instanceof AsyncDrawable) {
            final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
            return asyncDrawable.getBitmapWorkerTask();
        }
    }
    return null;
}