Fix: Some Invisible Icons

Improve: Use ResourceCompat to get Drawable
This commit is contained in:
LooKeR 2021-12-01 00:14:01 +05:30
parent a52c72e2c9
commit 7165757745
3 changed files with 19 additions and 38 deletions

View File

@ -7,22 +7,20 @@ import android.content.res.ColorStateList
import android.content.res.Resources
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.graphics.drawable.VectorDrawable
import android.util.TypedValue
import android.util.Xml
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.AttrRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import coil.util.CoilUtils
import com.google.android.material.imageview.ShapeableImageView
import com.google.android.material.textview.MaterialTextView
import com.looker.droidify.utility.extension.android.Android
import org.xmlpull.v1.XmlPullParser
import kotlin.math.roundToInt
object TypefaceExtra {
val bold = Typeface.create("sans-serif-medium", Typeface.BOLD)!!
val medium = Typeface.create("sans-serif-medium", Typeface.NORMAL)!!
val light = Typeface.create("sans-serif-light", Typeface.NORMAL)!!
}
@ -34,37 +32,7 @@ val Number.toPx
Resources.getSystem().displayMetrics
)
fun Context.getDrawableCompat(resId: Int): Drawable {
val drawable = if (!Android.sdk(24)) {
val fileName = TypedValue().apply { resources.getValue(resId, this, true) }.string
if (fileName.endsWith(".xml")) {
resources.getXml(resId).use { it ->
val eventType = generateSequence { it.next() }
.find { it == XmlPullParser.START_TAG || it == XmlPullParser.END_DOCUMENT }
if (eventType == XmlPullParser.START_TAG) {
when (it.name) {
"vector" -> VectorDrawable.createFromXmlInner(
resources,
it,
Xml.asAttributeSet(it),
theme
)
else -> null
}
} else {
null
}
}
} else {
null
}
} else {
null
}
return drawable ?: ContextCompat.getDrawable(this, resId)!!
}
fun Context.getColorFromAttr(attrResId: Int): ColorStateList {
fun Context.getColorFromAttr(@AttrRes attrResId: Int): ColorStateList {
val typedArray = obtainStyledAttributes(intArrayOf(attrResId))
val (colorStateList, resId) = try {
Pair(typedArray.getColorStateList(0), typedArray.getResourceId(0, 0))
@ -84,6 +52,10 @@ fun Context.getDrawableFromAttr(attrResId: Int): Drawable {
return getDrawableCompat(resId)
}
fun Context.getDrawableCompat(@DrawableRes resId: Int): Drawable =
ResourcesCompat.getDrawable(resources, resId, theme) ?: ContextCompat.getDrawable(this, resId)!!
fun Resources.sizeScaled(size: Int): Int {
return (size * displayMetrics.density).roundToInt()
}

View File

@ -4,8 +4,7 @@
android:tint="?attr/colorOnSurface"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?android:attr/textColor"
android:fillColor="@android:color/white"
android:pathData="M18,13h-5v5c0,0.55 -0.45,1 -1,1s-1,-0.45 -1,-1v-5H6c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1h5V6c0,-0.55 0.45,-1 1,-1s1,0.45 1,1v5h5c0.55,0 1,0.45 1,1s-0.45,1 -1,1z" />
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorOnPrimaryContainer">
<path
android:fillColor="@android:color/white"
android:pathData="M9,16.2l-3.5,-3.5c-0.39,-0.39 -1.01,-0.39 -1.4,0 -0.39,0.39 -0.39,1.01 0,1.4l4.19,4.19c0.39,0.39 1.02,0.39 1.41,0L20.3,7.7c0.39,-0.39 0.39,-1.01 0,-1.4 -0.39,-0.39 -1.01,-0.39 -1.4,0L9,16.2z"/>
</vector>