Example usage for android.graphics.drawable TransitionDrawable getNumberOfLayers

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

Introduction

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

Prototype

public int getNumberOfLayers() 

Source Link

Document

Returns the number of layers contained within this layer drawable.

Usage

From source file:io.plaidapp.ui.FeedAdapter.java

@NonNull
private DribbbleShotHolder createDribbbleShotHolder(ViewGroup parent) {
    final DribbbleShotHolder holder = new DribbbleShotHolder(
            layoutInflater.inflate(R.layout.dribbble_shot_item, parent, false));
    holder.image.setOnClickListener(new View.OnClickListener() {
        @Override/*from   ww  w.  j  ava 2 s .  c  om*/
        public void onClick(View view) {
            holder.itemView
                    .setTransitionName(holder.itemView.getResources().getString(R.string.transition_shot));
            holder.itemView.setBackgroundColor(ContextCompat.getColor(host, R.color.background_light));
            Intent intent = new Intent();
            intent.setClass(host, DribbbleShot.class);
            intent.putExtra(DribbbleShot.EXTRA_SHOT, (Shot) getItem(holder.getAdapterPosition()));
            setGridItemContentTransitions(holder.itemView);
            ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(host,
                    Pair.create(view, host.getString(R.string.transition_shot)),
                    Pair.create(view, host.getString(R.string.transition_shot_background)));
            host.startActivity(intent, options.toBundle());
        }
    });
    // play animated GIFs whilst touched
    holder.image.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // get the image and check if it's an animated GIF
            final Drawable drawable = holder.image.getDrawable();
            if (drawable == null)
                return false;
            GifDrawable gif = null;
            if (drawable instanceof GifDrawable) {
                gif = (GifDrawable) drawable;
            } else if (drawable instanceof TransitionDrawable) {
                // we fade in images on load which uses a TransitionDrawable; check its layers
                TransitionDrawable fadingIn = (TransitionDrawable) drawable;
                for (int i = 0; i < fadingIn.getNumberOfLayers(); i++) {
                    if (fadingIn.getDrawable(i) instanceof GifDrawable) {
                        gif = (GifDrawable) fadingIn.getDrawable(i);
                        break;
                    }
                }
            }
            if (gif == null)
                return false;
            // GIF found, start/stop it on press/lift
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                gif.start();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                gif.stop();
                break;
            }
            return false;
        }
    });
    return holder;
}

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;/*from   w  w w. j  a va  2s .co  m*/
                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:github.daneren2005.dsub.util.ImageLoader.java

private void setImage(View view, final 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) {
        final ImageView imageView = (ImageView) view;
        if (crossfade && drawable != null) {
            Drawable existingDrawable = imageView.getDrawable();
            if (existingDrawable == null) {
                Bitmap emptyImage;//from w  w w . j  a v a 2 s  . c o  m
                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(context.getResources(), emptyImage);
            } else if (existingDrawable instanceof TransitionDrawable) {
                // This should only ever be used if user is skipping through many songs quickly
                TransitionDrawable tmp = (TransitionDrawable) existingDrawable;
                existingDrawable = tmp.getDrawable(tmp.getNumberOfLayers() - 1);
            }
            if (existingDrawable != null && drawable != null) {
                Drawable[] layers = new Drawable[] { existingDrawable, drawable };
                final TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
                imageView.setImageDrawable(transitionDrawable);
                transitionDrawable.startTransition(250);

                // Get rid of transition drawable after transition occurs
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // Only execute if still on same transition drawable
                        if (imageView.getDrawable() == transitionDrawable) {
                            imageView.setImageDrawable(drawable);
                        }
                    }
                }, 500L);
            } else {
                imageView.setImageDrawable(drawable);
            }
        } else {
            imageView.setImageDrawable(drawable);
        }
    }
}

From source file:io.plaidapp.core.ui.FeedAdapter.java

