Skip to content

Commit

Permalink
增加修改上下阴影比例
Browse files Browse the repository at this point in the history
增加修改上下阴影比例
  • Loading branch information
19931121bigman committed Jul 27, 2018
1 parent f9ca539 commit 797c614
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 30 deletions.
4 changes: 4 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white"
>

<com.bigman.wmzx.customcardview.library.CardView
Expand All @@ -16,6 +17,7 @@
app:endColor="@color/cardview_shadow_end_color"
app:cardCornerRadius="4dp"
app:cardElevation="10dp"
app:topDelta="-1"
>

<LinearLayout
Expand Down Expand Up @@ -125,6 +127,7 @@
app:endColor="@color/cardview_shadow_end_color"
app:cardCornerRadius="4dp"
app:cardElevation="10dp"
app:topDelta="0"
>

<LinearLayout
Expand Down Expand Up @@ -234,6 +237,7 @@
app:endColor="@color/cardview_shadow_end_color"
app:cardCornerRadius="4dp"
app:cardElevation="10dp"
app:topDelta="1"
>

<LinearLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ class CardView : FrameLayout {
}
}

private var mTopDelta: Float=1f

private fun initialize(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
val a = context.obtainStyledAttributes(attrs, R.styleable.CardView, defStyleAttr,
R.style.CardView)
Expand Down Expand Up @@ -425,6 +427,9 @@ class CardView : FrameLayout {

val a2 = context.obtainStyledAttributes(attrs, R.styleable.CardViewShadow)
val n = a2.indexCount
if(a2.hasValue(R.styleable.CardViewShadow_topDelta)){
mTopDelta=a2.getFloat(R.styleable.CardViewShadow_topDelta,0f)
}
if (a2.hasValue(R.styleable.CardViewShadow_endColor) && a2.hasValue(R.styleable.CardViewShadow_startColor)) {
for (i in 0 until n) {
val attr = a.getIndex(i)
Expand All @@ -435,10 +440,10 @@ class CardView : FrameLayout {

}
IMPL.initialize(mCardViewDelegate, context, backgroundColor, radius,
elevation, maxElevation, mStartColor, mEndColor)
elevation, maxElevation, mStartColor, mEndColor,mTopDelta)
} else {
IMPL.initialize(mCardViewDelegate, context, backgroundColor, radius,
elevation, maxElevation)
elevation, maxElevation,mTopDelta)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import com.bigman.wmzx.cardviewlibrary.library.CardViewDelegate
* create time == 15:48
*/
internal class CardViewApi17Impl : CardViewBaseImpl() {
override fun initialize(cardView: CardViewDelegate, context: Context, backgroundColor: ColorStateList, radius: Float, elevation: Float, maxElevation: Float) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}


override fun initStatic() {
RoundRectDrawableWithShadow.sRoundRectHelper = object : RoundRectDrawableWithShadow.RoundRectHelper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ import com.bigman.wmzx.customcardview.library.RoundRectDrawableWithShadow

@RequiresApi(21)
internal class CardViewApi21Impl : CardViewImpl {
override fun initialize(cardViewDelegate: CardViewDelegate, context: Context, backgroundColor: ColorStateList,
radius: Float, elevation: Float, maxElevation: Float, startColor: Int, endColor: Int, topDelta: Float) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun initialize(cardView: CardViewDelegate, context: Context,
backgroundColor: ColorStateList, radius: Float, elevation: Float, maxElevation: Float) {
override fun initialize(cardView: CardViewDelegate, context: Context, backgroundColor: ColorStateList, radius: Float
, elevation: Float, maxElevation: Float, topDelta: Float) {
val background = RoundRectDrawable(backgroundColor, radius)
cardView.cardBackground = background

Expand All @@ -26,6 +30,8 @@ internal class CardViewApi21Impl : CardViewImpl {
setMaxElevation(cardView, maxElevation)
}



override fun setRadius(cardView: CardViewDelegate, radius: Float) {
getCardBackground(cardView).radius = radius
}
Expand Down Expand Up @@ -92,9 +98,7 @@ internal class CardViewApi21Impl : CardViewImpl {
return getCardBackground(cardView).color
}

override fun initialize(cardViewDelegate: CardViewDelegate, context: Context, backgroundColor: ColorStateList, radius: Float, elevation: Float, maxElevation: Float, startColor: Int, endColor: Int) {

}

private fun getCardBackground(cardView: CardViewDelegate): RoundRectDrawable {
return cardView.cardBackground as RoundRectDrawable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ public void initStatic() {

@Override
public void initialize(CardViewDelegate cardView, Context context,
ColorStateList backgroundColor, float radius, float elevation, float maxElevation) {
ColorStateList backgroundColor, float radius, float elevation, float maxElevation,float topDelta) {
RoundRectDrawableWithShadow background = createBackground(context, backgroundColor, radius,
elevation, maxElevation);
elevation, maxElevation,topDelta);
background.setAddPaddingForCorners(cardView.getPreventCornerOverlap());
cardView.setCardBackground(background);
updatePadding(cardView);
}

private RoundRectDrawableWithShadow createBackground(Context context,
ColorStateList backgroundColor, float radius, float elevation,
float maxElevation) {
float maxElevation,float topDelta) {
return new RoundRectDrawableWithShadow(context.getResources(), backgroundColor, radius,
elevation, maxElevation);
elevation, maxElevation,topDelta);
}

@Override
Expand Down Expand Up @@ -108,19 +108,19 @@ public ColorStateList getBackgroundColor(CardViewDelegate cardView) {
@Override
public void initialize(CardViewDelegate cardView, Context context,
ColorStateList backgroundColor, float radius, float elevation,
float maxElevation, int startColor, int endColor) {
float maxElevation, int startColor, int endColor,float topDelta) {
RoundRectDrawableWithShadow background = createBackground(context, backgroundColor, radius,
elevation, maxElevation,startColor,endColor);
elevation, maxElevation,startColor,endColor,topDelta);
background.setAddPaddingForCorners(cardView.getPreventCornerOverlap());
cardView.setCardBackground(background);
updatePadding(cardView);
}

private RoundRectDrawableWithShadow createBackground(Context context, ColorStateList backgroundColor,
float radius, float elevation, float maxElevation,
int startColor, int endColor) {
int startColor, int endColor,float topDelta) {
return new RoundRectDrawableWithShadow(context.getResources(), backgroundColor, radius,
elevation, maxElevation,startColor,endColor);
elevation, maxElevation,startColor,endColor,topDelta);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.bigman.wmzx.cardviewlibrary.library.CardViewDelegate

internal interface CardViewImpl {
fun initialize(cardView: CardViewDelegate, context: Context, backgroundColor: ColorStateList,
radius: Float, elevation: Float, maxElevation: Float)
radius: Float, elevation: Float, maxElevation: Float, topDelta: Float)

fun setRadius(cardView: CardViewDelegate, radius: Float)

Expand Down Expand Up @@ -38,5 +38,6 @@ internal interface CardViewImpl {
fun getBackgroundColor(cardView: CardViewDelegate): ColorStateList

fun initialize(cardViewDelegate: CardViewDelegate, context: Context,
backgroundColor: ColorStateList, radius: Float, elevation: Float, maxElevation: Float, startColor: Int, endColor: Int)
backgroundColor: ColorStateList, radius: Float, elevation: Float
, maxElevation: Float, startColor: Int, endColor: Int, topDelta: Float)
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ internal class RoundRectDrawableWithShadow : Drawable {
private val mShadowStartColor: Int

private val mShadowEndColor: Int
private var mTopDelta: Float

private var mAddPaddingForCorners = true

Expand Down Expand Up @@ -95,9 +96,10 @@ internal class RoundRectDrawableWithShadow : Drawable {
}

constructor(resources: Resources, backgroundColor: ColorStateList, radius: Float,
shadowSize: Float, maxShadowSize: Float) {
shadowSize: Float, maxShadowSize: Float,topDelta:Float) {
mShadowStartColor = resources.getColor(R.color.cardview_shadow_start_color)
mShadowEndColor = resources.getColor(R.color.cardview_shadow_end_color)
mTopDelta =topDelta
mInsetShadow = resources.getDimensionPixelSize(R.dimen.cardview_compat_inset_shadow)
mPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
setBackground(backgroundColor)
Expand All @@ -112,9 +114,10 @@ internal class RoundRectDrawableWithShadow : Drawable {

constructor(resources: Resources, backgroundColor: ColorStateList,
radius: Float, shadowSize: Float, maxShadowSize: Float,
startColor: Int, endColor: Int) {
startColor: Int, endColor: Int,topDelta:Float) {
mShadowStartColor = startColor
mShadowEndColor = endColor
mTopDelta = topDelta
mInsetShadow = resources.getDimensionPixelSize(R.dimen.cardview_compat_inset_shadow)
mPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
setBackground(backgroundColor)
Expand Down Expand Up @@ -219,23 +222,34 @@ internal class RoundRectDrawableWithShadow : Drawable {
return PixelFormat.TRANSLUCENT
}

/**
* 绘制主要工作
*/
override fun draw(canvas: Canvas) {
if (mDirty) {
buildComponents(bounds)
mDirty = false
}
canvas.translate(0f, mRawShadowSize / 2)
if(mTopDelta>1f){
mTopDelta=1f
}else if(mTopDelta>-1f&&mTopDelta<0f){
mTopDelta=-1f
}
canvas.translate(0f, mRawShadowSize / 2*mTopDelta)
drawShadow(canvas)
canvas.translate(0f, -mRawShadowSize / 2)
canvas.translate(0f, -mRawShadowSize / 2*mTopDelta)
sRoundRectHelper!!.drawRoundRect(canvas, mCardBounds, mCornerRadius, mPaint)
}

/**
* 绘制四个角阴影
*/
private fun drawShadow(canvas: Canvas) {
val edgeShadowTop = -mCornerRadius - mShadowSize
val inset = mCornerRadius + mInsetShadow.toFloat() + mRawShadowSize / 2
val drawHorizontalEdges = mCardBounds.width() - 2 * inset > 0
val drawVerticalEdges = mCardBounds.height() - 2 * inset > 0
// LT
// LT 左上
var saved = canvas.save()
canvas.translate(mCardBounds.left + inset, mCardBounds.top + inset)
canvas.drawPath(mCornerShadowPath!!, mCornerShadowPaint!!)
Expand All @@ -245,7 +259,7 @@ internal class RoundRectDrawableWithShadow : Drawable {
mEdgeShadowPaint!!)
}
canvas.restoreToCount(saved)
// RB
// RB 右下
saved = canvas.save()
canvas.translate(mCardBounds.right - inset, mCardBounds.bottom - inset)
canvas.rotate(180f)
Expand All @@ -256,7 +270,7 @@ internal class RoundRectDrawableWithShadow : Drawable {
mEdgeShadowPaint!!)
}
canvas.restoreToCount(saved)
// LB
// LB 左下
saved = canvas.save()
canvas.translate(mCardBounds.left + inset, mCardBounds.bottom - inset)
canvas.rotate(270f)
Expand All @@ -266,7 +280,7 @@ internal class RoundRectDrawableWithShadow : Drawable {
mCardBounds.height() - 2 * inset, -mCornerRadius, mEdgeShadowPaint!!)
}
canvas.restoreToCount(saved)
// RT
// RT 右上
saved = canvas.save()
canvas.translate(mCardBounds.right - inset, mCardBounds.top + inset)
canvas.rotate(90f)
Expand Down
10 changes: 7 additions & 3 deletions cardviewlibrary/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>


<attr name="startColor" format="reference|color"/>
<attr name="topDelta" format="float"/>
<!-- Selected Paint Color (color). -->
<attr name="endColor" format="reference|color"/>
<declare-styleable name="CardViewShadow">
<!-- Default Paint Color (color). -->
<attr name="startColor" format="reference|color"/>
<attr name="startColor" />
<!-- Selected Paint Color (color). -->
<attr name="endColor" format="reference|color"/>
<attr name="endColor" />
<attr name="topDelta" />
</declare-styleable>


Expand Down

0 comments on commit 797c614

Please sign in to comment.