Last active
September 25, 2016 16:19
-
-
Save jmatsu/cb80db56f2d2a992cc79fab22b24f869 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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