DetailsSupportFragmentBackgroundController

public class DetailsSupportFragmentBackgroundController


Controller for DetailsSupportFragment parallax background and embedded video play.

The parallax background drawable is made of two parts: cover drawable (by default FitWidthBitmapDrawable) above the details overview row and bottom drawable (by default ColorDrawable) below the details overview row. While vertically scrolling rows, the size of cover drawable and bottom drawable will be updated and the cover drawable will by default perform a parallax shift using PROPERTY_VERTICAL_OFFSET.

       ***************************
       *      Cover Drawable     *
       * (FitWidthBitmapDrawable)*
       *                         *
       ***************************
       *    DetailsOverviewRow   *
       *                         *
       ***************************
       *     Bottom Drawable     *
       *      (ColorDrawable)    *
       *         Related         *
       *         Content         *
       ***************************
Both parallax background drawable and embedded video play are optional. App must call enableParallax and/or setupVideoPlayback explicitly. The PlaybackGlue is automatically play when fragment starts and pause when fragment stops. When video is ready to play, cover drawable will be faded out. Example:
DetailsSupportFragmentBackgroundController mController = new DetailsSupportFragmentBackgroundController(this);

public void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    MediaPlayerGlue player = new MediaPlayerGlue(..);
    player.setUrl(...);
    mController.enableParallax();
    mController.setupVideoPlayback(player);
}

static class MyLoadBitmapTask extends ... {
    WeakReferencemFragmentRef;
    MyLoadBitmapTask(MyFragment fragment) {
        mFragmentRef = new WeakReference(fragment);
    }
    protected void onPostExecute(Bitmap bitmap) {
        MyFragment fragment = mFragmentRef.get();
        if (fragment != null) {
            fragment.mController.setCoverBitmap(bitmap);
        }
    }
}

public void onStart() {
    new MyLoadBitmapTask(this).execute(url);
}

public void onStop() {
    mController.setCoverBitmap(null);
}

To customize cover drawable and/or bottom drawable, app should call enableParallax. If app supplies a custom cover Drawable, it should not call setCoverBitmap. If app supplies a custom bottom Drawable, it should not call setSolidColor.

To customize playback fragment, app should override onCreateVideoSupportFragment and onCreateGlueHost.

Summary

Public constructors

Creates a DetailsSupportFragmentBackgroundController for a DetailsSupportFragment.

Public methods

boolean

Precondition allows user navigate to video fragment using DPAD.

void

Enables default parallax background using a FitWidthBitmapDrawable as cover drawable and ColorDrawable as bottom drawable.

void
enableParallax(
    @NonNull Drawable coverDrawable,
    @NonNull Drawable bottomDrawable,
    @Nullable ParallaxTarget.PropertyValuesHolderTarget coverDrawableParallaxTarget
)

Enables parallax background using a custom cover drawable at top and a custom bottom drawable.

final Fragment

Adds or gets fragment for rendering video in DetailsSupportFragment.

final Drawable

Returns the drawable at bottom.

final Bitmap

Returns Bitmap set by setCoverBitmap.

final Drawable

Returns the cover drawable at top.

final int

Returns Default parallax offset in pixels for bitmap moving vertically.

final PlaybackGlue

Returns current PlaybackGlue or null if not set or cleared.

final @ColorInt int

Returns color set by setSolidColor.

PlaybackGlueHost

Creates a PlaybackGlueHost to host PlaybackGlue.

Fragment

Creates a Fragment to host PlaybackGlue.

final void

Convenient method to set Bitmap in cover drawable.

final void

Sets default parallax offset in pixels for bitmap moving vertically.

final void
setSolidColor(@ColorInt int color)

Convenient method to set color in bottom drawable.

void

Enable video playback and set proper PlaybackGlueHost.

final void

Switch to rows fragment.

final void

Switch to video fragment, note that this method is not affected by result of canNavigateToVideoSupportFragment.

Public constructors

DetailsSupportFragmentBackgroundController

Added in 1.1.0
public DetailsSupportFragmentBackgroundController(
    DetailsSupportFragment fragment
)

Creates a DetailsSupportFragmentBackgroundController for a DetailsSupportFragment. Note that each DetailsSupportFragment can only associate with one DetailsSupportFragmentBackgroundController.

Parameters
DetailsSupportFragment fragment

The DetailsSupportFragment to control background and embedded video playing.

Throws
java.lang.IllegalStateException

If fragment was already associated with another controller.

Public methods

canNavigateToVideoSupportFragment

Added in 1.1.0
public boolean canNavigateToVideoSupportFragment()

Precondition allows user navigate to video fragment using DPAD. Default implementation returns true if PlaybackGlue is not null. Subclass may override, e.g. only allow navigation when isPrepared is true. Note this method does not block app calls switchToVideo.

Returns
boolean

True allow to navigate to video fragment.

enableParallax

Added in 1.1.0
public void enableParallax()

Enables default parallax background using a FitWidthBitmapDrawable as cover drawable and ColorDrawable as bottom drawable. A vertical parallax movement will be applied to the FitWidthBitmapDrawable. App may use setSolidColor and setCoverBitmap to change the content of bottom drawable and cover drawable. This method must be called before setupVideoPlayback.

enableParallax

Added in 1.1.0
public void enableParallax(
    @NonNull Drawable coverDrawable,
    @NonNull Drawable bottomDrawable,
    @Nullable ParallaxTarget.PropertyValuesHolderTarget coverDrawableParallaxTarget
)

Enables parallax background using a custom cover drawable at top and a custom bottom drawable. This method must be called before setupVideoPlayback.

Parameters
@NonNull Drawable coverDrawable

