Example usage for android.graphics.drawable TransitionDrawable TransitionDrawable

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

Introduction

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

Prototype

public TransitionDrawable(Drawable[] layers) 

Source Link

Document

Create a new transition drawable with the specified list of layers.

Usage

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 w  w. ja v  a 2  s  . 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 && 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:xyz.bringoff.testtaskvkfriends_androidmiddle.util.ImageWorker.java

/**
 * Called when the processing is complete and the final drawable should be
 * set on the ImageView./*  www . j a  v  a  2 s  .  c o m*/
 *
 * @param imageView
 * @param drawable
 */
@SuppressWarnings("ResourceAsColor")
private void setImageDrawable(ImageView imageView, Drawable drawable) {
    if (mFadeInBitmap) {
        // Transition drawable with a transparent drawable and the final drawable
        final TransitionDrawable td = new TransitionDrawable(
                new Drawable[] { new ColorDrawable(android.R.color.transparent), drawable });
        // Set background to loading bitmap
        imageView.setBackgroundDrawable(new BitmapDrawable(mResources, mLoadingBitmap));

        imageView.setImageDrawable(td);
        td.startTransition(FADE_IN_TIME);
    } else {
        imageView.setImageDrawable(drawable);
    }
}

From source file:github.madmarty.madsonic.util.ImageLoader.java

@SuppressWarnings("deprecation")
private void setImage(View view, Drawable drawable, boolean crossfade) {
    if (view instanceof TextView) {
        // Cross-fading is not implemented for TextView since it's not in use.  It would be easy to add it, though.
        TextView textView = (TextView) view;
        textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
    } else if (view instanceof ImageView) {
        ImageView imageView = (ImageView) view;
        if (crossfade) {

            Drawable existingDrawable = imageView.getDrawable();
            if (existingDrawable == null) {
                Bitmap emptyImage;/*  w w  w.  ja  v a 2  s  .c om*/
                if (drawable.getIntrinsicWidth() > 0 && drawable.getIntrinsicHeight() > 0) {
                    emptyImage = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                            drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
                } else {
                    emptyImage = Bitmap.createBitmap(imageSizeDefault, imageSizeDefault,
                            Bitmap.Config.ARGB_8888);
                }
                existingDrawable = new BitmapDrawable(emptyImage);
            } else {
                // Try to get rid of old transitions
                try {
                    TransitionDrawable tmp = (TransitionDrawable) existingDrawable;
                    int layers = tmp.getNumberOfLayers();
                    existingDrawable = tmp.getDrawable(layers - 1);
                } catch (Exception e) {
                    // Do nothing, just means that the drawable is a flat image
                }
            }

            Drawable[] layers = new Drawable[] { existingDrawable, drawable };

            TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
            imageView.setImageDrawable(transitionDrawable);
            transitionDrawable.startTransition(250);
        } else {
            imageView.setImageDrawable(drawable);
        }
    }
}

From source file:com.androidpi.bricks.gallery.lru.cache.ImageWorker.java

/**
 * Called when the processing is complete and the final drawable should be set on the ImageView.
 *
 * @param imageView/*from  w  ww. jav  a2s. co  m*/
 * @param drawable
 */
private void setImageDrawable(ImageView imageView, Drawable drawable) {
    if (mFadeInBitmap) {
        // Transition drawable with a transparent drawable and the final
        // drawable
        final TransitionDrawable td = new TransitionDrawable(new Drawable[] {
                new ColorDrawable(imageView.getResources().getColor(android.R.color.transparent)), drawable });
        // Set background to loading bitmap
        imageView.setBackgroundDrawable(new BitmapDrawable(mResources, mLoadingBitmap));

        imageView.setImageDrawable(td);
        td.startTransition(FADE_IN_TIME);
    } else {
        imageView.setImageDrawable(drawable);
    }
}

From source file:com.ysy.classpower_utils.search_view.OwnSearchViewLayout.java

/***
 * Set the background colours of the searchview.
 * @param collapsedDrawable drawable for collapsed state, default transparent
 * @param expandedDrawable drawable for expanded state, default color.default_color_expanded
 *//*  ww  w  . j a  v  a  2 s . co m*/
