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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: Add flat press animation
  • Loading branch information
Tamim Attafi authored and Tamim Attafi committed Feb 10, 2022
commit e7b6f918d37495cdb5c3a49c768d21b12413fd28
Original file line number Diff line number Diff line change
@@ -156,6 +156,7 @@ open class NeumorphButton @JvmOverloads constructor(

override fun setTranslationZ(translationZ: Float) {
super.setTranslationZ(translationZ)
val elevation = elevation
if (isInitialized) {
shapeDrawable.setTranslationZ(translationZ)
}
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import androidx.annotation.StyleRes
import soup.neumorphism.internal.drawable.NeumorphShadow
import soup.neumorphism.internal.shape.Shape
import soup.neumorphism.internal.shape.ShapeFactory
import kotlin.math.abs


open class NeumorphShapeDrawable : Drawable {
@@ -309,13 +310,9 @@ open class NeumorphShapeDrawable : Drawable {
drawBackgroundBitmap(canvas)
}

val elevation = drawableState.shadowElevation
val z = drawableState.shadowElevation + drawableState.translationZ

val pressPercentage = z / elevation
shadowPaint.alpha = (255 * pressPercentage).toInt()

shadow?.draw(canvas, shadowPaint)
val minTranslationZ = -18.74f
val pressPercentage = abs(drawableState.translationZ) / abs(minTranslationZ)
shadow?.draw(canvas, pressPercentage)

if (hasStroke()) {
drawStrokeShape(canvas)
Original file line number Diff line number Diff line change
@@ -26,8 +26,8 @@ class NeumorphBasinShape(
bounds
)

override fun draw(canvas: Canvas, paint: Paint?) {
outerShadow.draw(canvas, paint)
innerShadow.draw(canvas, null)
override fun draw(canvas: Canvas, pressPercentage: Float) {
outerShadow.draw(canvas, pressPercentage)
innerShadow.draw(canvas, pressPercentage)
}
}
Original file line number Diff line number Diff line change
@@ -8,27 +8,29 @@ import soup.neumorphism.internal.drawable.NeumorphInnerShadow
import soup.neumorphism.internal.drawable.NeumorphOuterShadow
import soup.neumorphism.internal.drawable.NeumorphShadow

class NeumorphShape(
class NeumorphFlatShape(
private val appearance: NeumorphShadow.Style,
private val theme: NeumorphShadow.Theme,
private val bounds: Rect,
private val isOuterShadow: Boolean
private val bounds: Rect
) : Shape {

private val shadow: NeumorphShadow
get() = if (isOuterShadow) NeumorphOuterShadow(appearance, theme, bounds)
else NeumorphInnerShadow(appearance, theme, bounds)
get() = NeumorphOuterShadow(appearance, theme, bounds)

private val shadowBitmap: Bitmap by lazy {
shadow.drawToBitmap()
}

override fun draw(canvas: Canvas, paint: Paint?) {
private val shadowPaint = Paint()

override fun draw(canvas: Canvas, pressPercentage: Float) {
val pressOffset = 1 - pressPercentage
shadowPaint.alpha = (255 * pressOffset).toInt()
canvas.drawBitmap(
shadowBitmap,
0f,
0f,
paint
shadowPaint
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package soup.neumorphism.internal.shape

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import soup.neumorphism.internal.drawable.NeumorphInnerShadow
import soup.neumorphism.internal.drawable.NeumorphOuterShadow
import soup.neumorphism.internal.drawable.NeumorphShadow

class NeumorphPressedShape(
private val appearance: NeumorphShadow.Style,
private val theme: NeumorphShadow.Theme,
private val bounds: Rect
) : Shape {

private val shadow: NeumorphShadow
get() = NeumorphInnerShadow(appearance, theme, bounds)

private val shadowBitmap: Bitmap by lazy {
shadow.drawToBitmap()
}

override fun draw(canvas: Canvas, pressPercentage: Float) {
canvas.drawBitmap(shadowBitmap, 0f, 0f, null)
}
}
Original file line number Diff line number Diff line change
@@ -6,5 +6,5 @@ import android.graphics.Path
import android.graphics.Rect

interface Shape {
fun draw(canvas: Canvas, paint: Paint?)
fun draw(canvas: Canvas, pressPercentage: Float)
}
Original file line number Diff line number Diff line change
@@ -27,18 +27,16 @@ object ShapeFactory {
): Shape {

val shape = when (shapeType) {
ShapeType.FLAT -> NeumorphShape(
ShapeType.FLAT -> NeumorphFlatShape(
appearance,
theme,
bounds,
isOuterShadow = true
bounds
)

ShapeType.PRESSED -> NeumorphShape(
ShapeType.PRESSED -> NeumorphPressedShape(
appearance,
theme,
bounds,
isOuterShadow = false
bounds
)

ShapeType.BASIN -> NeumorphBasinShape(