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: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. jav a2s  .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   ww w  .j  a  v a2 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, 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;
}

From source file:org.chromium.chrome.browser.toolbar.ToolbarPhone.java

@Override
protected void handleFindToolbarStateChange(boolean showing) {
    setVisibility(showing ? View.GONE : View.VISIBLE);
    TransitionDrawable shadowDrawable = (TransitionDrawable) mToolbarShadow.getDrawable();
    if (showing) {
        shadowDrawable.startTransition(URL_FOCUS_CHANGE_ANIMATION_DURATION_MS);
    } else {/*from  w  ww.  j a  v  a2  s .c om*/
        shadowDrawable.reverseTransition(URL_FOCUS_CHANGE_ANIMATION_DURATION_MS);
    }
}

From source file:com.fastbootmobile.encore.app.fragments.ArtistFragment.java

private void loadArt(final boolean materialTransition) {
    AlbumArtHelper.retrieveAlbumArt(getResources(), new AlbumArtHelper.AlbumArtListener() {
        @Override//from w  w  w .ja va 2 s .  c  om
        public void onArtLoaded(RecyclingBitmapDrawable output, BoundEntity request) {
            try {
                if (output != null && !isDetached()) {
                    mHeroImage = output.getBitmap();

                    if (materialTransition) {
                        MaterialTransitionDrawable mtd = new MaterialTransitionDrawable(
                                (BitmapDrawable) getResources().getDrawable(R.drawable.ic_cloud_offline),
                                (BitmapDrawable) getResources().getDrawable(R.drawable.album_placeholder));
                        mtd.transitionTo(output);

                        mHeroImageView.setImageDrawable(mtd);
                    } else {
                        final TransitionDrawable transition = new TransitionDrawable(
                                new Drawable[] { mHeroImageView.getDrawable(), output });

                        // Make sure the transition happens after the activity animation is done,
                        // otherwise weird sliding occurs.
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mHeroImageView.setImageDrawable(transition);
                                transition.startTransition(500);
                            }
                        }, 600);
                    }

                    generateHeroPalette();
                }
            } catch (IllegalStateException ignore) {
                // We might have left the activity, so go on
            }
        }
    }, mArtist, -1, false);
}

From source file:org.chromium.chrome.browser.toolbar.ToolbarPhone.java

@Override
public void onUrlFocusChange(final boolean hasFocus) {
    super.onUrlFocusChange(hasFocus);

    triggerUrlFocusAnimation(hasFocus);//from   w  w w. j av a2 s. co  m

    TransitionDrawable shadowDrawable = (TransitionDrawable) mToolbarShadow.getDrawable();
    if (hasFocus) {
        dismissTabSwitcherCallout();
        shadowDrawable.startTransition(URL_FOCUS_CHANGE_ANIMATION_DURATION_MS);
    } else {
        shadowDrawable.reverseTransition(URL_FOCUS_CHANGE_ANIMATION_DURATION_MS);
    }
}

From source file:com.bitmaphandler.ImageWorker.java