Custom cover drawable shown at top. setCoverBitmap will not work if coverDrawable is not FitWidthBitmapDrawable; in that case it's app's responsibility to set content into coverDrawable.

@NonNull Drawable bottomDrawable

Drawable shown at bottom. setSolidColor will not work if bottomDrawable is not ColorDrawable; in that case it's app's responsibility to set content of bottomDrawable.

@Nullable ParallaxTarget.PropertyValuesHolderTarget coverDrawableParallaxTarget

Target to perform parallax effect within coverDrawable. Use null for no parallax movement effect. Example to move bitmap within FitWidthBitmapDrawable: new ParallaxTarget.PropertyValuesHolderTarget( coverDrawable, PropertyValuesHolder.ofInt( FitWidthBitmapDrawable.PROPERTY_VERTICAL_OFFSET, 0, -120))

findOrCreateVideoSupportFragment

Added in 1.1.0
public final Fragment findOrCreateVideoSupportFragment()

Adds or gets fragment for rendering video in DetailsSupportFragment. A subclass that overrides onCreateGlueHost should call this method to get a fragment for creating a PlaybackGlueHost.

Returns
Fragment

Fragment the added or restored fragment responsible for rendering video.

See also
onCreateGlueHost

getBottomDrawable

Added in 1.1.0
public final Drawable getBottomDrawable()

Returns the drawable at bottom. Returns null if enableParallax is not called. By default it's a ColorDrawable.

Returns
Drawable

The bottom drawable.

getCoverBitmap

Added in 1.1.0
public final Bitmap getCoverBitmap()

Returns Bitmap set by setCoverBitmap.

Returns
Bitmap

Bitmap for cover drawable.

getCoverDrawable

Added in 1.1.0
public final Drawable getCoverDrawable()

Returns the cover drawable at top. Returns null if enableParallax is not called. By default it's a FitWidthBitmapDrawable.

Returns
Drawable

The cover drawable at top.

getParallaxDrawableMaxOffset

Added in 1.1.0
public final int getParallaxDrawableMaxOffset()

Returns Default parallax offset in pixels for bitmap moving vertically. When 0, a default value would be used.

Returns
int

Default parallax offset in pixels for bitmap moving vertically.

See also
enableParallax

getPlaybackGlue

Added in 1.1.0
public final PlaybackGlue getPlaybackGlue()

Returns current PlaybackGlue or null if not set or cleared.

Returns
PlaybackGlue

Current PlaybackGlue or null

getSolidColor

Added in 1.1.0
public final @ColorInt int getSolidColor()

Returns color set by setSolidColor.

Returns
@ColorInt int

Solid color used for bottom drawable.

onCreateGlueHost

Added in 1.1.0
public PlaybackGlueHost onCreateGlueHost()

Creates a PlaybackGlueHost to host PlaybackGlue. App may override this if it overrides onCreateVideoSupportFragment. This method must be called after calling Fragment super.onCreate(). When override this method, app may call findOrCreateVideoSupportFragment to get or create a fragment.

Returns
PlaybackGlueHost

A new PlaybackGlueHost to host PlaybackGlue.

onCreateVideoSupportFragment

Added in 1.1.0
public Fragment onCreateVideoSupportFragment()

Creates a Fragment to host PlaybackGlue. Returns a new VideoSupportFragment by default. App may override and return a different fragment and it also must override onCreateGlueHost.

Returns
Fragment

A new fragment used in onCreateGlueHost.

setCoverBitmap

Added in 1.1.0
public final void setCoverBitmap(Bitmap bitmap)

Convenient method to set Bitmap in cover drawable. If app is not using default FitWidthBitmapDrawable, app should not use this method It's safe to call setCoverBitmap() before calling enableParallax.

Parameters
Bitmap bitmap

bitmap to set as cover.

setParallaxDrawableMaxOffset

Added in 1.1.0
public final void setParallaxDrawableMaxOffset(int offset)

Sets default parallax offset in pixels for bitmap moving vertically. This method must be called before enableParallax.

Parameters
int offset

Offset in pixels (e.g. 120).

See also
enableParallax

setSolidColor

Added in 1.1.0
public final void setSolidColor(@ColorInt int color)

Convenient method to set color in bottom drawable. If app is not using default ColorDrawable, app should not use this method. It's safe to call setSolidColor() before calling enableParallax.

Parameters
@ColorInt int color

color for bottom drawable.

setupVideoPlayback

Added in 1.1.0
public void setupVideoPlayback(@NonNull PlaybackGlue playbackGlue)

Enable video playback and set proper PlaybackGlueHost. This method by default creates a VideoSupportFragment and VideoSupportFragmentGlueHost to host the PlaybackGlue. This method must be called after calling details Fragment super.onCreate(). This method can be called multiple times to replace existing PlaybackGlue or calling setupVideoPlayback(null) to clear. Note a typical PlaybackGlue subclass releases resources in onDetachedFromHost, when the PlaybackGlue subclass is not doing that, it's app's responsibility to release the resources.

Parameters
@NonNull PlaybackGlue playbackGlue

The new PlaybackGlue to set as background or null to clear existing one.

switchToRows

Added in 1.1.0
public final void switchToRows()

Switch to rows fragment.

switchToVideo

Added in 1.1.0
public final void switchToVideo()

Switch to video fragment, note that this method is not affected by result of canNavigateToVideoSupportFragment. If the method is called in DetailsSupportFragment.onCreate() it will make video fragment to be initially focused once it is created.

Calling switchToVideo() in DetailsSupportFragment.onCreate() will clear the activity enter transition and shared element transition.

If switchToVideo() is called after prepareEntranceTransition and before onEntranceTransitionEnd, it will be ignored.

If prepareEntranceTransition is called after switchToVideo(), an IllegalStateException will be thrown.