public void setTransitionDrawables(Drawable collapsedDrawable, Drawable expandedDrawable) {
    this.mCollapsedDrawable = collapsedDrawable;
    this.mExpandedDrawable = expandedDrawable;

    mBackgroundTransition = new TransitionDrawable(new Drawable[] { mCollapsedDrawable, mExpandedDrawable });
    mBackgroundTransition.setCrossFadeEnabled(true);
    setBackground(mBackgroundTransition);
    Utils.setPaddingAll(OwnSearchViewLayout.this, 8);
}

From source file:com.ifeng.util.imagecache.ImageWorker.java

/**
 * // w  ww .j  a v  a 2 s .  co  m
 * 
 * @param imageView
 * @param drawable
 */
public static void setFadeInDrawable(ImageView imageView, Drawable drawable) {
    // Bug fix by XuWei 2013-09-09
    // Drawable?bug?ViewDrawable??Drawable
    Drawable copyDrawable = new BitmapDrawable(imageView.getContext().getResources(),
            ((BitmapDrawable) drawable).getBitmap());

    // Transition drawable with a transparent drawable and the final
    // drawable
    final TransitionDrawable td = new TransitionDrawable(
            new Drawable[] { new ColorDrawable(android.R.color.transparent), copyDrawable });

    imageView.setImageDrawable(td);
    td.startTransition(FADE_IN_TIME);
}

From source file:com.mg.axe.androiddevelop.view.SearchViewLayout.java

/***
 * Set the background colours of the searchview.
 * @param collapsedDrawable drawable for collapsed state, default transparent
 * @param expandedDrawable drawable for expanded state, default color.default_color_expanded
 *///ww  w.  ja va2 s.co m
public void setTransitionDrawables(Drawable collapsedDrawable, Drawable expandedDrawable) {
    this.mCollapsedDrawable = collapsedDrawable;
    this.mExpandedDrawable = expandedDrawable;

    mBackgroundTransition = new TransitionDrawable(new Drawable[] { mCollapsedDrawable, mExpandedDrawable });
    mBackgroundTransition.setCrossFadeEnabled(true);
    setBackgroundCompat();
    Utils.setPaddingAll(SearchViewLayout.this, 8);
}

From source file:kuta.adrian.cv.displayingbitmaps.ImageWorker.java

/**
 * Called when the processing is complete and the final drawable should be
 * set on the ImageView./*from   ww  w  . j  a  v  a 2s.c  o  m*/
 *
 * @param imageView
 * @param drawable
 */
private void setImageDrawable(ImageView imageView, Drawable drawable) {
    try {
        ViewGroup viewGroup = (ViewGroup) imageView.getParent();
        if (viewGroup instanceof FrameLayout) {
            FrameLayout parent = (FrameLayout) viewGroup;
            View view;
            if ((view = parent.getChildAt(0)) instanceof ProgressBar)
                parent.removeView(view);
        }
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), "onPostExecute, progressBar not found", e);
    }

    if (mFadeInBitmap) {
        // Transition drawable with a transparent drawable and the final drawable
        final TransitionDrawable td = new TransitionDrawable(new Drawable[] {
                new ColorDrawable(ContextCompat.getColor(imageView.getContext(), android.R.color.transparent)),
                drawable });
        // Set background to loading bitmap
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
            imageView.setBackgroundDrawable(new BitmapDrawable(mResources, mLoadingBitmap));
        else
            imageView.setBackground(new BitmapDrawable(mResources, mLoadingBitmap));

        imageView.setImageDrawable(td);
        td.startTransition(FADE_IN_TIME);
    } else {
        imageView.setImageDrawable(drawable);
    }
}

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.//  w ww .  j  a v a2s .  c  om
 *
 * @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:gr.unfold.android.tsibato.images.ImageWorker.java

@TargetApi(16)
@SuppressWarnings("deprecation")
private void setImageDrawable(ImageView imageView, Drawable drawable) {
    if (mFadeInBitmap) {
        // Transition drawable with a transparent drawable and the final drawable
        final TransitionDrawable td = new TransitionDrawable(
                new Drawable[] { new ColorDrawable(android.R.color.transparent), drawable });
        // Set background to loading bitmap
        if (Utils.hasJellyBean()) {
            imageView.setBackground(new BitmapDrawable(mResources, mLoadingBitmap));
        } else {// w w w  .jav a2s.  c  om
            imageView.setBackgroundDrawable(new BitmapDrawable(mResources, mLoadingBitmap));
        }

        imageView.setImageDrawable(td);
        td.startTransition(FADE_IN_TIME);
    } else {
        imageView.setImageDrawable(drawable);
    }
}