@NonNull
private DribbbleShotHolder createDribbbleShotHolder(ViewGroup parent) {
    final DribbbleShotHolder holder = new DribbbleShotHolder(
            layoutInflater.inflate(R.layout.dribbble_shot_item, parent, false));
    holder.image.setBadgeColor(initialGifBadgeColor);
    holder.image.setOnClickListener(view -> {
        Intent intent = ActivityHelper.intentTo(Activities.Dribbble.Shot.INSTANCE);
        intent.putExtra(Activities.Dribbble.Shot.EXTRA_SHOT_ID, getItem(holder.getAdapterPosition()).getId());
        setGridItemContentTransitions(holder.image);
        ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(host,
                Pair.create(view, host.getString(R.string.transition_shot)),
                Pair.create(view, host.getString(R.string.transition_shot_background)));
        host.startActivityForResult(intent, REQUEST_CODE_VIEW_SHOT, options.toBundle());
    });//  www .  j  a v a2s  .co  m
    // play animated GIFs whilst touched
    holder.image.setOnTouchListener((v, event) -> {
        // check if it's an event we care about, else bail fast
        final int action = event.getAction();
        if (!(action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP
                || action == MotionEvent.ACTION_CANCEL)) {
            return false;
        }

        // get the image and check if it's an animated GIF
        final Drawable drawable = holder.image.getDrawable();
        if (drawable == null)
            return false;
        GifDrawable gif = null;
        if (drawable instanceof GifDrawable) {
            gif = (GifDrawable) drawable;
        } else if (drawable instanceof TransitionDrawable) {
            // we fade in images on load which uses a TransitionDrawable; check its layers
            TransitionDrawable fadingIn = (TransitionDrawable) drawable;
            for (int i = 0; i < fadingIn.getNumberOfLayers(); i++) {
                if (fadingIn.getDrawable(i) instanceof GifDrawable) {
                    gif = (GifDrawable) fadingIn.getDrawable(i);
                    break;
                }
            }
        }
        if (gif == null)
            return false;
        // GIF found, start/stop it on press/lift
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            gif.start();
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            gif.stop();
            break;
        }
        return false;
    });
    return holder;
}

From source file:io.plaidapp.ui.FeedAdapter.java

@NonNull
private DribbbleShotHolder createDribbbleShotHolder(ViewGroup parent) {
    final DribbbleShotHolder holder = new DribbbleShotHolder(
            layoutInflater.inflate(R.layout.dribbble_shot_item, parent, false));
    holder.image.setBadgeColor(initialGifBadgeColor);
    holder.image.setOnClickListener(new View.OnClickListener() {
        @Override//from   ww w.j a v  a 2  s . c  o m
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setClass(host, DribbbleShot.class);
            intent.putExtra(DribbbleShot.EXTRA_SHOT, (Shot) getItem(holder.getAdapterPosition()));
            setGridItemContentTransitions(holder.image);
            ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(host,
                    Pair.create(view, host.getString(R.string.transition_shot)),
                    Pair.create(view, host.getString(R.string.transition_shot_background)));
            host.startActivityForResult(intent, REQUEST_CODE_VIEW_SHOT, options.toBundle());
        }
    });
    // play animated GIFs whilst touched
    holder.image.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // check if it's an event we care about, else bail fast
            final int action = event.getAction();
            if (!(action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP
                    || action == MotionEvent.ACTION_CANCEL))
                return false;

            // get the image and check if it's an animated GIF
            final Drawable drawable = holder.image.getDrawable();
            if (drawable == null)
                return false;
            GifDrawable gif = null;
            if (drawable instanceof GifDrawable) {
                gif = (GifDrawable) drawable;
            } else if (drawable instanceof TransitionDrawable) {
                // we fade in images on load which uses a TransitionDrawable; check its layers
                TransitionDrawable fadingIn = (TransitionDrawable) drawable;
                for (int i = 0; i < fadingIn.getNumberOfLayers(); i++) {
                    if (fadingIn.getDrawable(i) instanceof GifDrawable) {
                        gif = (GifDrawable) fadingIn.getDrawable(i);
                        break;
                    }
                }
            }
            if (gif == null)
                return false;
            // GIF found, start/stop it on press/lift
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                gif.start();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                gif.stop();
                break;
            }
            return false;
        }
    });
    return holder;
}

From source file:com.silentcircle.contacts.ContactPhotoManager.java

/**
 * Checks if the photo is present in cache.  If so, sets the photo on the view.
 *
 * @return false if the photo needs to be (re)loaded from the provider.
 *//*from   ww  w .j  a  v a 2  s . c  o m*/
