Skip to content

Instantly share code, notes, and snippets.

@AndSky90
Created November 4, 2019 14:35
Show Gist options
  • Save AndSky90/c7aa886953b1d8a6a23f3e12f21f97ab to your computer and use it in GitHub Desktop.
Save AndSky90/c7aa886953b1d8a6a23f3e12f21f97ab to your computer and use it in GitHub Desktop.
Avatar Icon
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AvatarIcon">
<attr name="isBigAvatarSize" format="boolean"/>
<attr name="isRoundMask" format="boolean"/>
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/avatarBackground"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="ContentDescription"/>
<ImageView
android:id="@+id/avatarImage"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="ContentDescription"/>
<ImageView
android:id="@+id/avatarDecorator"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:src="@drawable/avatar_decorator_104"
tools:ignore="ContentDescription"/>
</androidx.constraintlayout.widget.ConstraintLayout>
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import kotlinx.android.synthetic.main.avatar_icon.view.*
class AvatarIcon(context: Context, attrs: AttributeSet?) : ConstraintLayout(context, attrs) {
private var mask: Int? = null
private var decorator: Int? = null
init {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.AvatarIcon, 0, 0)
val isBig = typedArray.getBoolean(R.styleable.AvatarIcon_isBigAvatarSize, false)
val isRound = typedArray.getBoolean(R.styleable.AvatarIcon_isRoundMask, false)
typedArray?.recycle()
LayoutInflater.from(context).inflate(R.layout.avatar_icon, this, true)
if (isRound) {
mask = R.drawable.icon_decorator_40
} else {
if (isBig) {
mask = R.drawable.avatar_mask_104
decorator = R.drawable.avatar_decorator_104
} else {
mask = R.drawable.avatar_mask_40
decorator = R.drawable.avatar_decorator_40
}
}
setImageWithGlide(
imageLink = getRandomAvatarBackground(resources),
imageView = avatarBackground,
maskImage = mask
)
setAvatarFromLink(null)
if (decorator != null) avatarDecorator.setImageResource(decorator!!)
}
fun setAvatarFromLink(imageLink: String?, cachingSignature: Any? = null) {
setImageWithGlide(
imageLink = imageLink,
imageView = avatarImage,
maskImage = mask,
errorImage = R.drawable.avatar_placeholder,
cachingSignature = cachingSignature
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment