Example usage for android.graphics.drawable TransitionDrawable startTransition

List of usage examples for android.graphics.drawable TransitionDrawable startTransition

Introduction

In this page you can find the example usage for android.graphics.drawable TransitionDrawable startTransition.

Prototype

public void startTransition(int durationMillis) 

Source Link

Document

Begin the second layer on top of the first layer.

Usage

From source file:Main.java

public static void setOnTouchBackgroundEffect(View view) {
    view.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (!(v.getBackground() instanceof TransitionDrawable))
                return false;

            TransitionDrawable transition = (TransitionDrawable) v.getBackground();

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                transition.startTransition(500);
                break;
            case MotionEvent.ACTION_HOVER_EXIT:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                transition.reverseTransition(500);
                break;
            }//from w  w w . j ava2s  .c  o m

            return false;
        }
    });
}

From source file:Main.java

/**
 * @see "http://blog.peterkuterna.net/2011/09/simple-crossfade-on-imageview.html"
 * with modifications by Thomas Suarez.//from  w  ww .j  a v  a  2  s . c o m
 */
public static void setImageDrawableWithFade(final Activity context, final ImageView imageView,
        final String drawableName, final int durationMillis) {
    int resId = getDrawableId(context, drawableName); // get new drawable resource
    Drawable drawable;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        drawable = context.getResources().getDrawable(resId, context.getTheme());
    } else {
        drawable = context.getResources().getDrawable(resId); // this is deprecated starting in Android 5.0 Lollipop
    }

    final Drawable currentDrawable = imageView.getDrawable();
    final Drawable newDrawable = drawable;

    Runnable r = new Runnable() {
        @Override
        public void run() {
            if (currentDrawable != null) {
                Drawable[] arrayDrawable = new Drawable[2];
                arrayDrawable[0] = currentDrawable;
                arrayDrawable[1] = newDrawable;
                TransitionDrawable transitionDrawable = new TransitionDrawable(arrayDrawable);
                transitionDrawable.setCrossFadeEnabled(true);
                imageView.setImageDrawable(transitionDrawable);
                transitionDrawable.startTransition(durationMillis);
            } else {
                imageView.setImageDrawable(newDrawable);
            }
        }
    };
    context.runOnUiThread(r);
}

From source file:com.vinay.volley.lazyload.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.
 *//*w ww  .  jav  a 2s  .c o  m*/
@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 && 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(ANIMATION_FADE_IN_TIME);
    } else {
        // No fade in, just set bitmap directly
        imageView.setImageBitmap(bitmap);
    }
}

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 v a2s.c o  m
 */
@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.likou.util.NImageLoader.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   w ww.  j a  v  a  2 s  .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 && NetUtils.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(250);
    } else {
        // No fade in, just set bitmap directly
        imageView.setImageBitmap(bitmap);
    }
}

From source file:com.android.volley.cache.plus.SimpleImageLoader.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.//  ww w. j  a  v  a 2 s .co  m
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private static void setPhotoImageBitmap(final PhotoView imageView, final BitmapDrawable bitmapDrawable,
        Resources resources, boolean fadeIn) {
    // If we're fading in and on HC MR1+
    if (fadeIn && Utils.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.bindDrawable(bitmapDrawable);
                        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;
        }
        // Use TransitionDrawable to fade in
        final TransitionDrawable td = new TransitionDrawable(
                new Drawable[] { initialDrawable, bitmapDrawable });
        imageView.bindDrawable(td);
        td.startTransition(Utils.ANIMATION_FADE_IN_TIME);
    } else {
        // No fade in, just set bitmap directly
        imageView.bindDrawable(bitmapDrawable);
    }
}

From source file:com.android.volley.cache.plus.SimpleImageLoader.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  av a  2 s .  c  o  m*/
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private static void setImageBitmap(final ImageView imageView, final BitmapDrawable bitmapDrawable,
        Resources resources, boolean fadeIn) {

    // If we're fading in and on HC MR1+
    if (fadeIn && Utils.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.setImageDrawable(bitmapDrawable);
                        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;
        }
        // Use TransitionDrawable to fade in
        final TransitionDrawable td = new TransitionDrawable(
                new Drawable[] { initialDrawable, bitmapDrawable });
        imageView.setImageDrawable(td);
        td.startTransition(Utils.ANIMATION_FADE_IN_TIME);
    } else {
        // No fade in, just set bitmap directly
        imageView.setImageDrawable(bitmapDrawable);
    }
}

From source file:com.amazon.android.utils.Helpers.java

/**
 * Loads an image using Glide from a URL into an image view and crossfades it with the image
 * view's current image./*from   w ww . j  a v  a 2  s  .  co m*/
 *
 * @param activity          The activity.
 * @param imageView         The image view to load the image into to.
 * @param url               The URL that points to the image to load.
 * @param crossFadeDuration The duration of the cross-fade in milliseconds.
 */
public static void loadImageWithCrossFadeTransition(Activity activity, ImageView imageView, String url,
        final int crossFadeDuration) {
    /*
     * With the Glide image managing framework, cross fade animations only take place if the
     * image is not already downloaded in cache. In order to have the cross fade animation
     * when the image is in cache, we need to make the following two calls.
     */
    Glide.with(activity).load(url).listener(new LoggingListener<>()).fitCenter()
            .error(R.drawable.browse_bg_color).placeholder(imageView.getDrawable()).crossFade().into(imageView);

    // Adding this second Glide call enables cross-fade transition even if the image is cached.
    Glide.with(activity).load(url).fitCenter().error(R.drawable.browse_bg_color)
            .placeholder(imageView.getDrawable())
            // Here we override the onResourceReady of the RequestListener to force
            // the cross fade animation.
            .listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target,
                        boolean isFirstResource) {

                    Log.d("GLIDE", String.format(Locale.ROOT, "onException(%s, %s, %s, %s)", e, model, target,
                            isFirstResource), e);
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model,
                        Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {

                    ImageViewTarget<GlideDrawable> imageTarget = (ImageViewTarget<GlideDrawable>) target;
                    Drawable current = imageTarget.getCurrentDrawable();
                    if (current != null) {
                        TransitionDrawable transitionDrawable = new TransitionDrawable(
                                new Drawable[] { current, resource });
                        transitionDrawable.setCrossFadeEnabled(true);
                        transitionDrawable.startTransition(crossFadeDuration);
                        imageTarget.setDrawable(transitionDrawable);
                        return true;
                    } else
                        return false;
                }
            }).crossFade().into(imageView);
}

From source file:com.android.volley.cache.SimpleImageLoader.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./*  ww  w. j  ava 2s.c  om*/
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private static void setPhotoImageBitmap(final PhotoView imageView, final Bitmap bitmap, Resources resources,
        boolean fadeIn) {
    // If we're fading in and on HC MR1+
    if (fadeIn && Utils.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.bindPhoto(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.bindDrawable(td);
        td.startTransition(Utils.ANIMATION_FADE_IN_TIME);
    } else {
        // No fade in, just set bitmap directly
        imageView.bindPhoto(bitmap);
    }
}

From source file:com.android.volley.cache.SimpleImageLoader.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  w w w . java  2s . co m*/
 */
@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 && Utils.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(Utils.ANIMATION_FADE_IN_TIME);
    } else {
        // No fade in, just set bitmap directly
        imageView.setImageBitmap(bitmap);
    }
}