Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: Added support for background drawables #42

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
a5719b0
Library: Added background drawable support
Aug 27, 2020
8257d29
Sample: Added background drawable examples
Aug 27, 2020
5df7ef4
Merge pull request #1 from tamimattafi/dev
tamimattafi Aug 28, 2020
17bc908
Library: Fix #41 - setBackgroundDrawable called before shapeDrawable …
tamimattafi Aug 28, 2020
6be1002
Merge branch 'master' of https://github.com/fornewid/neumorphism
tamimattafi Aug 28, 2020
ca9c881
Library: Fixed app crashing when initializing a component that is bas…
tamimattafi Aug 28, 2020
a066254
Library: Code refactoring
tamimattafi Aug 28, 2020
e1d287d
Library: added reusability for shadow and background drawables
tamimattafi Jan 22, 2021
106a526
Optimizations: Made reusable bitmaps and shapes references softly rea…
Feb 20, 2021
cfcdbc2
Project: Updated target api version, updated kotlin and gradle wrappe…
Feb 20, 2021
0954522
Optimization: Changed rect hash code calculation to depend on width a…
Feb 20, 2021
8d045ae
fix: Added max cache size, removed bitmap cache
Nov 12, 2021
d866bac
feat: Add button click animation (partially)
Nov 12, 2021
cc2c2dd
update: Upgrade dependecies versions
Nov 24, 2021
42b18ad
config: Downgrade kotlin and gradle versions
Nov 24, 2021
a923580
feat: Create shadow drawable
Feb 4, 2022
c2c1d82
fix: Correct press animation
Feb 4, 2022
db5a05f
Merge pull request #2 from tamimattafi/feat/press_animation
tamimattafi Feb 4, 2022
d834ffd
fix: Remove inset, adjust pressed shape shadows
Feb 5, 2022
61434c8
fix: Correct flat shape shadow
Feb 5, 2022
2b43c88
fix: Add oval shadow coverage
Feb 6, 2022
a392fca
fix: Merge light and dark shadow in one drawable
Feb 7, 2022
9c9775c
fix: Combine light and dark shadows in one bitmap
Feb 8, 2022
ee4b3ff
fix: Correct radius and elevation
Feb 8, 2022
4cbddce
fix: Use arcs to draw shadow
Feb 8, 2022
b21aad3
fix: Correct outer shadow appearance
Feb 8, 2022
67180ad
fix: Add new shadow drawable
Feb 9, 2022
090659f
fix: Inner and outer shadow elevation
Feb 9, 2022
7236b99
fix: Use blur paint instead of rs and stack
Feb 9, 2022
b918a23
fix: Correct inner shadow
Feb 9, 2022
07c3106
fix: Fixed bitmap caching
Feb 10, 2022
1a8d79d
fix: Add simple press animation
Feb 10, 2022
5597bc8
feat: Add radius attribute to xml
Feb 10, 2022
4f3f9a4
Merge pull request #3 from tamimattafi/fix/shadow_elevation
tamimattafi Feb 10, 2022
e7b6f91
feat: Add flat press animation
Feb 10, 2022
bfd73b6
fix: add basin press animation
Feb 10, 2022
33fcaf8
feat: Add support for shape drawable
Feb 10, 2022
189ffa6
Merge pull request #4 from tamimattafi/feat/click_animation
tamimattafi Feb 10, 2022
605e9cc
fix: Crash when setting drawable state from xml
Feb 10, 2022
700907a
update: Migrate to material components
Feb 10, 2022
a288797
fix: Oval shape is sharp for rectangles
Feb 10, 2022
2d8519c
refactor: Use LruCache for caching bitmaps, use 1/6 of available VM m…
Feb 11, 2022
f0c6f56
refactor: Add possibility to configure cache size
Feb 11, 2022
9dbaced
fix: Optimize basin shape
Feb 11, 2022
349c266
fix: put a low cache size instead of dynamic one
Apr 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
}
}

Expand Down
14 changes: 3 additions & 11 deletions neumorphism/src/main/java/soup/neumorphism/CornerFamily.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
package soup.neumorphism

