A library for seamlessly integrating Material shadows. The library takes existing material shadows to next level by adding the following features :
- Convex shadows : The shadows are not only rectangular or circular, they can take any convex shape depending on the view and its content.
- Support for shadow offsets : The library allows developers to set X and Y offset for the shadows.
- Support for shadow intensity : The library also has support for setting shadow intensity via
shadowAlpha
attribute. - Shadows for semi-transparent views : The library allows shadows for semi-transparent views.
Just add the following dependency in your app's build.gradle
dependencies {
compile 'com.sdsmdg.harjot:materialshadows:1.0.0'
}
The MaterialShadowViewWrapper
is an extension of Relative Layout
. All the child views go through the same process of generating shadow as given below :
- First a bitmap is generated from the drawing cache of the view.
- The bitmap is traversed pixel by pixel to remove all transparent pixels and get a list of points corresponding to the actual outline of the content of the view.
- Since the points corresponding to outline may give a concave path, hence GrahamScan algorithm is used to generate a convex hull of the outline points.
- A path is created from the points of the resulting convex hull.
- This path is passed to a
CustomViewOutlineProvider
object that is later attached to the view itself. - Hence we get a convex shadow for any type of view based on its content.
<com.sdsmdg.harjot.materialshadows.MaterialShadowViewWrapper
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:elevation="5dp"
android:src="@drawable/poly" />
</com.sdsmdg.harjot.materialshadows.MaterialShadowViewWrapper>
<com.sdsmdg.harjot.materialshadows.MaterialShadowViewWrapper
android:layout_width="match_parent"
android:layout_height="match_parent"
app:shadowOffsetX="-15"
app:shadowOffsetY="30">
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:elevation="10dp"
android:src="@drawable/poly" />
</com.sdsmdg.harjot.materialshadows.MaterialShadowViewWrapper>
<com.sdsmdg.harjot.materialshadows.MaterialShadowViewWrapper
android:layout_width="match_parent"
android:layout_height="match_parent"
app:shadowOffsetX="-15"
app:shadowOffsetY="30"
app:shadowAlpha="0.9">
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:elevation="10dp"
android:src="@drawable/poly" />
</com.sdsmdg.harjot.materialshadows.MaterialShadowViewWrapper>
<com.sdsmdg.harjot.materialshadows.MaterialShadowViewWrapper
android:layout_width="match_parent"
android:layout_height="match_parent"
app:shadowOffsetX="-30"
app:shadowOffsetY="30">
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:elevation="10dp"
android:background="#55000000" />
</com.sdsmdg.harjot.materialshadows.MaterialShadowViewWrapper>
XML attribute | Java set methods | Description | Default Value |
---|---|---|---|
shadowOffsetX | setOffsetX(...) | Set the X-offset of the shadow | 0.0f |
shadowOffsetY | setOffsetX(...) | Set the Y-offset of the shadow | 0.0f |
shadowAlpha | setShadowAlpha(...) | Set the value of shadow intensity (alpha) | 1.0f |
- Since the bitmap is traversed pixel by pixel, the performance for large views is bad. Hence the use of the library is limited to small views.
- Currently the shadow is generated only for direct children of the
MaterialShadowViewWrapper
. Hence if the desired views are placed inside a Linear Layout or some other view group, then each view must be wrapped by a seperateMaterialShadowViewWrapper
. This doesn't affect the performance as the number of operations are still the same, but affects the quality of code. - Each child of
MaterialShadowViewWrapper
is assigned same offset and shadow intensity. If fine control over every view's shadow is required then it must be wrapped inside its ownMaterialShadowViewWrapper
. Again this doesn't affect the performance, just the quality of code.
MaterialShadows is licensed under MIT license
. View license.