SmartBackgroundManager

Additional

Language
Kotlin
Version
1.0 (Sep 15, 2018)
Created
Sep 15, 2018
Updated
Sep 15, 2018
Owner
Sanjar Khodjaev (pentoligy)
Contributor
Sanjar Khodjaev (pentoligy)
1
Activity
Badge
Generate
Download
Source code

Show card

SmartBackgroundManager

SmartBackgroundManager allows to easily change the background of your Leanback app and apply blur to it using RxJava2. Glide and Picasso are also supported.

Getting Started

Add the JCenter repository to your app-module or project:

repositories {
    jcenter()
}

And then add the dependencies:

implementation "uz.pentoligy.leanback:smartbackgroundmanager:1.0"

If you want to use Glide or Picasso, add one of the following dependencies:

// for Glide
implementation "uz.pentoligy.leanback:smartbackgroundmanager-glide:1.0"
implementation "com.github.bumptech.glide:glide:$latestVersion"
kapt "com.github.bumptech.glide:compiler:$latestVersion"

// for Picasso
implementation "uz.pentoligy.leanback:smartbackgroundmanager-picasso:1.0"
implementation "com.squareup.picasso:picasso:$latestVersion"

Library dependencies

implementation "androidx.leanback:leanback:$latestVersion"
implementation "io.reactivex.rxjava2:rxandroid:$latestVersion"
implementation "io.reactivex.rxjava2:rxjava:$latestVersion"

Note, that library requires minimum SDK version 17 or higher.

Usage

  • Creating an instance:

    • Simple without blurring:
    val backgroundManager: SmartBackgroundManager =
           SmartBackgroundManager.Builder(requireActivity()).build()
    • Simple with blurring:
    val backgroundManager: SmartBackgroundManager =
           SmartBackgroundManager.Builder(this)
                   .withRenderScriptOptions()
                   .build()
    • Advanced:
    val options: RenderScriptOptions =
           RenderScriptOptions.Builder(requireContext())
                   .withBlurScale(0.5f)
                   .withBlurRadius(10f)
                   .build()
    
    val backgroundManager: SmartBackgroundManager =
            SmartBackgroundManager.Builder(this)
                   .withRenderScriptOptions(options)
                   .withUpdateDelay(200)
                   .build()
                   
  • Applying background:

    • From ImageCardView:
    (itemViewHolder?.view as? ImageCardView)?.let {
         backgroundManager.fromImageCardView(it)
    }
    • From Bitmap:
    backgroundManager.fromBitmap(bitmap)
    • From Drawable:
    backgroundManager.fromDrawable(drawable)
    // or
    backgroundManager.fromDrawable(R.id.drawable)
    • From color resource:
    backgroundManager.fromColor(R.color.color)
  • If you want to use Glide or Picasso:

    // for Glide:
    private val glideTarget: SmartBackgroundManagerGlideTarget by lazy {
          SmartBackgroundManagerGlideTarget(backgroundManager)
    }
    
    ...
    
    Glide.with(requireActivity()).asBitmap().load(url).into(glideTarget)
    // for Picasso:
    private val picassoTarget: SmartBackgroundManagerPicassoTarget by lazy {
          SmartBackgroundManagerPicassoTarget(backgroundManager)
    }
    
    ...
    
    val width: Int = backgroundManager.displayWidth
    val height: Int = backgroundManager.displayHeight
    
    Picasso.get().load(url)
            .resize(width, height).centerCrop().into(picassoTarget)

To avoid memory leaks, you must set following in your Activity/Fragment:

override fun onDestroy() {
     backgroundManager.onDestroy()
     super.onDestroy()
}

Please, check the sample app for more library usages.

Screenshots

  • Without blurring:

  • With blurring:

Developed by

License

Copyright 2018 Sanjar Khodjaev

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.