import androidx.annotation.IntDef
import androidx.annotation.RestrictTo

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@IntDef(CornerFamily.ROUNDED, CornerFamily.OVAL)
@Retention(AnnotationRetention.SOURCE)
annotation class CornerFamily {
companion object {
const val ROUNDED = 0
const val OVAL = 1
}
enum class CornerFamily {
ROUNDED,
OVAL
}
85 changes: 24 additions & 61 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.util.Log
import androidx.annotation.ColorInt
import androidx.appcompat.widget.AppCompatButton
import soup.neumorphism.internal.util.NeumorphResources
import kotlin.math.roundToInt

open class NeumorphButton @JvmOverloads constructor(
context: Context,
Expand All @@ -19,11 +20,6 @@ open class NeumorphButton @JvmOverloads constructor(
private var isInitialized: Boolean = false
private val shapeDrawable: NeumorphShapeDrawable

private var insetStart = 0
private var insetEnd = 0
private var insetTop = 0
private var insetBottom = 0

init {
val a = context.obtainStyledAttributes(
attrs, R.styleable.NeumorphButton, defStyleAttr, defStyleRes
Expand All @@ -32,25 +28,19 @@ open class NeumorphButton @JvmOverloads constructor(
val fillColor = a.getColorStateList(R.styleable.NeumorphButton_neumorph_backgroundColor)
val strokeColor = a.getColorStateList(R.styleable.NeumorphButton_neumorph_strokeColor)
val strokeWidth = a.getDimension(R.styleable.NeumorphButton_neumorph_strokeWidth, 0f)
val shapeType = a.getInt(R.styleable.NeumorphButton_neumorph_shapeType, ShapeType.DEFAULT)
val inset = a.getDimensionPixelSize(
R.styleable.NeumorphButton_neumorph_inset, 0
)
val insetStart = a.getDimensionPixelSize(
R.styleable.NeumorphButton_neumorph_insetStart, -1
)
val insetEnd = a.getDimensionPixelSize(
R.styleable.NeumorphButton_neumorph_insetEnd, -1
)
val insetTop = a.getDimensionPixelSize(
R.styleable.NeumorphButton_neumorph_insetTop, -1
)
val insetBottom = a.getDimensionPixelSize(
R.styleable.NeumorphButton_neumorph_insetBottom, -1
)
val shapeType = a.getInt(R.styleable.NeumorphButton_neumorph_shapeType, ShapeType.FLAT.ordinal)
.let { ordinal ->
ShapeType.values()[ordinal]
}

val shadowElevation = a.getDimension(
R.styleable.NeumorphButton_neumorph_shadowElevation, 0f
)
).roundToInt()

val shadowRadius = a.getDimension(
R.styleable.NeumorphButton_neumorph_shadowRadius, 15f
).roundToInt()

val shadowColorLight = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphButton_neumorph_shadowColorLight,
Expand All @@ -67,19 +57,15 @@ open class NeumorphButton @JvmOverloads constructor(
setInEditMode(isInEditMode)
setShapeType(shapeType)
setShadowElevation(shadowElevation)
setShadowRadius(shadowRadius)
setShadowColorLight(shadowColorLight)
setShadowColorDark(shadowColorDark)
setBackgroundDrawable(backgroundDrawable)
setFillColor(fillColor)
setStroke(strokeWidth, strokeColor)
setTranslationZ(translationZ)
}
internalSetInset(
if (insetStart >= 0) insetStart else inset,
if (insetTop >= 0) insetTop else inset,
if (insetEnd >= 0) insetEnd else inset,
if (insetBottom >= 0) insetBottom else inset
)

setBackgroundInternal(shapeDrawable)
isInitialized = true
}
Expand Down Expand Up @@ -136,51 +122,28 @@ open class NeumorphButton @JvmOverloads constructor(
return shapeDrawable.getStrokeWidth()
}

fun setShapeType(@ShapeType shapeType: Int) {
fun setShapeType(shapeType: ShapeType) {
shapeDrawable.setShapeType(shapeType)
}

@ShapeType
fun getShapeType(): Int {
fun getShapeType(): ShapeType {
return shapeDrawable.getShapeType()
}

fun setInset(left: Int, top: Int, right: Int, bottom: Int) {
internalSetInset(left, top, right, bottom)
fun setShadowElevation(shadowElevation: Int) {
shapeDrawable.setShadowElevation(shadowElevation)
}

private fun internalSetInset(left: Int, top: Int, right: Int, bottom: Int) {
var changed = false
if (insetStart != left) {
changed = true
insetStart = left
}
if (insetTop != top) {
changed = true
insetTop = top
}
if (insetEnd != right) {
changed = true
insetEnd = right
}
if (insetBottom != bottom) {
changed = true
insetBottom = bottom
}

if (changed) {
shapeDrawable.setInset(left, top, right, bottom)
requestLayout()
invalidateOutline()
}
fun getShadowElevation(): Int {
return shapeDrawable.getShadowElevation()
}

fun setShadowElevation(shadowElevation: Float) {
shapeDrawable.setShadowElevation(shadowElevation)
fun setNeumorphShadowRadius(shadowRadius: Int) {
shapeDrawable.setShadowRadius(shadowRadius)
}

fun getShadowElevation(): Float {
return shapeDrawable.getShadowElevation()
fun getNeumorphShadowRadius(): Int {
return shapeDrawable.getShadowRadius()
}

fun setShadowColorLight(@ColorInt shadowColor: Int) {
Expand Down
84 changes: 23 additions & 61 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphCardView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.view.View
import android.widget.FrameLayout
import androidx.annotation.ColorInt
import soup.neumorphism.internal.util.NeumorphResources
import kotlin.math.roundToInt

open class NeumorphCardView @JvmOverloads constructor(
context: Context,
Expand All @@ -21,11 +22,6 @@ open class NeumorphCardView @JvmOverloads constructor(
private var isInitialized: Boolean = false
private val shapeDrawable: NeumorphShapeDrawable

private var insetStart = 0
private var insetEnd = 0
private var insetTop = 0
private var insetBottom = 0

init {
val a = context.obtainStyledAttributes(
attrs, R.styleable.NeumorphCardView, defStyleAttr, defStyleRes
Expand All @@ -34,25 +30,19 @@ open class NeumorphCardView @JvmOverloads constructor(
val fillColor = a.getColorStateList(R.styleable.NeumorphCardView_neumorph_backgroundColor)
val strokeColor = a.getColorStateList(R.styleable.NeumorphCardView_neumorph_strokeColor)
val strokeWidth = a.getDimension(R.styleable.NeumorphCardView_neumorph_strokeWidth, 0f)
val shapeType = a.getInt(R.styleable.NeumorphCardView_neumorph_shapeType, ShapeType.DEFAULT)
val inset = a.getDimensionPixelSize(
R.styleable.NeumorphCardView_neumorph_inset, 0
)
val insetStart = a.getDimensionPixelSize(
R.styleable.NeumorphCardView_neumorph_insetStart, -1
)
val insetEnd = a.getDimensionPixelSize(
R.styleable.NeumorphCardView_neumorph_insetEnd, -1
)
val insetTop = a.getDimensionPixelSize(
R.styleable.NeumorphCardView_neumorph_insetTop, -1
)
val insetBottom = a.getDimensionPixelSize(
R.styleable.NeumorphCardView_neumorph_insetBottom, -1
)
val shapeType = a.getInt(R.styleable.NeumorphCardView_neumorph_shapeType, ShapeType.FLAT.ordinal)
.let { ordinal ->
ShapeType.values()[ordinal]
}

val shadowElevation = a.getDimension(
R.styleable.NeumorphCardView_neumorph_shadowElevation, 0f
)
).roundToInt()

val shadowRadius = a.getDimension(
R.styleable.NeumorphCardView_neumorph_shadowRadius, 15f
).roundToInt()

val shadowColorLight = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphCardView_neumorph_shadowColorLight,
Expand All @@ -69,19 +59,14 @@ open class NeumorphCardView @JvmOverloads constructor(
setInEditMode(isInEditMode)
setShapeType(shapeType)
setShadowElevation(shadowElevation)
setShadowRadius(shadowRadius)
setShadowColorLight(shadowColorLight)
setShadowColorDark(shadowColorDark)
setBackgroundDrawable(backgroundDrawable)
setFillColor(fillColor)
setStroke(strokeWidth, strokeColor)
setTranslationZ(translationZ)
}
internalSetInset(
if (insetStart >= 0) insetStart else inset,
if (insetTop >= 0) insetTop else inset,
if (insetEnd >= 0) insetEnd else inset,
if (insetBottom >= 0) insetBottom else inset
)
setBackgroundInternal(shapeDrawable)
isInitialized = true
}
Expand Down Expand Up @@ -148,51 +133,28 @@ open class NeumorphCardView @JvmOverloads constructor(
return shapeDrawable.getStrokeWidth()
}

fun setShapeType(@ShapeType shapeType: Int) {
fun setShapeType(shapeType: ShapeType) {
shapeDrawable.setShapeType(shapeType)
}

@ShapeType
fun getShapeType(): Int {
fun getShapeType(): ShapeType {
return shapeDrawable.getShapeType()
}

fun setInset(left: Int, top: Int, right: Int, bottom: Int) {
internalSetInset(left, top, right, bottom)
fun setShadowElevation(shadowElevation: Int) {
shapeDrawable.setShadowElevation(shadowElevation)
}

private fun internalSetInset(left: Int, top: Int, right: Int, bottom: Int) {
var changed = false
if (insetStart != left) {
changed = true
insetStart = left
}
if (insetTop != top) {
changed = true
insetTop = top
}
if (insetEnd != right) {
changed = true
insetEnd = right
}
if (insetBottom != bottom) {
changed = true
insetBottom = bottom
}

if (changed) {
shapeDrawable.setInset(left, top, right, bottom)
requestLayout()
invalidateOutline()
}
fun getShadowElevation(): Int {
return shapeDrawable.getShadowElevation()
}

fun setShadowElevation(shadowElevation: Float) {
shapeDrawable.setShadowElevation(shadowElevation)
fun setNeumorphShadowRadius(shadowRadius: Int) {
shapeDrawable.setShadowRadius(shadowRadius)
}

fun getShadowElevation(): Float {
return shapeDrawable.getShadowElevation()
fun getNeumorphShadowRadius(): Int {
return shapeDrawable.getShadowRadius()
}

fun setShadowColorLight(@ColorInt shadowColor: Int) {
Expand Down
Loading