Skip to content

Commit 531d005

Browse files
committed
Added center crop to animatable drawables
1 parent 2e86ba1 commit 531d005

File tree

4 files changed

+72
-22
lines changed

4 files changed

+72
-22
lines changed

app/src/main/java/com/goodayapps/avatarview/MainActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class MainActivity : AppCompatActivity() {
2626

2727
initViews()
2828

29-
avatar111.load("https://media4.giphy.com/media/f8hd7QP9LT31Rk2NG1/giphy.gif")
29+
// avatar111.load("https://media4.giphy.com/media/f8hd7QP9LT31Rk2NG1/giphy.gif")
30+
avatar111.load("https://comunitee.b-cdn.net/staging/avatars/users/hlHkulpffffqQr3NWcVKxI6GbSB3/65E808E7-3098-45CE-B0D6-E50B12912751_1640027738.jpeg")
3031
}
3132

3233
private fun initViews() {

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
android:layout_width="@dimen/avatar_size"
4141
android:layout_height="@dimen/avatar_size"
4242
android:src="@drawable/ic_bell"
43+
android:scaleType="centerCrop"
4344
app:avBorderColor="#FF0000"
4445
app:avBorderColorSecondary="#FF9D00"
4546
app:avBorderGradientAngle="135"

avatar-view/src/main/java/com/goodayapps/widget/AvatarDrawable.kt

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
package com.goodayapps.widget
22

3-
import android.graphics.Bitmap
4-
import android.graphics.Canvas
5-
import android.graphics.Color
6-
import android.graphics.ColorFilter
7-
import android.graphics.LinearGradient
8-
import android.graphics.Matrix
9-
import android.graphics.Paint
10-
import android.graphics.Path
11-
import android.graphics.PixelFormat
12-
import android.graphics.PorterDuff
13-
import android.graphics.PorterDuffXfermode
14-
import android.graphics.RectF
15-
import android.graphics.Shader
16-
import android.graphics.Typeface
3+
import android.graphics.*
174
import android.graphics.drawable.Animatable
18-
import android.graphics.drawable.AnimatedImageDrawable
195
import android.graphics.drawable.BitmapDrawable
206
import android.graphics.drawable.Drawable
217
import android.media.ThumbnailUtils
@@ -200,7 +186,7 @@ class AvatarDrawable private constructor(
200186
is Animatable -> {
201187
isIconDrawable = false
202188

203-
createAvatarBitmap(avatarDrawable, 1f)
189+
createAnimatableAvatarBitmap(avatarDrawable)
204190
}
205191
is Drawable -> {
206192
isIconDrawable = true
@@ -263,6 +249,19 @@ class AvatarDrawable private constructor(
263249
}
264250
}
265251

252+
private fun createAnimatableAvatarBitmap(
253+
avatarDrawable: Drawable
254+
): Bitmap? {
255+
val cropDrawable = CenterCropDrawable(avatarDrawable)
256+
257+
return Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888).also {
258+
cropDrawable.setBounds(0, 0, size, size)
259+
260+
iconColorFilter?.let { filter -> cropDrawable.colorFilter = filter }
261+
262+
cropDrawable.draw(Canvas(it))
263+
}
264+
}
266265

267266
private fun calculateBounds(): RectF {
268267
val availableWidth = size
@@ -279,7 +278,7 @@ class AvatarDrawable private constructor(
279278
private fun drawBorder() {
280279
if (border.width > 0) {
281280
if (((border.archesType == Border.ARCH_TYPE_DEFAULT && border.archesCount > 1)
282-
|| border.archesType == Border.ARCH_TYPE_MIRROR && border.archesCount > 0)
281+
|| border.archesType == Border.ARCH_TYPE_MIRROR && border.archesCount > 0)
283282
&& totalArchesDegreeArea > 0f
284283
) {
285284
drawArcBorder()
@@ -358,7 +357,7 @@ class AvatarDrawable private constructor(
358357

359358
private fun calculateSpaceBetweenArches() =
360359
(totalArchesDegreeArea - (border.archesCount * individualArcDegreeLength)) /
361-
(border.archesCount + if (totalArchesDegreeArea == 360f) 0 else 1)
360+
(border.archesCount + if (totalArchesDegreeArea == 360f) 0 else 1)
362361

363362
private fun calculateArcDegreeLength() =
364363
totalArchesDegreeArea / (if (border.archesType == Border.ARCH_TYPE_DEFAULT) {
@@ -382,8 +381,8 @@ class AvatarDrawable private constructor(
382381
val intrinsicHeightF = intrinsicHeight.toFloat()
383382
val intrinsicWidthF = intrinsicWidth.toFloat()
384383

385-
val ratioOfWidth = intrinsicWidthF/maxF
386-
val ratioOfHeight = intrinsicHeightF/maxF
384+
val ratioOfWidth = intrinsicWidthF / maxF
385+
val ratioOfHeight = intrinsicHeightF / maxF
387386
val ratio = intrinsicWidth / intrinsicHeightF
388387

389388
when {
@@ -395,7 +394,7 @@ class AvatarDrawable private constructor(
395394
_width = intrinsicWidth / ratioOfWidth * ratio
396395
_height = maxF
397396
}
398-
ratio == 1f ->{
397+
ratio == 1f -> {
399398
_width = maxF
400399
_height = maxF
401400
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.goodayapps.widget
2+
3+
import android.graphics.*
4+
import android.graphics.drawable.Drawable
5+
import androidx.annotation.IntRange
6+
import kotlin.math.roundToInt
7+
8+
class CenterCropDrawable(private val target: Drawable) : Drawable() {
9+
override fun setBounds(bounds: Rect) {
10+
super.setBounds(bounds.left, bounds.top, bounds.right, bounds.bottom)
11+
}
12+
13+
override fun setBounds(left: Int, top: Int, right: Int, bottom: Int) {
14+
val sourceRect = RectF(
15+
0f, 0f, target.intrinsicWidth.toFloat(), target.intrinsicHeight
16+
.toFloat()
17+
)
18+
val screenRect = RectF(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat())
19+
val matrix = Matrix()
20+
matrix.setRectToRect(screenRect, sourceRect, Matrix.ScaleToFit.CENTER)
21+
val inverse = Matrix()
22+
matrix.invert(inverse)
23+
inverse.mapRect(sourceRect)
24+
target.setBounds(
25+
sourceRect.left.roundToInt(), sourceRect.top.roundToInt(),
26+
sourceRect.right.roundToInt(), sourceRect.bottom.roundToInt()
27+
)
28+
super.setBounds(left, top, right, bottom)
29+
}
30+
31+
override fun draw(canvas: Canvas) {
32+
canvas.save()
33+
canvas.clipRect(bounds)
34+
target.draw(canvas)
35+
canvas.restore()
36+
}
37+
38+
override fun setAlpha(@IntRange(from = 0, to = 255) alpha: Int) {
39+
target.alpha = alpha
40+
}
41+
42+
override fun setColorFilter(colorFilter: ColorFilter?) {
43+
target.colorFilter = colorFilter
44+
}
45+
46+
override fun getOpacity(): Int {
47+
return target.opacity
48+
}
49+
}

0 commit comments

Comments
 (0)