Skip to content

Commit

Permalink
Merge pull request #12 from Blankeer/master
Browse files Browse the repository at this point in the history
imp circle.support change shadow alpha.
  • Loading branch information
ToDou authored Mar 13, 2018
2 parents 76faa55 + 30c52ad commit ef9ddfa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/loopeer/example/shadows/SeekItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum class SeekItem {
PADDING("Padding"),
SHADOW_RADIUS("Shadow Radius"),
SHADOW_MARGIN("Shadow Margin"),
SHADOW_ALPHA("Shadow Alpha"),
CORNER_RADIUS("Corner Radius"),
FOREGROUND_COLOR("Foreground Color"),
SHADOW_COLOR("Shadow Color"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package com.loopeer.example.shadows

import android.content.Context
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.activity_shadow_view.*
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.flexbox.FlexboxLayout
import com.loopeer.shadow.ShadowView
import kotlinx.android.synthetic.main.activity_shadow_view.*
import kotlinx.android.synthetic.main.list_item_color_select.view.*
import kotlinx.android.synthetic.main.list_item_seek_select.view.*

Expand Down Expand Up @@ -48,7 +48,7 @@ class ShadowViewActivity : AppCompatActivity() {
holder.bind(SeekItem.values()[position], shadowView)
} else if (holder is ShadowViewColorItemHolder) {
holder.bind(SeekItem.values()[position], shadowView)
holder.onClickColor = {notifyItemChanged(position)}
holder.onClickColor = { notifyItemChanged(position) }
}
}

Expand Down Expand Up @@ -94,6 +94,12 @@ class ShadowViewActivity : AppCompatActivity() {
itemView.text_value.text = p.toString()
})
}
SeekItem.SHADOW_ALPHA -> {
itemView.seek_bar.onProgressChange({ _, p, _ ->
shadowView.shadowAlpha = p * 255 / 100
itemView.text_value.text = shadowView.shadowAlpha.toString()
})
}
SeekItem.SHADOW_MARGIN_LEFT -> {
itemView.text_value.text = shadowView.shadowMarginLeft.toString()
itemView.seek_bar.onProgressChange({ _, p, _ ->
Expand Down Expand Up @@ -184,6 +190,7 @@ class ShadowViewActivity : AppCompatActivity() {
}
}
}

class ShadowViewColorItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var onClickColor: (() -> Unit)? = null

Expand Down
17 changes: 13 additions & 4 deletions shadow/src/main/java/com/loopeer/shadow/ShadowView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import android.content.res.ColorStateList
import android.graphics.*
import android.graphics.drawable.Drawable
import android.graphics.drawable.RippleDrawable
import android.util.AttributeSet
import android.widget.FrameLayout
import android.os.Build
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout


open class ShadowView @JvmOverloads constructor(context: Context?, attributeSet: AttributeSet? = null, defStyleInt: Int = 0)
Expand Down Expand Up @@ -41,7 +41,13 @@ open class ShadowView @JvmOverloads constructor(context: Context?, attributeSet:
var backgroundClr: Int = 0
set(value) {
field = value
bgPaint.color = value
invalidate()
}

var shadowAlpha: Int = 255
set(value) {
field = value
bgPaint.alpha = value
invalidate()
}

Expand Down Expand Up @@ -107,6 +113,7 @@ open class ShadowView @JvmOverloads constructor(context: Context?, attributeSet:
shadowDx = a.getFloat(R.styleable.ShadowView_shadowDx, 0f)
shadowDy = a.getFloat(R.styleable.ShadowView_shadowDy, 1f)
shadowRadius = a.getDimensionPixelSize(R.styleable.ShadowView_shadowRadius, SIZE_DEFAULT).toFloat()
shadowAlpha = a.getInteger(R.styleable.ShadowView_shadowAlpha, 255)
val d = a.getDrawable(R.styleable.ShadowView_android_foreground)
if (d != null) {
setForeground(d)
Expand Down Expand Up @@ -138,8 +145,8 @@ open class ShadowView @JvmOverloads constructor(context: Context?, attributeSet:
}
a.recycle()

bgPaint.color = backgroundClr
bgPaint.isAntiAlias = true
bgPaint.alpha = shadowAlpha
bgPaint.style = Paint.Style.FILL
setLayerType(LAYER_TYPE_SOFTWARE, null)
setWillNotDraw(false)
Expand Down Expand Up @@ -284,6 +291,8 @@ open class ShadowView @JvmOverloads constructor(context: Context?, attributeSet:
, cornerRadiusBR
, cornerRadiusBL)
it.drawPath(path, bgPaint)
canvas.clipPath(path)
canvas.drawColor(backgroundClr) // draw backgroundColor
}
}

Expand Down
14 changes: 10 additions & 4 deletions shadow/src/main/java/com/loopeer/shadow/ShapeUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,18 @@ object ShapeUtils {
if (bl < 0) bl = 0f
val width = right - left
val height = bottom - top
if (tl > Math.min(width, height) / 2) tl = Math.min(width, height) / 2
if (tr > Math.min(width, height) / 2) tr = Math.min(width, height) / 2
if (br > Math.min(width, height) / 2) br = Math.min(width, height) / 2
if (bl > Math.min(width, height) / 2) bl = Math.min(width, height) / 2
val min = Math.min(width, height)
if (tl > min / 2) tl = min / 2
if (tr > min / 2) tr = min / 2
if (br > min / 2) br = min / 2
if (bl > min / 2) bl = min / 2
// val widthMinusCorners = width - 2 * rx
// val heightMinusCorners = height - 2 * ry
if (tl == tr && tr == br && br == bl && tl == min / 2) {
val radius = min / 2F
path.addCircle(left + radius, top + radius, radius, Path.Direction.CW)
return path
}

path.moveTo(right, top + tr)
if (tr > 0)
Expand Down
1 change: 1 addition & 0 deletions shadow/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<attr name="cornerRadiusBR" format="dimension"/>
<attr name="foregroundColor" format="color"/>
<attr name="shadowColor" format="color"/>
<attr name="shadowAlpha" format="integer"/>
<attr name="shadowDx" format="float"/>
<attr name="shadowDy" format="float"/>
<attr name="shadowRadius" format="dimension"/>
Expand Down

0 comments on commit ef9ddfa

Please sign in to comment.