Example usage for android.graphics.drawable TransitionDrawable setCrossFadeEnabled

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

Introduction

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

Prototype

public void setCrossFadeEnabled(boolean enabled) 

Source Link

Document

Enables or disables the cross fade of the drawables.

Usage

From source file:Main.java

/**
 * @see "http://blog.peterkuterna.net/2011/09/simple-crossfade-on-imageview.html"
 * with modifications by Thomas Suarez.//w  ww.  j  a  v  a 2s  .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.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 ava 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.benefit.buy.library.http.query.callback.BitmapAjaxCallback.java

private static void setBmAnimate(ImageView iv, Bitmap bm, Bitmap preset, int fallback, int animation,
        float ratio, float anchor, int source) {
    bm = filter(iv, bm, fallback);/*from w  w w .j av a 2s  .c  om*/
    if (bm == null) {
        iv.setImageBitmap(null);
        return;
    }
    Drawable d = makeDrawable(iv, bm, ratio, anchor);
    Animation anim = null;
    if (fadeIn(animation, source)) {
        if (preset == null) {
            anim = new AlphaAnimation(0, 1);
            anim.setInterpolator(new DecelerateInterpolator());
            anim.setDuration(FADE_DUR);
        } else {
            Drawable pd = makeDrawable(iv, preset, ratio, anchor);
            Drawable[] ds = new Drawable[] { pd, d };
            TransitionDrawable td = new TransitionDrawable(ds);
            td.setCrossFadeEnabled(true);
            td.startTransition(FADE_DUR);
            d = td;
        }
    } else if (animation > 0) {
        anim = AnimationUtils.loadAnimation(iv.getContext(), animation);
    }
    iv.setImageDrawable(d);
    if (anim != null) {
        anim.setStartTime(AnimationUtils.currentAnimationTimeMillis());
        iv.startAnimation(anim);
    } else {
        iv.setAnimation(null);
    }
}

From source file:com.appbase.androidquery.callback.BitmapAjaxCallback.java

private static void setBmAnimate(ImageView iv, Bitmap bm, Bitmap preset, int fallback, int animation,
        float ratio, float anchor, int source) {

    bm = filter(iv, bm, fallback);/*from   ww w .  j a v  a 2 s  . c o  m*/
    if (bm == null) {
        iv.setImageBitmap(null);
        return;
    }

    Drawable d = makeDrawable(iv, bm, ratio, anchor);
    Animation anim = null;

    if (fadeIn(animation, source)) {
        if (preset == null) {
            anim = new AlphaAnimation(0, 1);
            anim.setInterpolator(new DecelerateInterpolator());
            anim.setDuration(FADE_DUR);
        } else {

            Drawable pd = makeDrawable(iv, preset, ratio, anchor);
            Drawable[] ds = new Drawable[] { pd, d };
            TransitionDrawable td = new TransitionDrawable(ds);
            td.setCrossFadeEnabled(true);
            td.startTransition(FADE_DUR);
            d = td;
        }
    } else if (animation > 0) {
        anim = AnimationUtils.loadAnimation(iv.getContext(), animation);
    }

    iv.setImageDrawable(d);

    if (anim != null) {
        anim.setStartTime(AnimationUtils.currentAnimationTimeMillis());
        iv.startAnimation(anim);
    } else {
        iv.setAnimation(null);
    }
}

From source file:com.appsimobile.appsii.module.weather.WeatherActivity.java

void showDrawable(Drawable drawable, boolean isImmediate) {
    mBackgroundImage.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    if (isImmediate) {
        int w = drawable.getIntrinsicWidth();
        int viewWidth = mBackgroundImage.getWidth();
        float factor = viewWidth / (float) w;
        int h = (int) (drawable.getIntrinsicHeight() * factor);
        drawable.setBounds(0, 0, w, h);//w w  w  .j a v  a2s .  co m
        mBackgroundImage.setImageDrawable(drawable);
    } else {
        Drawable current = mBackgroundImage.getDrawable();
        if (current == null)
            current = new ColorDrawable(Color.TRANSPARENT);
        TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[] { current, drawable });
        transitionDrawable.setCrossFadeEnabled(true);
        mBackgroundImage.setImageDrawable(transitionDrawable);
        transitionDrawable.startTransition(500);
    }

}

From source file:arun.com.chromer.webheads.ui.views.BaseWebHead.java

/**
 * Applies a cross fade animation to transform the current favicon to an X icon. Ensures favicon
 * is visible by hiding indicators./*from  www .ja  v  a  2  s  .c  o  m*/
 */
void crossFadeFaviconToX() {
    favicon.setVisibility(VISIBLE);
    favicon.clearAnimation();
    favicon.setScaleType(CENTER);
    final TransitionDrawable icon = new TransitionDrawable(
            new Drawable[] { new ColorDrawable(TRANSPARENT), xDrawable });
    favicon.setImageDrawable(icon);
    icon.setCrossFadeEnabled(true);
    icon.startTransition(50);
    favicon.animate().withLayer().rotation(180).setDuration(250)
            .setInterpolator(new LinearOutSlowInInterpolator()).start();
}

From source file:arun.com.chromer.webheads.ui.views.BaseWebHead.java

public void setFaviconDrawable(@NonNull final Drawable faviconDrawable) {
    if (indicator != null && favicon != null) {
        indicator.animate().alpha(0).withLayer().start();
        TransitionDrawable transitionDrawable = new TransitionDrawable(
                new Drawable[] { new ColorDrawable(TRANSPARENT), faviconDrawable });
        favicon.setVisibility(VISIBLE);//from   w  ww.  j  a  va 2s. co m
        favicon.setImageDrawable(transitionDrawable);
        transitionDrawable.setCrossFadeEnabled(true);
        transitionDrawable.startTransition(500);
    }
}

From source file:com.android.systemui.statusbar.phone.NavigationBarView.java

private void updateColor(boolean defaults) {
    Bitmap bm = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    Canvas cnv = new Canvas(bm);

    if (defaults) {
        cnv.drawColor(0xFF000000);//from  w  w w. j av  a  2s .  co  m
        setBackground(new BitmapDrawable(bm));
        return;
    }

    String setting = Settings.System.getString(mContext.getContentResolver(), Settings.System.NAV_BAR_COLOR);
    String[] colors = (setting == null || setting.equals("")
            ? ExtendedPropertiesUtils.PARANOID_COLORS_DEFAULTS[ExtendedPropertiesUtils.PARANOID_COLORS_NAVBAR]
            : setting).split(ExtendedPropertiesUtils.PARANOID_STRING_DELIMITER);
    String currentColor = colors[Integer.parseInt(colors[2])];

    cnv.drawColor(new BigInteger(currentColor, 16).intValue());

    TransitionDrawable transition = new TransitionDrawable(
            new Drawable[] { getBackground(), new BitmapDrawable(bm) });
    transition.setCrossFadeEnabled(true);
    setBackground(transition);
    transition.startTransition(1000);
}

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

/*************************************
 * //from   w w w.ja  v a 2  s.  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();
    }
}

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

/*********************************************
 * //from  w  w w .  ja v  a2 s.  c o  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();
    }
}