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
/** | |
* Create and return a new [Painter] that wraps [painter] with its [alpha], [colorFilter], or [onDraw] overwritten. | |
*/ | |
fun forwardingPainter( | |
painter: Painter, | |
alpha: Float = DefaultAlpha, | |
colorFilter: ColorFilter? = null, | |
onDraw: DrawScope.(ForwardingDrawInfo) -> Unit = DefaultOnDraw, | |
): Painter = ForwardingPainter(painter, alpha, colorFilter, onDraw) |
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
open class ForwardingPainter( | |
private val delegate: Painter, | |
private var alpha: Float = DefaultAlpha, | |
private var colorFilter: ColorFilter? = null, | |
) : Painter() { | |
override val intrinsicSize get() = delegate.intrinsicSize | |
override fun applyAlpha(alpha: Float): Boolean { | |
if (alpha == DefaultAlpha) { |
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
imageView.load("https://www.example.com/image.jpg") { | |
listener( | |
onSuccess = { /** Handle success. */ }, | |
onError = { /** Handle error. */ } | |
) | |
} | |
// Call this inside of your coroutine scope. | |
// This will throw an error inside of the scope if it fails. | |
val drawable = Coil.get("https://www.example.com/image.jpg") |
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
// To load an image into an ImageView, use the load extension function. | |
imageView.load("https://www.example.com/image.jpg") | |
// Coil supports urls, uris, resources, drawables, bitmaps, files, and more. | |
imageView.load(R.drawable.image) | |
imageView.load(File("/path/to/image.jpg")) | |
imageView.load("content://com.android.externalstorage/image.jpg") | |
// Requests can be configured with an optional trailing lambda. | |
imageView.load("https://www.example.com/image.jpg") { |
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
fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer) | |
@JvmOverloads @Dimension(unit = Dimension.PX) fun Number.dpToPx( | |
metrics: DisplayMetrics = Resources.getSystem().displayMetrics | |
): Float { | |
return toFloat() * metrics.density | |
} | |
@JvmOverloads @Dimension(unit = Dimension.DP) fun Number.pxToDp( | |
metrics: DisplayMetrics = Resources.getSystem().displayMetrics |
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
@Suppress("UNCHECKED_CAST") | |
@JvmOverloads fun <V : View> ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): V { | |
return LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot) as V | |
} |
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
fun Context.openWebPage(url: String): Boolean { | |
// Format the URI properly. | |
val uri = url.toWebUri() | |
// Try using Chrome Custom Tabs. | |
try { | |
val intent = CustomTabsIntent.Builder() | |
.setToolbarColor(getColorCompat(R.color.primary)) | |
.setShowTitle(true) | |
.build() |
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
@CheckResult fun Drawable.tint(@ColorInt color: Int): Drawable { | |
val tintedDrawable = DrawableCompat.wrap(this).mutate() | |
DrawableCompat.setTint(tintedDrawable, color) | |
return tintedDrawable | |
} | |
@CheckResult fun Drawable.tint(context: Context, @ColorRes color: Int): Drawable { | |
return tint(context.getColorCompat(color)) | |
} |
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
fun Context.toActivity(): Activity? { | |
var context = this | |
while (context is ContextWrapper) { | |
if (context is Activity) { | |
return context | |
} | |
context = context.baseContext | |
} | |
return null | |
} |
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
@ColorInt fun Context.getColorCompat(@ColorRes colorRes: Int): Int { | |
return ContextCompat.getColor(this, colorRes) | |
} | |
fun Context.getDrawableCompat(@DrawableRes drawableRes: Int): Drawable { | |
return AppCompatResources.getDrawable(this, drawableRes)!! | |
} |
NewerOlder