private boolean loadCachedPhoto(ImageView view, Request request, boolean fadeIn) {
    BitmapHolder holder = mBitmapHolderCache.get(request.getKey());
    if (holder == null) {
        // The bitmap has not been loaded ==> show default avatar
        request.applyDefaultImage(view);
        return false;
    }

    if (holder.bytes == null) {
        request.applyDefaultImage(view);
        return holder.fresh;
    }

    Bitmap cachedBitmap = holder.bitmapRef == null ? null : holder.bitmapRef.get();
    if (cachedBitmap == null) {
        if (holder.bytes.length < 8 * 1024) {
            // Small thumbnails are usually quick to inflate. Let's do that on the UI thread
            inflateBitmap(holder, request.getRequestedExtent());
            cachedBitmap = holder.bitmap;
            if (cachedBitmap == null)
                return false;
        } else {
            // This is bigger data. Let's send that back to the Loader so that we can
            // inflate this in the background
            request.applyDefaultImage(view);
            return false;
        }
    }

    final Drawable previousDrawable = view.getDrawable();
    if (fadeIn && previousDrawable != null) {
        final Drawable[] layers = new Drawable[2];
        // Prevent cascade of TransitionDrawables.
        if (previousDrawable instanceof TransitionDrawable) {
            final TransitionDrawable previousTransitionDrawable = (TransitionDrawable) previousDrawable;
            layers[0] = previousTransitionDrawable
                    .getDrawable(previousTransitionDrawable.getNumberOfLayers() - 1);
        } else {
            layers[0] = previousDrawable;
        }
        layers[1] = new BitmapDrawable(mContext.getResources(), cachedBitmap);
        TransitionDrawable drawable = new TransitionDrawable(layers);
        view.setImageDrawable(drawable);
        drawable.startTransition(FADE_TRANSITION_DURATION);
    } else {
        view.setImageBitmap(cachedBitmap);
    }

    // Put the bitmap in the LRU cache. But only do this for images that are small enough
    // (we require that at least six of those can be cached at the same time)
    int byteCount = (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1)
            ? cachedBitmap.getRowBytes() * cachedBitmap.getHeight()
            : cachedBitmap.getByteCount();
    if (byteCount < mBitmapCache.maxSize() / 6) {
        mBitmapCache.put(request.getKey(), cachedBitmap);
    }
    holder.bitmap = null; // Soften the reference

    return holder.fresh;
}

From source file:com.silentcircle.contacts.ContactPhotoManagerNew.java

/**
 * Checks if the photo is present in cache.  If so, sets the photo on the view.
 *
 * @return false if the photo needs to be (re)loaded from the provider.
 *//*w  w w.j  a  va2 s  .c om*/
private boolean loadCachedPhoto(ImageView view, Request request, boolean fadeIn) {
    BitmapHolder holder = mBitmapHolderCache.get(request.getKey());
    if (holder == null) {
        // The bitmap has not been loaded ==> show default avatar
        request.applyDefaultImage(view, request.mIsCircular);
        return false;
    }

    if (holder.bytes == null) {
        request.applyDefaultImage(view, request.mIsCircular);
        return holder.fresh;
    }

    Bitmap cachedBitmap = holder.bitmapRef == null ? null : holder.bitmapRef.get();
    if (cachedBitmap == null) {
        if (holder.bytes.length < 8 * 1024) {
            // Small thumbnails are usually quick to inflate. Let's do that on the UI thread
            inflateBitmap(holder, request.getRequestedExtent());
            cachedBitmap = holder.bitmap;
            if (cachedBitmap == null)
                return false;
        } else {
            // This is bigger data. Let's send that back to the Loader so that we can
            // inflate this in the background
            request.applyDefaultImage(view, request.mIsCircular);
            return false;
        }
    }

    final Drawable previousDrawable = view.getDrawable();
    if (fadeIn && previousDrawable != null) {
        final Drawable[] layers = new Drawable[2];
        // Prevent cascade of TransitionDrawables.
        if (previousDrawable instanceof TransitionDrawable) {
            final TransitionDrawable previousTransitionDrawable = (TransitionDrawable) previousDrawable;
            layers[0] = previousTransitionDrawable
                    .getDrawable(previousTransitionDrawable.getNumberOfLayers() - 1);
        } else {
            layers[0] = previousDrawable;
        }
        layers[1] = getDrawableForBitmap(mContext.getResources(), cachedBitmap, request);
        TransitionDrawable drawable = new TransitionDrawable(layers);
        view.setImageDrawable(drawable);
        drawable.startTransition(FADE_TRANSITION_DURATION);
    } else {
        view.setImageDrawable(getDrawableForBitmap(mContext.getResources(), cachedBitmap, request));
    }

    // Put the bitmap in the LRU cache. But only do this for images that are small enough
    // (we require that at least six of those can be cached at the same time)
    if (cachedBitmap.getByteCount() < mBitmapCache.maxSize() / 6) {
        mBitmapCache.put(request.getKey(), cachedBitmap);
    }

    // hack: Don't allow resources from external requests to expire easily
    Uri uri = request.getUri();
    if (uri != null) {
        final String scheme = uri.getScheme();
        if (scheme.equals("http") || scheme.equals("https")) {
            holder.fresh = true;
        }
    }

    // Soften the reference
    holder.bitmap = null;

    return holder.fresh;
}

From source file:com.android.contacts.common.ContactPhotoManager.java

/**
 * Checks if the photo is present in cache.  If so, sets the photo on the view.
 *
 * @return false if the photo needs to be (re)loaded from the provider.
 *///from  w  w w.  j a  v  a2  s .  co m
