Skip to content

Commit

Permalink
[#13] 뱃지가 달린 Round Image View 추가
Browse files Browse the repository at this point in the history
- 리뷰를 반영한 전체적인 코드 수정
  • Loading branch information
DongJun-H committed Mar 31, 2022
1 parent 04f846c commit ea46f89
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 47 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ android {
}

dependencies {
def glide_version = '4.13.1'

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
Expand All @@ -77,4 +78,8 @@ dependencies {
implementation platform('com.google.firebase:firebase-bom:29.2.0')
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.google.firebase:firebase-crashlytics-ktx'

// Glide
implementation "com.github.bumptech.glide:glide:$glide_version"
annotationProcessor "com.github.bumptech.glide:compiler:$glide_version"
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.moyerun.moyeorun_android">

<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".MoyeoRunApplication"
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
package com.moyerun.moyeorun_android.views
import android.Manifest
import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.util.Patterns
import android.view.LayoutInflater
import androidx.annotation.ColorRes
import androidx.annotation.Dimension
import androidx.annotation.DrawableRes
import androidx.annotation.RequiresPermission
import androidx.constraintlayout.widget.ConstraintLayout
import com.bumptech.glide.Glide
import com.moyerun.moyeorun_android.R
import com.moyerun.moyeorun_android.databinding.ViewBadgeRoundImageBinding
import java.io.IOException

class BadgeRoundImageView : ConstraintLayout {
constructor(context: Context): super(context) { initView() }
constructor(context: Context, attrs: AttributeSet): super(context, attrs) {
initView()
setAttrs(attrs)
}
constructor(context: Context, attrs: AttributeSet, defStyle: Int): super(context, attrs) {
initView()
setAttrs(attrs, defStyle)
}
class BadgeRoundImageView @JvmOverloads constructor(
context: Context, attrs: AttributeSet?=null, defStyle: Int = 0)
: ConstraintLayout(context, attrs, defStyle) {
private var binding: ViewBadgeRoundImageBinding = ViewBadgeRoundImageBinding.inflate(LayoutInflater.from(context), this, true)

private lateinit var binding: ViewBadgeRoundImageBinding
init {
if(attrs != null) { setAttrs(attrs, defStyle) }
}

private fun initView() { binding = ViewBadgeRoundImageBinding.inflate(LayoutInflater.from(context), this, true) }
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val verticalPadding = paddingTop + paddingBottom
val horizontalPadding = paddingStart + paddingEnd
if(widthMeasureSpec >= heightMeasureSpec) {
super.onMeasure(heightMeasureSpec + horizontalPadding, heightMeasureSpec + verticalPadding)
} else {
super.onMeasure(widthMeasureSpec + horizontalPadding, widthMeasureSpec + verticalPadding)
}
}

private fun getAttrs(attributeSet: AttributeSet, customViewAttrs: IntArray, defStyle: Int=0, defStyleRes: Int=0)
= context.obtainStyledAttributes(attributeSet, customViewAttrs, defStyle, defStyleRes)
private fun getAttrs(attributeSet: AttributeSet, customViewAttrs: IntArray, defStyle: Int=0, defStyleRes: Int=0)
= context.obtainStyledAttributes(attributeSet, customViewAttrs, defStyle, defStyleRes)

private fun setAttrs(attrs: AttributeSet) {
val attributesTypedArray = getAttrs(attrs, R.styleable.BadgeRoundImageView)
setTypedArray(attributesTypedArray)
}
private fun setAttrs(attrs: AttributeSet, defStyle: Int) {
val attributesTypedArray = getAttrs(attrs, R.styleable.BadgeRoundImageView, defStyle, 0)
setTypedArray(attributesTypedArray)
Expand All @@ -37,23 +45,18 @@ class BadgeRoundImageView : ConstraintLayout {
binding.imgBigCircle.apply {
val bigCircleImgResId = typedArray.getResourceId(R.styleable.BadgeRoundImageView_bigCircleImgSrc, R.drawable.ic_launcher_foreground)
val bigCircleImgBgResId = typedArray.getResourceId(R.styleable.BadgeRoundImageView_bigCircleBackgroundColor, R.drawable.ic_launcher_foreground)
val bigCircleSize = typedArray.getDimensionPixelSize(R.styleable.BadgeRoundImageView_bigCircleSize, 0)

this.setImageResource(bigCircleImgResId)
this.setBackgroundResource(bigCircleImgBgResId)
this.layoutParams.apply {
this.height = bigCircleSize
this.width = bigCircleSize
}
}

binding.imgBadgeSymbol.apply {
val badgeResId = typedArray.getResourceId(R.styleable.BadgeRoundImageView_badgeImgSrc, R.drawable.ic_launcher_foreground)
val badgeBgResId = typedArray.getResourceId(R.styleable.BadgeRoundImageView_badgeBackgroundColor, R.drawable.ic_launcher_foreground)
val badgeBgResId = typedArray.getResourceId(R.styleable.BadgeRoundImageView_badgeBackgroundColor, R.color.main_white)
val badgeSize = typedArray.getDimensionPixelSize(R.styleable.BadgeRoundImageView_badgeSize, 0)

this.setImageResource(badgeResId)
this.setBackgroundResource(badgeBgResId)
this.setBackgroundColor(resources.getColor(badgeBgResId, context?.theme))
this.layoutParams.apply {
this.height = badgeSize
this.width = badgeSize
Expand All @@ -62,25 +65,43 @@ class BadgeRoundImageView : ConstraintLayout {
typedArray.recycle()
}

fun setBigCircleImgSrc(imgResId: Int) {
fun setBigCircleImgSrc(@DrawableRes imgResId: Int) {
binding.imgBigCircle.setImageResource(imgResId)
}
fun setBigCircleImageBg(bgResId: Int) {
binding.imgBigCircle.setBackgroundResource(bgResId)
}
fun setBigCircleImageSize(imgSize: Int) {
binding.imgBigCircle.layoutParams = binding.imgBigCircle.layoutParams.apply {
this.height = imgSize
this.width = imgSize

@RequiresPermission(Manifest.permission.INTERNET)
@Throws(IOException::class)
fun setBigCircleImgSrc(imgUrl: String, isImageCropped:Boolean = false) {
if(Patterns.WEB_URL.matcher(imgUrl).matches()) {
if(isImageCropped) {
Glide.with(context).load(imgUrl).centerCrop().into(binding.imgBigCircle)
} else
Glide.with(context).load(imgUrl).into(binding.imgBigCircle)
}
}
fun setBadgeImgSrc(imgResId: Int) {

fun setBigCircleImageBg(@ColorRes bgResId: Int) {
binding.imgBigCircle.setBackgroundColor(resources.getColor(bgResId, context?.theme))
}
fun setBadgeImgSrc(@DrawableRes imgResId: Int) {
binding.imgBadgeSymbol.setImageResource(imgResId)
}
fun setBadgeImageBg(bgResId: Int) {
binding.imgBadgeSymbol.setBackgroundResource(bgResId)

@RequiresPermission(Manifest.permission.INTERNET)
@Throws(IOException::class)
fun setBadgeImgSrc(imgUrl: String, isImageCropped:Boolean = false) {
if(Patterns.WEB_URL.matcher(imgUrl).matches()) {
if(isImageCropped) {
Glide.with(context).load(imgUrl).centerCrop().into(binding.imgBadgeSymbol)
} else
Glide.with(context).load(imgUrl).into(binding.imgBadgeSymbol)
}
}

fun setBadgeImageBg(@ColorRes bgResId: Int) {
binding.imgBadgeSymbol.setBackgroundColor(resources.getColor(bgResId, context?.theme))
}
fun setBadgeImageSize(imgSize: Int) {
fun setBadgeImageSize(@Dimension imgSize: Int) {
binding.imgBadgeSymbol.layoutParams = binding.imgBadgeSymbol.layoutParams.apply {
this.height = imgSize
this.width = imgSize
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/res/layout/view_badge_round_image.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?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"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/img_big_circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:shapeAppearanceOverlay="@style/RoundImageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
Expand All @@ -17,6 +17,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:shapeAppearanceOverlay="@style/RoundImageView"
app:layout_constraintTop_toTopOf="@id/img_big_circle"
app:layout_constraintBottom_toBottomOf="@+id/img_big_circle"
app:layout_constraintEnd_toEndOf="@+id/img_big_circle"/>
app:layout_constraintStart_toStartOf="@id/img_big_circle"
app:layout_constraintEnd_toEndOf="@+id/img_big_circle"
app:layout_constraintVertical_bias="1.0"
app:layout_constraintHorizontal_bias="1.0"/>
</androidx.constraintlayout.widget.ConstraintLayout>
9 changes: 4 additions & 5 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="BadgeRoundImageView">
<attr name="bigCircleImgSrc" format="reference|integer"/>
<attr name="bigCircleBackgroundColor" format="reference|integer|color"/>
<attr name="bigCircleSize" format="reference|integer|dimension"/>
<attr name="badgeImgSrc" format="reference|integer"/>
<attr name="badgeBackgroundColor" format="reference|integer|color"/>
<attr name="bigCircleImgSrc" format="reference"/>
<attr name="bigCircleBackgroundColor" format="color"/>
<attr name="badgeImgSrc" format="reference"/>
<attr name="badgeBackgroundColor" format="color"/>
<attr name="badgeSize" format="reference|integer|dimension"/>
</declare-styleable>
</resources>

0 comments on commit ea46f89

Please sign in to comment.