Last active
February 7, 2022 07:16
-
-
Save zihadrizkyef/ac2bc209bdfab5a1388a3e431386af76 to your computer and use it in GitHub Desktop.
Load image with placeholder. If fails, show text describing the image
This file contains 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 ImageView.loadImageOrInitial( | |
url: String?, | |
text: String?, | |
textSize: Float = 16F, | |
textColor: Int = Color.BLACK, | |
backgroundColor: Int = Color.WHITE, | |
maxChar: Int = 2, | |
allCaps: Boolean = true, | |
progressStroke: Float = 5F, | |
progressRadius: Float = 30F, | |
) { | |
//CircularProgressDrawable is drawable from androidx.swiperefreshlayout.widget | |
val progressDrawable = CircularProgressDrawable(context) | |
progressDrawable.strokeWidth = progressStroke.dp | |
progressDrawable.centerRadius = progressRadius.dp | |
progressDrawable.start() | |
Glide.with(this) | |
.load(url) | |
.placeholder(progressDrawable) | |
.addListener(object : RequestListener<Drawable> { | |
override fun onLoadFailed( | |
e: GlideException?, | |
model: Any?, | |
target: Target<Drawable>?, | |
isFirstResource: Boolean | |
): Boolean { | |
val context = this@loadImageOrInitial.context | |
val textDrawable = InitialsDrawable( | |
context, | |
text ?: "", | |
textSize, | |
textColor, | |
backgroundColor, | |
maxChar, | |
allCaps, | |
) | |
textDrawable.setBounds(0, 0, 40, 40) | |
this@loadImageOrInitial.setImageDrawable(textDrawable) | |
return true | |
} | |
override fun onResourceReady( | |
resource: Drawable?, | |
model: Any?, | |
target: Target<Drawable>?, | |
dataSource: DataSource?, | |
isFirstResource: Boolean | |
): Boolean { | |
return false | |
} | |
}) | |
.into(this) | |
} |
This file contains 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
package com.kikota.pos.views | |
import android.content.Context | |
import android.graphics.* | |
import android.graphics.drawable.Drawable | |
import com.kikota.pos.utils.getInitials | |
import java.util.* | |
/** | |
* Create initials drawable | |
* [text]: Text to get the initials from it. eg: "Pizza Mozzarella" will show "PM" | |
*/ | |
class InitialsDrawable( | |
context: Context, | |
text: String, | |
textSize: Float = 16F, | |
textColor: Int = Color.BLACK, | |
private var backgroundColor: Int = Color.TRANSPARENT, | |
maxChar: Int = 2, | |
allCaps: Boolean = true, | |
) : Drawable() { | |
private val paint: Paint = Paint() | |
private var initials = if (allCaps) { | |
text.getInitials(maxChar).toUpperCase(Locale.ROOT) | |
} else { | |
text.getInitials(maxChar) | |
} | |
init { | |
val font = Typeface.createFromAsset(context.assets, "fonts/Nunito-Bold.ttf") | |
paint.typeface = font | |
val spSize = textSize * context.resources.displayMetrics.scaledDensity | |
paint.textSize = spSize | |
paint.color = textColor | |
paint.isAntiAlias = true | |
paint.textAlign = Paint.Align.CENTER | |
} | |
override fun draw(canvas: Canvas) { | |
canvas.drawColor(backgroundColor) | |
val yCenter = (bounds.exactCenterY() - (paint.descent() + paint.ascent()) / 2) | |
canvas.drawText(initials, bounds.exactCenterX(), yCenter, paint) | |
} | |
override fun setAlpha(alpha: Int) { | |
paint.alpha = alpha | |
} | |
override fun setColorFilter(cf: ColorFilter?) { | |
paint.colorFilter = cf | |
} | |
override fun getOpacity(): Int { | |
return PixelFormat.TRANSLUCENT | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment