Skip to content

Commit

Permalink
Library: Support backgroundColor, stroke for NeumorphShapeDrawable
Browse files Browse the repository at this point in the history
  • Loading branch information
fornewid committed May 8, 2020
1 parent 5a249b6 commit db5da70
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 18 deletions.
32 changes: 31 additions & 1 deletion neumorphism/src/main/java/soup/neumorphism/NeumorphButton.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package soup.neumorphism

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
Expand All @@ -21,6 +22,9 @@ class NeumorphButton @JvmOverloads constructor(
val a = context.obtainStyledAttributes(
attrs, R.styleable.NeumorphButton, defStyleAttr, defStyleRes
)
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 shadowElevation = a.getDimension(
R.styleable.NeumorphButton_neumorph_shadowElevation, 0f
Expand All @@ -38,6 +42,8 @@ class NeumorphButton @JvmOverloads constructor(
setShadowElevation(shadowElevation)
setShadowColorLight(shadowColorLight)
setShadowColorDark(shadowColorDark)
setFillColor(fillColor)
setStroke(strokeWidth, strokeColor)
}
setBackgroundInternal(shapeDrawable)
}
Expand All @@ -47,7 +53,7 @@ class NeumorphButton @JvmOverloads constructor(
}

override fun setBackgroundDrawable(drawable: Drawable?) {
Log.i(LOG_TAG, "Setting a custom background is not supported.");
Log.i(LOG_TAG, "Setting a custom background is not supported.")
}

private fun setBackgroundInternal(drawable: Drawable?) {
Expand All @@ -62,6 +68,30 @@ class NeumorphButton @JvmOverloads constructor(
return shapeDrawable.getShapeAppearanceModel()
}

fun setBackgroundColor(backgroundColor: ColorStateList?) {
shapeDrawable.setFillColor(backgroundColor)
}

fun getBackgroundColor(): ColorStateList? {
return shapeDrawable.getFillColor()
}

fun setStrokeColor(strokeColor: ColorStateList?) {
shapeDrawable.setStrokeColor(strokeColor)
}

fun getStrokeColor(): ColorStateList? {
return shapeDrawable.getStrokeColor()
}

fun setStrokeWidth(strokeWidth: Float) {
shapeDrawable.setStrokeWidth(strokeWidth)
}

fun getStrokeWidth(): Float {
return shapeDrawable.getStrokeWidth()
}

fun setShapeType(@ShapeType shapeType: Int) {
shapeDrawable.setShapeType(shapeType)
}
Expand Down
33 changes: 32 additions & 1 deletion neumorphism/src/main/java/soup/neumorphism/NeumorphCardView.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package soup.neumorphism

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
Expand All @@ -21,6 +22,9 @@ class NeumorphCardView @JvmOverloads constructor(
val a = context.obtainStyledAttributes(
attrs, R.styleable.NeumorphCardView, defStyleAttr, defStyleRes
)
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 shadowElevation = a.getDimension(
R.styleable.NeumorphCardView_neumorph_shadowElevation, 0f
Expand All @@ -38,6 +42,8 @@ class NeumorphCardView @JvmOverloads constructor(
setShadowElevation(shadowElevation)
setShadowColorLight(shadowColorLight)
setShadowColorDark(shadowColorDark)
setFillColor(fillColor)
setStroke(strokeWidth, strokeColor)
}
setBackgroundInternal(shapeDrawable)
}
Expand All @@ -47,7 +53,7 @@ class NeumorphCardView @JvmOverloads constructor(
}

override fun setBackgroundDrawable(drawable: Drawable?) {
Log.i(LOG_TAG, "Setting a custom background is not supported.");
Log.i(LOG_TAG, "Setting a custom background is not supported.")
}

private fun setBackgroundInternal(drawable: Drawable?) {
Expand All @@ -62,6 +68,31 @@ class NeumorphCardView @JvmOverloads constructor(
return shapeDrawable.getShapeAppearanceModel()
}


fun setBackgroundColor(backgroundColor: ColorStateList?) {
shapeDrawable.setFillColor(backgroundColor)
}

fun getBackgroundColor(): ColorStateList? {
return shapeDrawable.getFillColor()
}

fun setStrokeColor(strokeColor: ColorStateList?) {
shapeDrawable.setStrokeColor(strokeColor)
}

fun getStrokeColor(): ColorStateList? {
return shapeDrawable.getStrokeColor()
}

fun setStrokeWidth(strokeWidth: Float) {
shapeDrawable.setStrokeWidth(strokeWidth)
}

fun getStrokeWidth(): Float {
return shapeDrawable.getStrokeWidth()
}

fun setShapeType(@ShapeType shapeType: Int) {
shapeDrawable.setShapeType(shapeType)
}
Expand Down
30 changes: 30 additions & 0 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphEditText.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package soup.neumorphism

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Canvas
import android.graphics.Color
import android.util.AttributeSet
Expand Down Expand Up @@ -29,6 +30,9 @@ class NeumorphEditText @JvmOverloads constructor(
val a = context.obtainStyledAttributes(
attrs, R.styleable.NeumorphEditText, defStyleAttr, defStyleRes
)
val fillColor = a.getColorStateList(R.styleable.NeumorphEditText_neumorph_backgroundColor)
val strokeColor = a.getColorStateList(R.styleable.NeumorphEditText_neumorph_strokeColor)
val strokeWidth = a.getDimension(R.styleable.NeumorphEditText_neumorph_strokeWidth, 0f)
val shapeType = a.getInt(R.styleable.NeumorphEditText_neumorph_shapeType, ShapeType.DEFAULT)
val shadowElevation = a.getDimension(
R.styleable.NeumorphEditText_neumorph_shadowElevation, 0f
Expand All @@ -46,6 +50,8 @@ class NeumorphEditText @JvmOverloads constructor(
setShadowElevation(shadowElevation)
setShadowColorLight(shadowColorLight)
setShadowColorDark(shadowColorDark)
setFillColor(fillColor)
setStroke(strokeWidth, strokeColor)
}
with(context.resources) {
underlineHeight = getDimensionPixelSize(R.dimen.edit_text_underline_height)
Expand Down Expand Up @@ -93,6 +99,30 @@ class NeumorphEditText @JvmOverloads constructor(
return shapeDrawable.getShapeAppearanceModel()
}

fun setBackgroundColor(backgroundColor: ColorStateList?) {
shapeDrawable.setFillColor(backgroundColor)
}

fun getBackgroundColor(): ColorStateList? {
return shapeDrawable.getFillColor()
}

fun setStrokeColor(strokeColor: ColorStateList?) {
shapeDrawable.setStrokeColor(strokeColor)
}

fun getStrokeColor(): ColorStateList? {
return shapeDrawable.getStrokeColor()
}

fun setStrokeWidth(strokeWidth: Float) {
shapeDrawable.setStrokeWidth(strokeWidth)
}

fun getStrokeWidth(): Float {
return shapeDrawable.getStrokeWidth()
}

fun setShapeType(@ShapeType shapeType: Int) {
shapeDrawable.setShapeType(shapeType)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package soup.neumorphism

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
Expand All @@ -21,6 +22,9 @@ class NeumorphFloatingActionButton @JvmOverloads constructor(
val a = context.obtainStyledAttributes(
attrs, R.styleable.NeumorphFloatingActionButton, defStyleAttr, defStyleRes
)
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.NeumorphFloatingActionButton_neumorph_shapeType, ShapeType.DEFAULT)
val shadowElevation = a.getDimension(
Expand All @@ -39,6 +43,8 @@ class NeumorphFloatingActionButton @JvmOverloads constructor(
setShadowElevation(shadowElevation)
setShadowColorLight(shadowColorLight)
setShadowColorDark(shadowColorDark)
setFillColor(fillColor)
setStroke(strokeWidth, strokeColor)
}
setBackgroundInternal(shapeDrawable)
}
Expand All @@ -48,7 +54,7 @@ class NeumorphFloatingActionButton @JvmOverloads constructor(
}

override fun setBackgroundDrawable(drawable: Drawable?) {
Log.i(LOG_TAG, "Setting a custom background is not supported.");
Log.i(LOG_TAG, "Setting a custom background is not supported.")
}

private fun setBackgroundInternal(drawable: Drawable?) {
Expand All @@ -63,6 +69,30 @@ class NeumorphFloatingActionButton @JvmOverloads constructor(
return shapeDrawable.getShapeAppearanceModel()
}

fun setBackgroundColor(backgroundColor: ColorStateList?) {
shapeDrawable.setFillColor(backgroundColor)
}

fun getBackgroundColor(): ColorStateList? {
return shapeDrawable.getFillColor()
}

fun setStrokeColor(strokeColor: ColorStateList?) {
shapeDrawable.setStrokeColor(strokeColor)
}

fun getStrokeColor(): ColorStateList? {
return shapeDrawable.getStrokeColor()
}

fun setStrokeWidth(strokeWidth: Float) {
shapeDrawable.setStrokeWidth(strokeWidth)
}

fun getStrokeWidth(): Float {
return shapeDrawable.getStrokeWidth()
}

fun setShapeType(@ShapeType shapeType: Int) {
shapeDrawable.setShapeType(shapeType)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package soup.neumorphism

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
Expand All @@ -22,6 +23,9 @@ class NeumorphImageButton @JvmOverloads constructor(
val a = context.obtainStyledAttributes(
attrs, R.styleable.NeumorphImageButton, defStyleAttr, defStyleRes
)
val fillColor = a.getColorStateList(R.styleable.NeumorphImageButton_neumorph_backgroundColor)
val strokeColor = a.getColorStateList(R.styleable.NeumorphImageButton_neumorph_strokeColor)
val strokeWidth = a.getDimension(R.styleable.NeumorphImageButton_neumorph_strokeWidth, 0f)
val shapeType = a.getInt(R.styleable.NeumorphImageButton_neumorph_shapeType, ShapeType.DEFAULT)
val shadowElevation = a.getDimension(
R.styleable.NeumorphImageButton_neumorph_shadowElevation, 0f
Expand All @@ -39,6 +43,8 @@ class NeumorphImageButton @JvmOverloads constructor(
setShadowElevation(shadowElevation)
setShadowColorLight(shadowColorLight)
setShadowColorDark(shadowColorDark)
setFillColor(fillColor)
setStroke(strokeWidth, strokeColor)
}
setBackgroundInternal(shapeDrawable)
}
Expand All @@ -48,7 +54,7 @@ class NeumorphImageButton @JvmOverloads constructor(
}

override fun setBackgroundDrawable(drawable: Drawable?) {
Log.i(LOG_TAG, "Setting a custom background is not supported.");
Log.i(LOG_TAG, "Setting a custom background is not supported.")
}

private fun setBackgroundInternal(drawable: Drawable?) {
Expand All @@ -63,6 +69,30 @@ class NeumorphImageButton @JvmOverloads constructor(
return shapeDrawable.getShapeAppearanceModel()
}

fun setBackgroundColor(backgroundColor: ColorStateList?) {
shapeDrawable.setFillColor(backgroundColor)
}

fun getBackgroundColor(): ColorStateList? {
return shapeDrawable.getFillColor()
}

fun setStrokeColor(strokeColor: ColorStateList?) {
shapeDrawable.setStrokeColor(strokeColor)
}

fun getStrokeColor(): ColorStateList? {
return shapeDrawable.getStrokeColor()
}

fun setStrokeWidth(strokeWidth: Float) {
shapeDrawable.setStrokeWidth(strokeWidth)
}

fun getStrokeWidth(): Float {
return shapeDrawable.getStrokeWidth()
}

fun setShapeType(@ShapeType shapeType: Int) {
shapeDrawable.setShapeType(shapeType)
}
Expand Down
32 changes: 31 additions & 1 deletion neumorphism/src/main/java/soup/neumorphism/NeumorphImageView.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package soup.neumorphism

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
Expand All @@ -21,6 +22,9 @@ class NeumorphImageView @JvmOverloads constructor(
val a = context.obtainStyledAttributes(
attrs, R.styleable.NeumorphImageView, defStyleAttr, defStyleRes
)
val fillColor = a.getColorStateList(R.styleable.NeumorphImageView_neumorph_backgroundColor)
val strokeColor = a.getColorStateList(R.styleable.NeumorphImageView_neumorph_strokeColor)
val strokeWidth = a.getDimension(R.styleable.NeumorphImageView_neumorph_strokeWidth, 0f)
val shapeType = a.getInt(R.styleable.NeumorphImageView_neumorph_shapeType, ShapeType.DEFAULT)
val shadowElevation = a.getDimension(
R.styleable.NeumorphImageView_neumorph_shadowElevation, 0f
Expand All @@ -38,6 +42,8 @@ class NeumorphImageView @JvmOverloads constructor(
setShadowElevation(shadowElevation)
setShadowColorLight(shadowColorLight)
setShadowColorDark(shadowColorDark)
setFillColor(fillColor)
setStroke(strokeWidth, strokeColor)
}
setBackgroundInternal(shapeDrawable)
}
Expand All @@ -47,7 +53,7 @@ class NeumorphImageView @JvmOverloads constructor(
}

override fun setBackgroundDrawable(drawable: Drawable?) {
Log.i(LOG_TAG, "Setting a custom background is not supported.");
Log.i(LOG_TAG, "Setting a custom background is not supported.")
}

private fun setBackgroundInternal(drawable: Drawable?) {
Expand All @@ -62,6 +68,30 @@ class NeumorphImageView @JvmOverloads constructor(
return shapeDrawable.getShapeAppearanceModel()
}

fun setBackgroundColor(backgroundColor: ColorStateList?) {
shapeDrawable.setFillColor(backgroundColor)
}

fun getBackgroundColor(): ColorStateList? {
return shapeDrawable.getFillColor()
}

fun setStrokeColor(strokeColor: ColorStateList?) {
shapeDrawable.setStrokeColor(strokeColor)
}

fun getStrokeColor(): ColorStateList? {
return shapeDrawable.getStrokeColor()
}

fun setStrokeWidth(strokeWidth: Float) {
shapeDrawable.setStrokeWidth(strokeWidth)
}

fun getStrokeWidth(): Float {
return shapeDrawable.getStrokeWidth()
}

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

0 comments on commit db5da70

Please sign in to comment.