Example usage for android.widget ImageView getBackground

List of usage examples for android.widget ImageView getBackground

Introduction

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

Prototype

public Drawable getBackground() 

Source Link

Document

Gets the background drawable

Usage

From source file:Main.java

@SuppressWarnings("deprecation")
public static void setAlpha(ImageView iv, int alpha) {
    if (iv.getBackground() != null) {
        iv.getBackground().setAlpha(alpha);
        return;/* w w  w.  ja  v  a 2 s.  com*/
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        iv.setImageAlpha(alpha);
    } else {
        iv.setAlpha(alpha);
    }
}

From source file:Main.java

public static AnimationDrawable startAnimDrawable(ImageView img) {
    AnimationDrawable animDrawble = (AnimationDrawable) img.getBackground();
    animDrawble.setOneShot(false);/* ww  w . ja v  a  2s .c  o m*/
    animDrawble.start();
    return animDrawble;
}

From source file:Main.java

public static void stopAnimation(ImageView pImageView) {
    AnimationDrawable pAnimation = null;
    if (pImageView != null) {
        pAnimation = (AnimationDrawable) pImageView.getBackground();
    }//from w  w  w .  java2 s  .  c o  m
    if (pAnimation != null && pAnimation.isRunning()) {
        pAnimation.stop();
    }
}

From source file:Main.java

/**
 * Starts an ImageView animation at the specified point in the future.
 * @param handler The Handler for the Activity in which the animation will run.
 * @param imageView An ImageView that has a background that can be casted to AnimationDrawable.
 * @param millisFromNow The number of milliseconds to wait before starting this animation. 
 *//*from  w ww .  ja va  2s .c  o  m*/
public static void startAnimationInFuture(final Handler handler, final ImageView imageView,
        long millisFromNow) {
    startAnimationInFuture(handler, (AnimationDrawable) imageView.getBackground(), millisFromNow);
}

From source file:Main.java

public static void unbindImageView(ImageView imageView) {
    if (imageView == null) {
        return;/*  w w w .  ja va2  s .  c o m*/
    }

    if (imageView.getBackground() != null) {
        imageView.getBackground().setCallback(null);
    }

    if (imageView.getDrawable() == null)
        return;
    if (!(imageView.getDrawable() instanceof BitmapDrawable))
        return;
    if (((BitmapDrawable) imageView.getDrawable()).getBitmap() == null)
        return;

    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
    try {
        if (drawable != null && imageView.getTag() != null && !drawable.getBitmap().isRecycled()) {
            if (!imageView.getTag().toString().equalsIgnoreCase("resource") && !usingMemoryCache) {
                drawable.getBitmap().recycle();
            }
        }
    } catch (RuntimeException e) {
        e.printStackTrace();
    }
    drawable.setCallback(null);
    imageView.setImageBitmap(null);
    imageView.setImageDrawable(null);
}

From source file:Main.java

/**
 * Replaces an ImageView's background AnimationDrawable with the specified resource id in the
 * future. Calls start() on the AnimationDrawable once it has been replaced. 
 * @param handler The Handler for the Activity in which the animation will run.
 * @param imageView An ImageView that has a background that can be casted to AnimationDrawable.
 * @param animationResourceId The animation's resource id.
 * @param color The color to apply to the AnimationDrawable. 
 * @param millisFromNow The number of milliseconds to wait before replacing the animation. 
 *//*from   www  .  j a  va  2 s  .co  m*/
public static void replaceAnimationInFuture(final Handler handler, final ImageView imageView,
        final int animationResourceId, final int color, long millisFromNow) {
    handler.postAtTime(new Runnable() {
        @Override
        public void run() {
            imageView.setBackgroundResource(animationResourceId);
            imageView.getBackground().mutate().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
            ((AnimationDrawable) (imageView.getBackground())).start();
        }
    }, SystemClock.uptimeMillis() + millisFromNow);
}

From source file:com.example.util.ImageUtils.java

private static BitmapDownloaderTask getBitmapDownloaderTask2(ImageView imageView) {
    if (imageView != null) {
        Drawable drawable = imageView.getBackground();
        if (drawable == null || !(drawable instanceof DownloadedDrawable2)) {
            return null;
        }/*from w w  w .  j ava 2  s  .  c  o m*/
        DownloadedDrawable2 downloadedDrawable = (DownloadedDrawable2) drawable;
        return downloadedDrawable.getBitmapDownloaderTask();
    }
    return null;
}

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

public static void tintView(ImageView view, int color) {
    Drawable bg = view.getBackground();
    if (bg != null) {
        bg = DrawableCompat.wrap(bg);//  www .j ava 2s . c  om
        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:cn.com.wo.bitmap.ImageWorker.java

/**
 * @param imageView Any imageView/* w ww.  j  a  v  a  2  s.  co  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 isBackground) {
    if (imageView != null) {
        Drawable drawable = null;
        if (isBackground)
            drawable = imageView.getBackground();
        else
            drawable = imageView.getDrawable();
        if (drawable instanceof AsyncDrawable) {
            final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
            return asyncDrawable.getBitmapWorkerTask();
        }
    }
    return null;
}

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);
            }//from w w  w.  j a  v a2 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);
        }
    }
}