Skip to content

Instantly share code, notes, and snippets.

@jmatsu
Last active September 25, 2016 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmatsu/cb80db56f2d2a992cc79fab22b24f869 to your computer and use it in GitHub Desktop.
Save jmatsu/cb80db56f2d2a992cc79fab22b24f869 to your computer and use it in GitHub Desktop.
import android.support.v7.preference.Preference
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import com.squareup.picasso.Target
import java.lang.ref.WeakReference
private class PreferenceTarget(pref: Preference) : Target {
private val ref: WeakReference<Preference?> = WeakReference(pref)
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
ref.get()?.icon = placeHolderDrawable
}
override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom?) {
ref.get()?.let { pref ->
pref.icon = BitmapDrawable(pref.context.resources, bitmap)
}
}
override fun onBitmapFailed(errorDrawable: Drawable?) {
ref.get()?.icon = errorDrawable
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment