-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
plugins { | ||
id 'com.android.library' | ||
id 'org.jetbrains.kotlin.android' | ||
} | ||
|
||
android { | ||
namespace 'com.appcent.animatedbutton' | ||
compileSdk 33 | ||
|
||
defaultConfig { | ||
minSdk 24 | ||
targetSdk 33 | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
consumerProguardFiles "consumer-rules.pro" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation 'androidx.core:core-ktx:1.7.0' | ||
implementation 'androidx.appcompat:appcompat:1.6.1' | ||
implementation 'com.google.android.material:material:1.10.0' | ||
testImplementation 'junit:junit:4.13.2' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.5' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.appcent.animatedbutton | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.appcent.animatedbutton.test", appContext.packageName) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
</manifest> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.appcent.animatedbutton | ||
|
||
import android.graphics.Bitmap | ||
import android.view.View | ||
import androidx.core.content.ContextCompat | ||
import androidx.core.graphics.drawable.toBitmap | ||
|
||
fun View.getBitmap(drawableId: Int): Bitmap { | ||
return ContextCompat.getDrawable(context, drawableId)!!.toBitmap() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package com.appcent.animatedbutton | ||
|
||
|
||
import android.animation.ArgbEvaluator | ||
import android.animation.ValueAnimator | ||
import android.content.Context | ||
import android.graphics.* | ||
import android.text.TextPaint | ||
import android.util.AttributeSet | ||
import android.view.View | ||
import androidx.core.animation.doOnEnd | ||
import androidx.core.animation.doOnStart | ||
import androidx.core.content.ContextCompat | ||
import androidx.core.graphics.withScale | ||
import androidx.interpolator.view.animation.FastOutSlowInInterpolator | ||
|
||
import com.google.android.material.math.MathUtils.lerp | ||
class LikeButton @JvmOverloads constructor( | ||
context: Context, | ||
attrs: AttributeSet? = null, | ||
defStyle: Int = 0 | ||
) : View(context, attrs, defStyle) { | ||
|
||
|
||
private var fraction: Float = 0f | ||
|
||
private val iconBitmap: Bitmap | ||
|
||
private var iconWidth: Int | ||
private var iconHeight: Int | ||
|
||
private val radius = 32 * resources.displayMetrics.density | ||
|
||
private val paint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply { | ||
textAlign = Paint.Align.LEFT | ||
} | ||
// Initialize color variables with default values or values from attributes | ||
private var unlikeBgColor = ContextCompat.getColor(context, R.color.unlike_bg_color) | ||
private var BgColor = ContextCompat.getColor(context, R.color.like_bg_color) | ||
private var likeBgColor = ContextCompat.getColor(context, R.color.like_bg_color) | ||
private var unlikeImgColor = ContextCompat.getColor(context, R.color.unlike_img_color) | ||
private var ImgColor = ContextCompat.getColor(context, R.color.like_img_color) | ||
private var likeImgColor = ContextCompat.getColor(context, R.color.like_img_color) | ||
|
||
private var loopLike: Boolean = false | ||
private var uiState: UIState = UIState.UnLike | ||
|
||
|
||
init { | ||
// Obtain attribute values if provided in XML layout | ||
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.LikeButton) | ||
BgColor = typedArray.getColor(R.styleable.LikeButton_likeBgColor, BgColor) | ||
ImgColor = typedArray.getColor(R.styleable.LikeButton_likeImgColor, ImgColor) | ||
likeBgColor = typedArray.getColor(R.styleable.LikeButton_likeBgColor, BgColor) | ||
likeImgColor = typedArray.getColor(R.styleable.LikeButton_likeImgColor, ImgColor) | ||
unlikeBgColor = typedArray.getColor(R.styleable.LikeButton_unlikeBgColor, unlikeBgColor) | ||
unlikeImgColor = typedArray.getColor(R.styleable.LikeButton_unlikeImgColor, unlikeImgColor) | ||
val iconDrawableResId = typedArray.getResourceId(R.styleable.LikeButton_iconDrawable, R.drawable.ic_baseline_add_smile) | ||
iconWidth = typedArray.getDimensionPixelSize(R.styleable.LikeButton_iconWidth, 64.dpToPx()) | ||
iconHeight = typedArray.getDimensionPixelSize(R.styleable.LikeButton_iconHeight, 64.dpToPx()) | ||
iconBitmap = scaleBitmap(getBitmap(iconDrawableResId), iconWidth, iconHeight) | ||
loopLike = typedArray.getBoolean(R.styleable.LikeButton_loopLike, false) | ||
typedArray.recycle() | ||
} | ||
|
||
private val srcInMode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN) | ||
private val drawableCanvas = Canvas(this.iconBitmap) | ||
private val argbEvaluator = ArgbEvaluator() | ||
private fun getRoundRectPaint() = paint.apply { | ||
val bgFraction = (fraction * 2).coerceAtMost(1f) | ||
color = argbEvaluator.evaluate(bgFraction, unlikeBgColor, BgColor) as Int | ||
} | ||
|
||
private fun Int.dpToPx(): Int { | ||
return (this * resources.displayMetrics.density).toInt() | ||
} | ||
|
||
private fun scaleBitmap(bitmap: Bitmap, width: Int, height: Int): Bitmap { | ||
val scaledBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true) | ||
if (scaledBitmap != bitmap) { | ||
bitmap.recycle() | ||
} | ||
return scaledBitmap | ||
} | ||
|
||
private fun getBitmapPaint() = paint.apply { | ||
val bgFraction = (fraction * 2).coerceAtMost(1f) | ||
color = argbEvaluator.evaluate(bgFraction, unlikeImgColor, ImgColor) as Int | ||
} | ||
|
||
private fun tintBitmap() { | ||
paint.xfermode = srcInMode | ||
drawableCanvas.drawRect(0f, 0f, this.iconBitmap.width.toFloat(), this.iconBitmap.height.toFloat(), getBitmapPaint()) | ||
paint.xfermode = null | ||
} | ||
|
||
private fun updateBgColor(uiState: UIState) { | ||
if (uiState == UIState.UnLike) { | ||
BgColor = unlikeBgColor | ||
ImgColor = unlikeImgColor | ||
} else { | ||
BgColor = likeBgColor | ||
ImgColor = likeImgColor | ||
} | ||
} | ||
|
||
|
||
|
||
override fun onDraw(canvas: Canvas) { | ||
// round rect | ||
getRoundRectPaint().also { | ||
// bound | ||
val left = 0f | ||
val top = 0f | ||
val right = width.toFloat() | ||
val bottom = height.toFloat() | ||
// scale | ||
val scaleX = lerp(1f, 0.9f, 1f, fraction) | ||
val scaleY = scaleX | ||
val pivotX = width / 2f | ||
val pivotY = height / 2f | ||
canvas.withScale(scaleX, scaleY, pivotX, pivotY) { | ||
canvas.drawRoundRect(left, top, right, bottom, radius, radius, it) | ||
} | ||
} | ||
|
||
// heart icon | ||
getBitmapPaint().also { paint -> | ||
val scaleX = lerp(1f, 0.6f, 1f, fraction) | ||
val scaleY = scaleX | ||
val pivotX = width / 2f | ||
val pivotY = height / 2f | ||
canvas.withScale(scaleX, scaleY, pivotX, pivotY) { | ||
val left = width / 2f - iconBitmap.width / 2f | ||
val top = height / 2f - iconBitmap.height / 2f | ||
tintBitmap() | ||
canvas.drawBitmap(iconBitmap, left, top, paint) | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
private fun runAnimation(): ValueAnimator { | ||
return ValueAnimator.ofFloat(0f, 1f).apply { | ||
addUpdateListener { | ||
fraction = it.animatedValue as Float | ||
invalidate() | ||
} | ||
interpolator = FastOutSlowInInterpolator() | ||
doOnStart { | ||
this@LikeButton.uiState = UIState.Animating | ||
} | ||
duration = 500L | ||
start() | ||
} | ||
} | ||
|
||
private fun lerp(a: Float, b: Float, c: Float, fraction: Float): Float { | ||
return if (fraction <= 0.5f) { | ||
lerp(a, b, fraction * 2) | ||
} else { | ||
val tempFraction = fraction - 0.5f | ||
lerp(b, c, tempFraction * 2) | ||
} | ||
} | ||
|
||
|
||
fun toggleUIState() { | ||
if (uiState == UIState.Like) { | ||
setUIState(UIState.UnLike, true) | ||
} else { | ||
setUIState(UIState.Like, true) | ||
} | ||
} | ||
|
||
private fun setUIState(uiState: UIState, isAnim: Boolean) { | ||
updateBgColor(uiState) | ||
|
||
|
||
if (isAnim) { | ||
runAnimation().apply { | ||
doOnEnd { | ||
this@LikeButton.uiState = uiState | ||
} | ||
}.start() // Start the animation explicitly | ||
} else { | ||
this.uiState = uiState | ||
fraction = 0f | ||
invalidate() | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.appcent.animatedbutton | ||
|
||
sealed class UIState { | ||
object UnLike : UIState() | ||
object Like : UIState() | ||
object Animating : UIState() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="64dp" | ||
android:height="64dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path android:fillColor="@android:color/white" android:pathData="M18,9V7h-2V2.84C14.77,2.3 13.42,2 11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12c0,-1.05 -0.17,-2.05 -0.47,-3H18zM15.5,8C16.33,8 17,8.67 17,9.5S16.33,11 15.5,11S14,10.33 14,9.5S14.67,8 15.5,8zM8.5,8C9.33,8 10,8.67 10,9.5S9.33,11 8.5,11S7,10.33 7,9.5S7.67,8 8.5,8zM12,17.5c-2.33,0 -4.31,-1.46 -5.11,-3.5h10.22C16.31,16.04 14.33,17.5 12,17.5zM22,3h2v2h-2v2h-2V5h-2V3h2V1h2V3z"/> | ||
</vector> |