private boolean loadCachedPhoto(ImageView view, Request request, boolean fadeIn) {
    BitmapHolder holder = mBitmapHolderCache.get(request.getKey());
    if (holder == null) {
        // The bitmap has not been loaded ==> show default avatar
        request.applyDefaultImage(view, request.mIsCircular);
        return false;
    }

    if (holder.bytes == null) {
        request.applyDefaultImage(view, request.mIsCircular);
        return holder.fresh;
    }

    Bitmap cachedBitmap = holder.bitmapRef == null ? null : holder.bitmapRef.get();
    if (cachedBitmap == null) {
        if (holder.bytes.length < 8 * 1024) {
            // Small thumbnails are usually quick to inflate. Let's do that on the UI thread
            inflateBitmap(holder, request.getRequestedExtent());
            cachedBitmap = holder.bitmap;
            if (cachedBitmap == null)
                return false;
        } else {
            // This is bigger data. Let's send that back to the Loader so that we can
            // inflate this in the background
            request.applyDefaultImage(view, request.mIsCircular);
            return false;
        }
    }

    final Drawable previousDrawable = view.getDrawable();
    if (fadeIn && previousDrawable != null) {
        final Drawable[] layers = new Drawable[2];
        // Prevent cascade of TransitionDrawables.
        if (previousDrawable instanceof TransitionDrawable) {
            final TransitionDrawable previousTransitionDrawable = (TransitionDrawable) previousDrawable;
            layers[0] = previousTransitionDrawable
                    .getDrawable(previousTransitionDrawable.getNumberOfLayers() - 1);
        } else {
            layers[0] = previousDrawable;
        }
        layers[1] = getDrawableForBitmap(mContext.getResources(), cachedBitmap, request);
        TransitionDrawable drawable = new TransitionDrawable(layers);
        view.setImageDrawable(drawable);
        drawable.startTransition(FADE_TRANSITION_DURATION);
    } else {
        view.setImageDrawable(getDrawableForBitmap(mContext.getResources(), cachedBitmap, request));
    }

    // Put the bitmap in the LRU cache. But only do this for images that are small enough
    // (we require that at least six of those can be cached at the same time)
    if (cachedBitmap.getByteCount() < mBitmapCache.maxSize() / 6) {
        mBitmapCache.put(request.getKey(), cachedBitmap);
    }

    // Soften the reference
    holder.bitmap = null;

    return holder.fresh;
}

From source file:com.android.contacts.ContactPhotoManager.java

/**
 * Checks if the photo is present in cache.  If so, sets the photo on the view.
 *
 * @return false if the photo needs to be (re)loaded from the provider.
 *///from   www  .j a v a  2  s  .c om
private boolean loadCachedPhoto(ImageView view, Request request, boolean fadeIn) {
    BitmapHolder holder = mBitmapHolderCache.get(request.getKey());
    if (holder == null) {
        // The bitmap has not been loaded ==> show default avatar
        request.applyDefaultImage(view, request.mIsCircular);
        return false;
    }

    if (holder.bytes == null || holder.bytes.length == 0) {
        request.applyDefaultImage(view, request.mIsCircular);
        return holder.fresh;
    }

    Bitmap cachedBitmap = holder.bitmapRef == null ? null : holder.bitmapRef.get();
    if (cachedBitmap == null) {
        if (holder.bytes.length < 8 * 1024) {
            // Small thumbnails are usually quick to inflate. Let's do that on the UI thread
            inflateBitmap(holder, request.getRequestedExtent());
            cachedBitmap = holder.bitmap;
            if (cachedBitmap == null)
                return false;
        } else {
            // This is bigger data. Let's send that back to the Loader so that we can
            // inflate this in the background
            request.applyDefaultImage(view, request.mIsCircular);
            return false;
        }
    }

    final Drawable previousDrawable = view.getDrawable();
    if (fadeIn && previousDrawable != null) {
        final Drawable[] layers = new Drawable[2];
        // Prevent cascade of TransitionDrawables.
        if (previousDrawable instanceof TransitionDrawable) {
            final TransitionDrawable previousTransitionDrawable = (TransitionDrawable) previousDrawable;
            layers[0] = previousTransitionDrawable
                    .getDrawable(previousTransitionDrawable.getNumberOfLayers() - 1);
        } else {
            layers[0] = previousDrawable;
        }
        layers[1] = getDrawableForBitmap(mContext.getResources(), cachedBitmap, request);
        TransitionDrawable drawable = new TransitionDrawable(layers);
        view.setImageDrawable(drawable);
        drawable.startTransition(FADE_TRANSITION_DURATION);
    } else {
        view.setImageDrawable(getDrawableForBitmap(mContext.getResources(), cachedBitmap, request));
    }

    // Put the bitmap in the LRU cache. But only do this for images that are small enough
    // (we require that at least six of those can be cached at the same time)
    if (cachedBitmap.getByteCount() < mBitmapCache.maxSize() / 6) {
        mBitmapCache.put(request.getKey(), cachedBitmap);
    }

    // Soften the reference
    holder.bitmap = null;

    return holder.fresh;
}