Skip to content

Commit ba23a42

Browse files
author
Roman Tcaregorodtcev
authored
Merge pull request #4 from R12rus/develop
left and right shadow effect has been added
2 parents 4307c8a + 26203c5 commit ba23a42

4 files changed

Lines changed: 64 additions & 14 deletions

File tree

shadowframelayout/src/main/java/r12/shadowframelayout/ShadowFrameLayout.java

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,19 @@
2121
public class ShadowFrameLayout extends FrameLayout {
2222

2323
private static final int DEFAULT_SHADOW_HEIGHT = 0;
24-
private Drawable mTopDrawable;
25-
private int mTopShadowHeight;
26-
private Drawable mBottomDrawable;
27-
private int mBottomShadowHeight;
24+
25+
private Drawable mShadowTopDrawable;
26+
private int mShadowTopHeight;
27+
28+
private Drawable mShadowBottomDrawable;
29+
private int mShadowBottomHeight;
30+
31+
private Drawable mShadowLeftDrawable;
32+
private int mShadowLeftHeight;
33+
34+
private Drawable mShadowRightDrawable;
35+
private int mShadowRightHeight;
36+
2837

2938
public ShadowFrameLayout(@NonNull Context context) {
3039
super(context);
@@ -49,21 +58,43 @@ public ShadowFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs,
4958

5059
private void init(Context context, AttributeSet attrs) {
5160
if (attrs != null) {
52-
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShadowFrameLayout);
53-
mTopShadowHeight = typedArray.getDimensionPixelOffset(R.styleable.ShadowFrameLayout_shadowTopHeight, DEFAULT_SHADOW_HEIGHT);
54-
mBottomShadowHeight = typedArray.getDimensionPixelOffset(R.styleable.ShadowFrameLayout_shadowBottomHeight, DEFAULT_SHADOW_HEIGHT);
55-
typedArray.recycle();
61+
TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.ShadowFrameLayout);
62+
mShadowTopHeight = styledAttrs.getDimensionPixelSize(R.styleable.ShadowFrameLayout_shadowTopHeight, DEFAULT_SHADOW_HEIGHT);
63+
mShadowBottomHeight = styledAttrs.getDimensionPixelSize(R.styleable.ShadowFrameLayout_shadowBottomHeight, DEFAULT_SHADOW_HEIGHT);
64+
mShadowLeftHeight = styledAttrs.getDimensionPixelSize(R.styleable.ShadowFrameLayout_shadowLeftHeight, DEFAULT_SHADOW_HEIGHT);
65+
mShadowRightHeight = styledAttrs.getDimensionPixelSize(R.styleable.ShadowFrameLayout_shadowRightHeight, DEFAULT_SHADOW_HEIGHT);
66+
styledAttrs.recycle();
5667
}
57-
mTopDrawable = ContextCompat.getDrawable(context, R.drawable.bg_top_shadow);
58-
mBottomDrawable = ContextCompat.getDrawable(context, R.drawable.bg_bottom_shadow);
68+
69+
mShadowTopDrawable = ContextCompat.getDrawable(getContext(), R.drawable.bg_top_shadow);
70+
mShadowBottomDrawable = ContextCompat.getDrawable(getContext(), R.drawable.bg_bottom_shadow);
71+
mShadowLeftDrawable = ContextCompat.getDrawable(getContext(), R.drawable.bg_left_shadow);
72+
mShadowRightDrawable = ContextCompat.getDrawable(getContext(), R.drawable.bg_right_shadow);
5973
}
6074

6175
@Override
6276
protected void dispatchDraw(Canvas canvas) {
6377
super.dispatchDraw(canvas);
64-
mTopDrawable.setBounds(0, 0, canvas.getWidth(), mTopShadowHeight);
65-
mTopDrawable.draw(canvas);
66-
mBottomDrawable.setBounds(0, canvas.getHeight() - mBottomShadowHeight, canvas.getWidth(), canvas.getHeight());
67-
mBottomDrawable.draw(canvas);
78+
drawShadow(canvas);
6879
}
80+
81+
private void drawShadow(Canvas canvas) {
82+
mShadowTopDrawable.setBounds(0, 0, canvas.getWidth(), mShadowTopHeight);
83+
mShadowTopDrawable.draw(canvas);
84+
mShadowBottomDrawable.setBounds(0, canvas.getHeight() - mShadowBottomHeight, canvas.getWidth(), canvas.getHeight());
85+
mShadowBottomDrawable.draw(canvas);
86+
mShadowLeftDrawable.setBounds(0, 0, mShadowLeftHeight, canvas.getHeight());
87+
mShadowLeftDrawable.draw(canvas);
88+
mShadowRightDrawable.setBounds(canvas.getWidth() - mShadowRightHeight, 0, canvas.getWidth(), canvas.getHeight());
89+
mShadowRightDrawable.draw(canvas);
90+
}
91+
92+
public void setShadowsHeight(int left, int top, int right, int bottom) {
93+
mShadowLeftHeight = left;
94+
mShadowTopHeight = top;
95+
mShadowRightHeight = right;
96+
mShadowBottomHeight = bottom;
97+
invalidate();
98+
}
99+
69100
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape android:shape="rectangle"
3+
xmlns:android="http://schemas.android.com/apk/res/android">
4+
5+
<gradient
6+
android:endColor="@android:color/transparent"
7+
android:startColor="@color/shadow_color"/>
8+
</shape>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape android:shape="rectangle"
3+
xmlns:android="http://schemas.android.com/apk/res/android">
4+
5+
<gradient
6+
android:angle="180"
7+
android:endColor="@android:color/transparent"
8+
android:startColor="@color/shadow_color"/>
9+
</shape>

shadowframelayout/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<declare-styleable name="ShadowFrameLayout">
55
<attr name="shadowTopHeight" format="dimension"/>
66
<attr name="shadowBottomHeight" format="dimension"/>
7+
<attr name="shadowLeftHeight" format="dimension"/>
8+
<attr name="shadowRightHeight" format="dimension"/>
79
</declare-styleable>
810

911
</resources>

0 commit comments

Comments
 (0)