百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

MaterialShadows

> 编程语言
开源

Material Shadows for android: 用于支持凸面材质阴影的库

2.2K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Material Shadows for android: 用于支持凸面材质阴影的库

# MaterialShadows 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. - Support for Async Shadow calculations : The library allows the operations to be async to avoid blocking the UI thread for long calculations. - Shadow animations : The library supports fade out animation for shadow. # Usage Just add the following dependency in your app's `build.gradle` ```groovy dependencies { implementation 'com.sdsmdg.harjot:materialshadows:1.2.5' } ``` # Third Party Bindings [React Native](https://github.com/facebook/react-native)
You may now use this library with `react-native` via the module [here](https://github.com/prscX/react-native-material-shadows), built by [Pranav Raj Singh Chauhan](https://github.com/prscX). # How does this work ? The `MaterialShadowViewWrapper` is an extension of `Relative Layout`. The `MaterialShadowFrameLayoutWrapper` is an extension of `FrameLayout`. Use any one of them as per your convenience. All the child views go through the same process of generating shadow as given below : 1. First a bitmap is generated from the drawing cache of the view. 2. 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. 3. 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. 4. A path is created from the points of the resulting convex hull. 5. This path is passed to a `CustomViewOutlineProvider` object that is later attached to the view itself. 6. Hence we get a convex shadow for any type of view based on its content. P.S. : All the calculations related to graham scan are done asynchronously by default. This behavior can be controlled by `calculateAsync` parameter. (Thanks [Yaroslav!](https://github.com/yarolegovich)) # Example Usage 1 (Simple) #### XML ```xml ``` #### Result # Example Usage 2 (Offset) #### XML ```xml ``` #### Result # Example Usage 3 (Shadow intensity) #### XML ```xml ``` #### Result # Example Usage 4 (Semi-transparent views) #### XML ```xml ``` #### Result # Using MaterialShadows with custom ViewGroups Since the `ShadowGenerator.java` encapsulates all the code related to the generation of convex shadows, it is really easy to plug in convex shadows with any custom ViewGroup or some platform ViewGroup like `LinearLayout` etc. ``` … ``` Note : Make sure to define all the 7 parameters required for `ShadowGenerator`, namely `offsetX`, `offsetY`, `shadowAlpha`, `shouldShowWhenAllReady`, `shouldCalculateAsync`, `shouldAnimateShadow`, `animationDuration` in the custom wrapper and handle changes in their values. In case any parameter value changes, say `OffsetX`, add the following code inside the setter method for `OffsetX` : ```java public void setOffsetX(float offsetX) { this.offsetX = offsetX; if (shadowGenerator != null) { shadowGenerator.setOffsetX(offsetX); } } ``` If you want to add custom XML attributes with your `CustomShadowWrapper` class, add this to `attrs.xml` ([here](/materialshadows/src/main/res/values/attrs.xml)) and handle them in your `CustomShadowWrapper` class accordingly. ```xml ``` See [MaterialShadowViewWrapper](/materialshadows/src/main/java/com/sdsmdg/harjot/materialshadows/MaterialShadowViewWrapper.java) for more details. # Documentation |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 | |calculateAsync |setShouldCalculateAsync(...) |Set the flag for async shadow calculations. |true | |showWhenAllReady |setShowShadowsWhenAllReady(...)|Set the flag for showing all shadows after all calculations are over|true| |animateShadow |setShouldAnimateShadow(...) |Set the flag for shadow animation |true | |animationDuration |setAnimationDuration(...) |Set the value of shadow animation duration.|300ms | # Limitations 1. 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. 2. 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 seperate `MaterialShadowViewWrapper` or `MaterialShadowFrameLayoutWrapper` or a custom view group wrapper may be implemented. 3. Each child of `MaterialShadowViewWrapper` or custom view group wrapper is assigned the same offset and shadow intensity. If fine control over every view's shadow is required then it must be wrapped inside its own `MaterialShadowViewWrapper` or `MaterialShadowFrameLayoutWrapper`. # Credits 1. [Yaroslav](https://github.com/yarolegovich) : Implementation of asynchronous calculations and shadow animations. 2. [Pranav Raj Singh Chauhan](https://github.com/prscX) : Building react-native bridge plugin for the library. # License MaterialShadows is licensed under `MIT license`. View [license](LICENSE.md).

GitHub Issues· 0 开放

在 GitHub 查看全部

暂无开放 Issues,或尚未同步最近议题。

核心特点

  • •<b>Convex shadows</b> : The shadows are not only rectangular or circular, they can take any convex shape depending on the view and its content.
  • •<b>Support for shadow offsets</b> : The library allows developers to set <b>X</b> and <b>Y</b> offset for the shadows.
  • •<b>Support for shadow intensity</b> : The library also has support for setting shadow intensity via shadowAlpha attribute.
  • •<b>Shadows for semi-transparent views</b> : The library allows shadows for semi-transparent views.
  • •<b>Support for Async Shadow calculations</b> : The library allows the operations to be async to avoid blocking the UI thread for long calculations.
  • •<b>Shadow animations</b> : The library supports fade out animation for shadow.

> 标签

Javaandroidconvex-hullgraham-scan-algorithmlibrary

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言