/**
 * Called when the processing is complete and the final drawable should be 
 * set on the ImageView./* w w  w  .j  av a 2 s . co  m*/
 *
 * @param imageView
 * @param drawable
 */
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
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 });
        if (useLoadingImage && mLoadingBitmap != null) {
            // Set background to loading bitmap
            if (imageView instanceof CircleImageView) {
                if (!Utils.hasJellyBean()) {
                    imageView.setBackgroundDrawable(new BitmapDrawable(mResources,
                            CircleImageView.getCroppedBitmap(mLoadingBitmap, mLoadingBitmap.getWidth())));
                } else {
                    imageView.setBackground(new BitmapDrawable(mResources,
                            CircleImageView.getCroppedBitmap(mLoadingBitmap, mLoadingBitmap.getWidth())));
                }
            } else if (imageView instanceof RoundedBitmapDisplayer) {
                if (!Utils.hasJellyBean()) {
                    imageView.setBackgroundDrawable(
                            new BitmapDrawable(mResources, RoundedBitmapDisplayer.roundCorners(mLoadingBitmap,
                                    imageView, ((RoundedBitmapDisplayer) imageView).getRoundPixle())));
                } else {
                    imageView.setBackground(new BitmapDrawable(mResources, RoundedBitmapDisplayer.roundCorners(
                            mLoadingBitmap, imageView, ((RoundedBitmapDisplayer) imageView).getRoundPixle())));
                }
            } else {
                if (!Utils.hasJellyBean()) {
                    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.xmobileapp.rockplayer.RockPlayer.java

/*********************************************
 * /*w w  w  . j av  a 2 s  .co  m*/
 * getCurrentPlaying
 *    get what the player service is currently
 *    playing and update UI
 *
 *********************************************/
public void getCurrentPlaying() {
    //if(true) return;

    /*
     * ask the player service if/what he is playing
     */
    int albumCursorPosition = 0;
    int songCursorPosition = 0;
    try {
        albumCursorPosition = this.playerServiceIface.getAlbumCursorPosition();
        songCursorPosition = this.playerServiceIface.getSongCursorPosition();
    } catch (RemoteException e) {
        e.printStackTrace();
    }
    if (albumCursorPosition == -1 || songCursorPosition == -1) {
        //         Log.i("GETCURPLAY", "BOTH CURSORS ARE -1");
        //         try{
        //            Random rand = new Random();
        //            if(albumCursor != null && rand != null)
        //               albumCursorPosition = rand.nextInt(albumCursor.getCount()-1);
        //            else
        //               albumCursorPosition = 1;
        //            albumCursorPositionPlaying = albumCursorPosition;
        //            songCursorPosition = 0;
        //         } catch (Exception e) {
        //            try{
        //               if(albumCursor != null)
        //                  Log.i("EXCP", ""+albumCursor.getCount());
        //            } catch (Exception ee){
        //               ee.printStackTrace();
        //            }
        //            e.printStackTrace();
        //            albumCursorPosition = 0;
        //            albumCursorPositionPlaying = albumCursorPosition;
        //            songCursorPosition = 0;
        //         }
        //         try {
        //            playerServiceIface.setAlbumCursorPosition(albumCursorPosition);
        //            playerServiceIface.setSongCursorPosition(songCursorPosition);
        //            playerServiceIface.play(albumCursorPosition, songCursorPosition);
        //            playerServiceIface.pause();
        //            // HACKzzzzzzzzz we need a paused event from the service
        //            this.stopSongProgress();
        //         } catch (RemoteException e) {
        //            e.printStackTrace();
        //         }
    } else {
        try {
            if (playerServiceIface.isPlaying()) {
                TransitionDrawable playPauseTDrawable = (TransitionDrawable) playPauseImage.getDrawable();
                playPauseTDrawable.setCrossFadeEnabled(true);
                playPauseTDrawable.startTransition(1);
                playPauseTDrawable.invalidateSelf();
                //                this.playPauseImage.setImageResource(android.R.drawable.ic_media_pause);          }
            } else {
                albumCursorPosition = playerServiceIface.getAlbumCursorPosition();
                songCursorPosition = playerServiceIface.getSongCursorPosition();
                albumCursorPositionPlaying = albumCursorPosition;

                //               Log.i("INIT", albumCursorPosition+" "+songCursorPosition);
                //               playerServiceIface.play(albumCursorPositionPlaying,
                //                                 songCursorPosition);
                //               playerServiceIface.pause();
                this.stopSongProgress();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // TODO: ask if he is playing
    /*
     * If media Library is empty just show a popup
     */
    if (albumCursor.getCount() == 0) {
        Dialog noMediaDialog = new Dialog(this);
        noMediaDialog.setTitle("No Media Available");
        //TextView noMediaText = new TextView(this);
        //noMediaText.setText("Please add some Music to your SD Card");
        //noMediaDialog.setContentView(noMediaText);
        noMediaDialog.show();
        return;
    }

    /*
     * Go to the current playing Media
     */
    Log.i("GETCURPLAY", "MOVING CURSORS");

    try {
        albumCursor.moveToPosition(albumCursorPosition);
        albumCursorPositionPlaying = albumCursorPosition;
        songCursor = initializeSongCursor(
                albumCursor.getString(albumCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM)));
        songCursor.moveToPosition(songCursorPosition);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Log.i("GETCURPLAY", "GET ALBUM ART");

    try {
        //         /*
        //          * get albumArt
        //          */
        //         String albumCoverPath = albumCursor.getString(
        //                              albumCursor.getColumnIndexOrThrow(
        //                                 MediaStore.Audio.Albums.ALBUM_ART));
        //          // if it does not exist in database look for our dir
        //          if(albumCoverPath == null){
        //             String artistName = albumCursor.getString(
        //                              albumCursor.getColumnIndexOrThrow(
        //                                    MediaStore.Audio.Albums.ARTIST));
        //             String albumName = albumCursor.getString(
        //                              albumCursor.getColumnIndexOrThrow(
        //                                    MediaStore.Audio.Albums.ALBUM));
        //             String path = this.FILEX_ALBUM_ART_PATH+
        //                         validateFileName(artistName)+
        //                        " - "+
        //                        validateFileName(albumName)+
        //                        FILEX_FILENAME_EXTENSION;
        //             File albumCoverFilePath = new File(path);
        //            if(albumCoverFilePath.exists() && albumCoverFilePath.length() > 0){
        //               albumCoverPath = path;
        //            }
        //          }
        //          
        //          Log.i("GETCURPLAY", "UPDATING UI COMPONENT");
        //
        //           /*
        //           * Update currentPlaying albumArt UI component
        //           */
        //         if(albumCoverPath != null){
        //            // TODO:
        //            // adjust sample size
        //            Options opts = new Options();
        //            opts.inSampleSize = 1;
        //            Bitmap albumCoverBitmap = BitmapFactory.decodeFile(albumCoverPath, opts);
        //            this.currentAlbumPlayingImageView.setImageBitmap(albumCoverBitmap);
        //         } else {
        //            // TODO:
        //            // adjust sample size dynamically
        //            Options opts = new Options();
        //            opts.inSampleSize = 1;
        //            Bitmap albumCoverBitmap = BitmapFactory.decodeResource(this.context.getResources(),
        //                                                R.drawable.albumart_mp_unknown, opts);
        //            if(albumCoverBitmap != null)
        //               this.currentAlbumPlayingImageView.setImageBitmap(albumCoverBitmap);
        //         }

        if (VIEW_STATE == FULLSCREEN_VIEW)
            this.currentAlbumPlayingImageView.setImageBitmap(
                    albumAdapter.getAlbumBitmap(albumCursor.getPosition(), BITMAP_SIZE_FULLSCREEN));
        else
            this.currentAlbumPlayingImageView
                    .setImageBitmap(albumAdapter.getAlbumBitmap(albumCursor.getPosition(), BITMAP_SIZE_NORMAL));

        if (showFrame)
            this.currentAlbumPlayingOverlayImageView.setVisibility(View.VISIBLE);
        else
            this.currentAlbumPlayingOverlayImageView.setVisibility(View.GONE);

        Log.i("GETCURPLAY", "UPDATING REST OF UI");

        /*
         * Update currentPlaying artist UI component
         */
        this.updateArtistTextUI();
        /*
         * Update currentPlaying song UI component
         */
        this.updateSongTextUI();
        /*
         * Update Song Progress
         */
        this.updateSongProgress();

        Log.i("GETCURPLAY", "CENTER ALBUMLIST");

        /*
         * Center Album List
         */
        albumNavigatorList.setSelectionFromTop(albumCursorPositionPlaying,
                (int) Math.round((display.getHeight() - 20) / 2.0
                        - (display.getWidth() * (1 - CURRENT_PLAY_SCREEN_FRACTION_LANDSCAPE))));
        this.albumNavigatorScrollListener.onScrollStateChanged(this.albumNavigatorList,
                OnScrollListener.SCROLL_STATE_IDLE);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.xmobileapp.rockplayer.RockPlayer.java

/*************************************
 * //from   www  . j  a  va  2s  . c  om
 * playPauseClickListenerHelper
 * 
 *************************************/
public void playPauseClickListenerHelper() {
    try {
        // TODO: UI related updates----
        //   these are just mockups
        if (playerServiceIface.isPlaying()) {
            playerServiceIface.pause();
            //            AlphaAnimation albumPlayingFadeOut = new AlphaAnimation((float)1.0, (float)0.66);
            //            albumPlayingFadeOut.setFillAfter(true);
            //            albumPlayingFadeOut.setDuration(200);
            //            currentAlbumPlayingLayout.startAnimation(albumPlayingFadeOut);
            //playPauseImage.setImageResource(android.R.drawable.ic_media_play);
            TransitionDrawable playPauseTDrawable = (TransitionDrawable) playPauseImage.getDrawable();
            playPauseTDrawable.setCrossFadeEnabled(true);
            playPauseTDrawable.reverseTransition(300);
            playPauseTDrawable.invalidateSelf();
            invalidateCurrentSongLayout.sendEmptyMessageDelayed(0, 50);
            invalidateCurrentSongLayout.sendEmptyMessageDelayed(0, 100);
            invalidateCurrentSongLayout.sendEmptyMessageDelayed(0, 150);
            invalidateCurrentSongLayout.sendEmptyMessageDelayed(0, 200);
            invalidateCurrentSongLayout.sendEmptyMessageDelayed(0, 250);
            //            TransitionDrawable playPauseBgTDrawable = (TransitionDrawable) playPauseImage.getBackground();
            //            playPauseBgTDrawable.startTransition(500);
            //            playPauseBgTDrawable.invalidateSelf();
            //            playPauseOverlayHandler.sendEmptyMessageDelayed(0, 500);
            //            currentAlbumPlayingLayout.invalidate();
            //TODO: reverse transition with an handler
            //            
            //            playPauseImageOverlay.setVisibility(View.VISIBLE);
            //            AlphaAnimation playPauseImageOverlayFadeIn = new AlphaAnimation(0.0f, 1.0f);
            //            playPauseImageOverlayFadeIn.setFillAfter(true);
            //            playPauseImageOverlayFadeIn.setDuration(200);
            //            playPauseImageOverlayFadeIn.setAnimationListener(playPauseOverlayFadeInAnimationListener);
            //            playPauseImageOverlay.startAnimation(playPauseImageOverlayFadeIn);
            //currentAlbumPlayingLayout.setBackgroundColor(Color.argb(128, 255, 255, 255));
            if (songProgressTimer != null)
                songProgressTimer.cancel();
        } else {
            //playerServiceIface.resume(); - use a delayed timer to not interfere with the button animations
            Message msg = new Message();
            msg.what = 0;
            playerServiceResumeHandler.sendMessageDelayed(new Message(), 900);
            //            AlphaAnimation albumPlayingFadeIn = new AlphaAnimation((float)0.66, (float)1.0);
            //            albumPlayingFadeIn.setFillAfter(true);
            //            albumPlayingFadeIn.setDuration(200);
            //            currentAlbumPlayingLayout.startAnimation(albumPlayingFadeIn);
            //playPauseImage.setImageResource(android.R.drawable.ic_media_pause);
            TransitionDrawable playPauseTDrawable = (TransitionDrawable) playPauseImage.getDrawable();
            playPauseTDrawable.setCrossFadeEnabled(true);
            playPauseTDrawable.startTransition(500);
            playPauseTDrawable.invalidateSelf();
            invalidateCurrentSongLayout.sendEmptyMessageDelayed(0, 150);
            invalidateCurrentSongLayout.sendEmptyMessageDelayed(0, 300);
            invalidateCurrentSongLayout.sendEmptyMessageDelayed(0, 450);
            invalidateCurrentSongLayout.sendEmptyMessageDelayed(0, 600);
            invalidateCurrentSongLayout.sendEmptyMessageDelayed(0, 750);
            //            TransitionDrawable playPauseBgTDrawable = (TransitionDrawable) playPauseImage.getBackground();
            //            playPauseBgTDrawable.startTransition(500);
            //            playPauseBgTDrawable.invalidateSelf();
            //            playPauseOverlayHandler.sendEmptyMessageDelayed(0, 500);
            //            currentAlbumPlayingLayout.invalidate();
            //TODO: reverse transition with an handler
            //            
            //            playPauseImageOverlay.setVisibility(View.VISIBLE);
            //            AlphaAnimation playPauseImageOverlayFadeIn = new AlphaAnimation(0.0f, 1.0f);
            //            playPauseImageOverlayFadeIn.setFillAfter(true);
            //            playPauseImageOverlayFadeIn.setDuration(250);
            //            playPauseImageOverlayFadeIn.setAnimationListener(playPauseOverlayFadeInAnimationListener);
            //            playPauseImageOverlay.startAnimation(playPauseImageOverlayFadeIn);
            //currentAlbumPlayingLayout.setBackgroundColor(Color.argb(0, 255, 255, 255));
            //Log.i("RES", "1");
            //            songProgressTimer = new Timer();
            //            Log.i("RES", "7");
            //            songProgressTimer.scheduleAtFixedRate(new SongProgressTimerTask(), 100, 1000);
            //            Log.i("RES", "8");
            updateSongTextUI(); // starts the progress timer
            //triggerSongProgress();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}