├── .gitignore
├── LICENSE.md
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── sdsmdg
│ │ └── harjot
│ │ └── materialshadowsdemo
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── sdsmdg
│ │ │ └── harjot
│ │ │ └── materialshadowsdemo
│ │ │ └── MainActivity.java
│ └── res
│ │ ├── drawable
│ │ ├── airbnb.png
│ │ ├── facebook.png
│ │ ├── google.png
│ │ ├── google_play.png
│ │ ├── playstore.png
│ │ └── poly.png
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ └── ic_launcher.png
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── sdsmdg
│ └── harjot
│ └── materialshadowsdemo
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── materialshadows
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── sdsmdg
│ │ └── harjot
│ │ └── materialshadows
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── sdsmdg
│ │ │ └── harjot
│ │ │ └── materialshadows
│ │ │ ├── Constants.java
│ │ │ ├── MaterialShadowFrameLayoutWrapper.java
│ │ │ ├── MaterialShadowViewWrapper.java
│ │ │ ├── outlineprovider
│ │ │ └── CustomViewOutlineProvider.java
│ │ │ ├── shadowutils
│ │ │ └── ShadowGenerator.java
│ │ │ └── utilities
│ │ │ ├── GrahamScan.java
│ │ │ └── Point2D.java
│ └── res
│ │ └── values
│ │ ├── attrs.xml
│ │ └── strings.xml
│ └── test
│ └── java
│ └── com
│ └── sdsmdg
│ └── harjot
│ └── materialshadows
│ └── ExampleUnitTest.java
├── screens
├── cover.png
├── example_1.png
├── example_2.png
├── example_3.png
└── example_4.png
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | ### Android ###
3 | # Built application files
4 | *.apk
5 | *.ap_
6 |
7 | # Files for the ART/Dalvik VM
8 | *.dex
9 |
10 | # Java class files
11 | *.class
12 |
13 | # Generated files
14 | bin/
15 | gen/
16 | out/
17 |
18 | # Gradle files
19 | .gradle/
20 | build/
21 |
22 | # Local configuration file (sdk path, etc)
23 | local.properties
24 |
25 | # Proguard folder generated by Eclipse
26 | proguard/
27 |
28 | # Log Files
29 | *.log
30 |
31 | # Android Studio Navigation editor temp files
32 | .navigation/
33 |
34 | # Android Studio captures folder
35 | captures/
36 |
37 | # Intellij
38 | *.iml
39 | .idea/
40 |
41 | # Keystore files
42 | *.jks
43 |
44 | # External native build folder generated in Android Studio 2.2 and later
45 | .externalNativeBuild
46 |
47 | # Google Services (e.g. APIs or Firebase)
48 | google-services.json
49 |
50 | ### Android Patch ###
51 | gen-external-apklibs
52 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | Copyright 2016-17 Harjot Singh Oberai
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |

2 |
3 | # MaterialShadows
4 | [](https://www.android.com)
5 | [](https://android-arsenal.com/api?level=21)
6 | [](https://opensource.org/licenses/MIT)
7 |
8 | A library for seamlessly integrating Material shadows.
9 | The library takes existing material shadows to next level by adding the following features :
10 |
11 | - Convex shadows : The shadows are not only rectangular or circular, they can take any convex shape depending on the view and its content.
12 | - Support for shadow offsets : The library allows developers to set X and Y offset for the shadows.
13 | - Support for shadow intensity : The library also has support for setting shadow intensity via `shadowAlpha` attribute.
14 | - Shadows for semi-transparent views : The library allows shadows for semi-transparent views.
15 | - Support for Async Shadow calculations : The library allows the operations to be async to avoid blocking the UI thread for long calculations.
16 | - Shadow animations : The library supports fade out animation for shadow.
17 |
18 | # Usage
19 | Just add the following dependency in your app's `build.gradle`
20 | ```groovy
21 | dependencies {
22 | implementation 'com.sdsmdg.harjot:materialshadows:1.2.5'
23 | }
24 | ```
25 |
26 | # Third Party Bindings
27 | [React Native](https://github.com/facebook/react-native)
28 | 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).
29 |
30 | # How does this work ?
31 | The `MaterialShadowViewWrapper` is an extension of `Relative Layout`. The `MaterialShadowFrameLayoutWrapper` is an extension of `FrameLayout`. Use any one of them as per your convenience.
32 |
33 | All the child views go through the same process of generating shadow as given below :
34 | 1. First a bitmap is generated from the drawing cache of the view.
35 | 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.
36 | 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.
37 | 4. A path is created from the points of the resulting convex hull.
38 | 5. This path is passed to a `CustomViewOutlineProvider` object that is later attached to the view itself.
39 | 6. Hence we get a convex shadow for any type of view based on its content.
40 |
41 | 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))
42 |
43 | # Example Usage 1 (Simple)
44 | #### XML
45 | ```xml
46 |
49 |
50 |
55 |
56 |
57 | ```
58 | #### Result
59 |
60 |
61 | # Example Usage 2 (Offset)
62 | #### XML
63 | ```xml
64 |
69 |
70 |
75 |
76 |
77 | ```
78 | #### Result
79 |
80 |
81 | # Example Usage 3 (Shadow intensity)
82 | #### XML
83 | ```xml
84 |
90 |
91 |
96 |
97 |
98 | ```
99 | #### Result
100 |
101 |
102 | # Example Usage 4 (Semi-transparent views)
103 | #### XML
104 | ```xml
105 |
110 |
111 |
116 |
117 |
118 | ```
119 | #### Result
120 |
121 |
122 | # Using MaterialShadows with custom ViewGroups
123 | 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.
124 |
125 | ```java
126 | public class CustomShadowWrapper extends CustomViewGroup {
127 |
128 | ShadowGenerator shadowGenerator;
129 |
130 | @Override
131 | protected void onLayout(boolean changed, int l, int t, int r, int b) {
132 | super.onLayout(changed, l, t, r, b);
133 | if (shadowGenerator == null) {
134 | shadowGenerator = new ShadowGenerator(this, offsetX, offsetY, shadowAlpha, shouldShowWhenAllReady, shouldCalculateAsync, shouldAnimateShadow, animationDuration);
135 | }
136 | shadowGenerator.generate();
137 | }
138 |
139 | @Override
140 | protected void onDetachedFromWindow() {
141 | super.onDetachedFromWindow();
142 | if (shadowGenerator != null) {
143 | shadowGenerator.releaseResources();
144 | }
145 | }
146 |
147 | }
148 | ```
149 |
150 | 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.
151 |
152 | In case any parameter value changes, say `OffsetX`, add the following code inside the setter method for `OffsetX` :
153 | ```java
154 | public void setOffsetX(float offsetX) {
155 | this.offsetX = offsetX;
156 | if (shadowGenerator != null) {
157 | shadowGenerator.setOffsetX(offsetX);
158 | }
159 | }
160 | ```
161 |
162 | 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.
163 | ```xml
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 | ```
175 |
176 | See [MaterialShadowViewWrapper](/materialshadows/src/main/java/com/sdsmdg/harjot/materialshadows/MaterialShadowViewWrapper.java) for more details.
177 |
178 | # Documentation
179 | |XML attribute |Java set methods |Description | Default Value |
180 | |----------------------|------------------------------|----------------------------------------|-------------------|
181 | |shadowOffsetX |setOffsetX(...) |Set the X-offset of the shadow |0.0f |
182 | |shadowOffsetY |setOffsetX(...) |Set the Y-offset of the shadow |0.0f |
183 | |shadowAlpha |setShadowAlpha(...) |Set the value of shadow intensity (alpha)|1.0f |
184 | |calculateAsync |setShouldCalculateAsync(...) |Set the flag for async shadow calculations. |true |
185 | |showWhenAllReady |setShowShadowsWhenAllReady(...)|Set the flag for showing all shadows after all calculations are over|true|
186 | |animateShadow |setShouldAnimateShadow(...) |Set the flag for shadow animation |true |
187 | |animationDuration |setAnimationDuration(...) |Set the value of shadow animation duration.|300ms |
188 |
189 | # Limitations
190 | 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.
191 | 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.
192 | 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`.
193 |
194 | # Credits
195 | 1. [Yaroslav](https://github.com/yarolegovich) : Implementation of asynchronous calculations and shadow animations.
196 | 2. [Pranav Raj Singh Chauhan](https://github.com/prscX) : Building react-native bridge plugin for the library.
197 |
198 |
199 | # License
200 | MaterialShadows is licensed under `MIT license`. View [license](LICENSE.md).
201 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion '26.0.2'
6 | defaultConfig {
7 | applicationId "com.sdsmdg.harjot.materialshadowsdemo"
8 | minSdkVersion 21
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | compile 'com.android.support:appcompat-v7:25.3.0'
25 | compile project(":materialshadows")
26 | }
27 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\Harjot\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/sdsmdg/harjot/materialshadowsdemo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.sdsmdg.harjot.materialshadowsdemo;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.sdsmdg.harjot.materialshadowsdemo", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sdsmdg/harjot/materialshadowsdemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.sdsmdg.harjot.materialshadowsdemo;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.widget.CheckBox;
7 |
8 | import com.sdsmdg.harjot.materialshadows.MaterialShadowViewWrapper;
9 |
10 | public class MainActivity extends AppCompatActivity implements View.OnClickListener {
11 |
12 | private CheckBox calculateAsync;
13 | private CheckBox showWhenAllReady;
14 |
15 | private MaterialShadowViewWrapper shadowViewWrapper;
16 |
17 | @Override
18 | protected void onCreate(Bundle savedInstanceState) {
19 | super.onCreate(savedInstanceState);
20 | setContentView(R.layout.activity_main);
21 |
22 | shadowViewWrapper = (MaterialShadowViewWrapper) findViewById(R.id.shadow_wrapper);
23 | calculateAsync = (CheckBox) findViewById(R.id.cb_calculate_async);
24 | showWhenAllReady = (CheckBox) findViewById(R.id.cb_show_when_all_ready);
25 |
26 | findViewById(R.id.btn_re_render).setOnClickListener(this);
27 | }
28 |
29 | @Override
30 | public void onClick(View v) {
31 | shadowViewWrapper.setShouldCalculateAsync(calculateAsync.isChecked());
32 | shadowViewWrapper.setShowShadowsWhenAllReady(showWhenAllReady.isChecked());
33 | shadowViewWrapper.requestLayout();
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/airbnb.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/app/src/main/res/drawable/airbnb.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/facebook.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/app/src/main/res/drawable/facebook.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/google.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/app/src/main/res/drawable/google.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/google_play.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/app/src/main/res/drawable/google_play.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/playstore.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/app/src/main/res/drawable/playstore.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/poly.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/app/src/main/res/drawable/poly.png
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
18 |
19 |
27 |
28 |
36 |
37 |
45 |
46 |
54 |
55 |
56 |
57 |
63 |
64 |
70 |
71 |
76 |
77 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | MaterialShadowsDemo
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/test/java/com/sdsmdg/harjot/materialshadowsdemo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.sdsmdg.harjot.materialshadowsdemo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:3.0.0'
9 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
10 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
11 |
12 | // NOTE: Do not place your application dependencies here; they belong
13 | // in the individual module build.gradle files
14 | }
15 | }
16 |
17 | allprojects {
18 | repositories {
19 | jcenter()
20 | }
21 | }
22 |
23 | task clean(type: Delete) {
24 | delete rootProject.buildDir
25 | }
26 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Jan 27 18:11:45 IST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/materialshadows/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/materialshadows/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | ext {
4 | bintrayRepo = 'MaterialShadows'
5 | bintrayName = 'materialshadows'
6 |
7 | publishedGroupId = 'com.sdsmdg.harjot'
8 | libraryName = 'MaterialShadows'
9 | artifact = 'materialshadows'
10 |
11 | libraryDescription = 'A library for integrating Material shadows seamlessly'
12 |
13 | siteUrl = 'https://github.com/harjot-oberai/MaterialShadows/'
14 | gitUrl = 'https://github.com/harjot-oberai/MaterialShadows/.git'
15 |
16 | libraryVersion = '1.2.5'
17 |
18 | developerId = 'harjot-oberai'
19 | developerName = 'Harjot Singh Oberai'
20 | developerEmail = 'harjot.oberai@gmail.com'
21 |
22 | licenseName = 'The MIT License'
23 | licenseUrl = 'https://opensource.org/licenses/MIT'
24 | allLicenses = ["MIT"]
25 | }
26 |
27 | android {
28 | compileSdkVersion 25
29 | buildToolsVersion '26.0.2'
30 |
31 | defaultConfig {
32 | minSdkVersion 21
33 | targetSdkVersion 25
34 | versionCode 1
35 | versionName "1.0"
36 |
37 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
38 |
39 | }
40 | buildTypes {
41 | release {
42 | minifyEnabled false
43 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
44 | }
45 | }
46 | }
47 |
48 | dependencies {
49 | compile fileTree(dir: 'libs', include: ['*.jar'])
50 | compile 'com.android.support:appcompat-v7:25.3.0'
51 | }
52 |
53 | apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
54 | apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
55 |
--------------------------------------------------------------------------------
/materialshadows/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\Harjot\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/materialshadows/src/androidTest/java/com/sdsmdg/harjot/materialshadows/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.sdsmdg.harjot.materialshadows;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.sdsmdg.harjot.materialshadows.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/materialshadows/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/materialshadows/src/main/java/com/sdsmdg/harjot/materialshadows/Constants.java:
--------------------------------------------------------------------------------
1 | package com.sdsmdg.harjot.materialshadows;
2 |
3 | /**
4 | * Created by Harjot on 01-Jun-17.
5 | */
6 |
7 | public class Constants {
8 |
9 | public static final float DEFAULT_X_OFFSET = 0.0f;
10 | public static final float DEFAULT_Y_OFFSET = 0.0f;
11 | public static final float DEFAULT_SHADOW_ALPHA = 0.99f;
12 | public static final boolean DEFAULT_SHOW_WHEN_ALL_READY = true;
13 | public static final boolean DEFAULT_CALCULATE_ASYNC = true;
14 | public static final boolean DEFAULT_ANIMATE_SHADOW = true;
15 | public static final int DEFAULT_ANIMATION_TIME = 300;
16 |
17 | public static final int POS_UPDATE_ALL = -1;
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/materialshadows/src/main/java/com/sdsmdg/harjot/materialshadows/MaterialShadowFrameLayoutWrapper.java:
--------------------------------------------------------------------------------
1 | package com.sdsmdg.harjot.materialshadows;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.util.AttributeSet;
6 | import android.widget.FrameLayout;
7 |
8 | import com.sdsmdg.harjot.materialshadows.shadowutils.ShadowGenerator;
9 |
10 | import static com.sdsmdg.harjot.materialshadows.Constants.DEFAULT_ANIMATE_SHADOW;
11 | import static com.sdsmdg.harjot.materialshadows.Constants.DEFAULT_ANIMATION_TIME;
12 | import static com.sdsmdg.harjot.materialshadows.Constants.DEFAULT_CALCULATE_ASYNC;
13 | import static com.sdsmdg.harjot.materialshadows.Constants.DEFAULT_SHADOW_ALPHA;
14 | import static com.sdsmdg.harjot.materialshadows.Constants.DEFAULT_SHOW_WHEN_ALL_READY;
15 | import static com.sdsmdg.harjot.materialshadows.Constants.DEFAULT_X_OFFSET;
16 | import static com.sdsmdg.harjot.materialshadows.Constants.DEFAULT_Y_OFFSET;
17 |
18 | public class MaterialShadowFrameLayoutWrapper extends FrameLayout {
19 |
20 | float offsetX = DEFAULT_X_OFFSET;
21 | float offsetY = DEFAULT_Y_OFFSET;
22 |
23 | float shadowAlpha = DEFAULT_SHADOW_ALPHA;
24 |
25 | boolean shouldShowWhenAllReady = DEFAULT_SHOW_WHEN_ALL_READY;
26 | boolean shouldCalculateAsync = DEFAULT_CALCULATE_ASYNC;
27 | boolean shouldAnimateShadow = DEFAULT_ANIMATE_SHADOW;
28 |
29 | int animationDuration = DEFAULT_ANIMATION_TIME;
30 |
31 | ShadowGenerator shadowGenerator;
32 |
33 | public MaterialShadowFrameLayoutWrapper(Context context) {
34 | super(context);
35 | }
36 |
37 | public MaterialShadowFrameLayoutWrapper(Context context, AttributeSet attrs) {
38 | super(context, attrs);
39 | initXMLAttrs(context, attrs);
40 | }
41 |
42 | public MaterialShadowFrameLayoutWrapper(Context context, AttributeSet attrs, int defStyleAttr) {
43 | super(context, attrs, defStyleAttr);
44 | initXMLAttrs(context, attrs);
45 | }
46 |
47 | void initXMLAttrs(Context context, AttributeSet attrs) {
48 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MaterialShadowFrameLayoutWrapper);
49 | final int N = a.getIndexCount();
50 | for (int i = 0; i < N; ++i) {
51 | int attr = a.getIndex(i);
52 | if (attr == R.styleable.MaterialShadowFrameLayoutWrapper_shadowAlpha) {
53 | shadowAlpha = a.getFloat(attr, DEFAULT_SHADOW_ALPHA);
54 | } else if (attr == R.styleable.MaterialShadowFrameLayoutWrapper_shadowOffsetX) {
55 | offsetX = a.getFloat(attr, DEFAULT_X_OFFSET);
56 | } else if (attr == R.styleable.MaterialShadowFrameLayoutWrapper_shadowOffsetY) {
57 | offsetY = a.getFloat(attr, DEFAULT_Y_OFFSET);
58 | } else if (attr == R.styleable.MaterialShadowFrameLayoutWrapper_calculateAsync) {
59 | shouldCalculateAsync = a.getBoolean(attr, DEFAULT_CALCULATE_ASYNC);
60 | } else if (attr == R.styleable.MaterialShadowFrameLayoutWrapper_animateShadow) {
61 | shouldAnimateShadow = a.getBoolean(attr, DEFAULT_ANIMATE_SHADOW);
62 | } else if (attr == R.styleable.MaterialShadowFrameLayoutWrapper_showWhenAllReady) {
63 | shouldShowWhenAllReady = a.getBoolean(attr, DEFAULT_SHOW_WHEN_ALL_READY);
64 | } else if (attr == R.styleable.MaterialShadowFrameLayoutWrapper_animationDuration) {
65 | animationDuration = a.getInteger(attr, DEFAULT_ANIMATION_TIME);
66 | }
67 | }
68 | a.recycle();
69 | }
70 |
71 | @Override
72 | protected void onLayout(boolean changed, int l, int t, int r, int b) {
73 | super.onLayout(changed, l, t, r, b);
74 | if (shadowGenerator == null) {
75 | shadowGenerator = new ShadowGenerator(this, offsetX, offsetY, shadowAlpha, shouldShowWhenAllReady, shouldCalculateAsync, shouldAnimateShadow, animationDuration);
76 | }
77 | shadowGenerator.generate();
78 | }
79 |
80 | @Override
81 | protected void onDetachedFromWindow() {
82 | super.onDetachedFromWindow();
83 | if (shadowGenerator != null) {
84 | shadowGenerator.releaseResources();
85 | }
86 | }
87 |
88 | public float getOffsetX() {
89 | return offsetX;
90 | }
91 |
92 | public void setOffsetX(float offsetX) {
93 | this.offsetX = offsetX;
94 | if (shadowGenerator != null) {
95 | shadowGenerator.setOffsetX(offsetX);
96 | }
97 | }
98 |
99 | public float getOffsetY() {
100 | return offsetY;
101 | }
102 |
103 | public void setOffsetY(float offsetY) {
104 | this.offsetY = offsetY;
105 | if (shadowGenerator != null) {
106 | shadowGenerator.setOffsetY(offsetY);
107 | }
108 | }
109 |
110 | public float getShadowAlpha() {
111 | return shadowAlpha;
112 | }
113 |
114 | public void setShadowAlpha(float shadowAlpha) {
115 | this.shadowAlpha = shadowAlpha;
116 | if (shadowGenerator != null) {
117 | shadowGenerator.setShadowAlpha(shadowAlpha);
118 | }
119 | }
120 |
121 | public void setShouldAnimateShadow(boolean shouldAnimateShadow) {
122 | this.shouldAnimateShadow = shouldAnimateShadow;
123 | if (shadowGenerator != null) {
124 | shadowGenerator.setShouldAnimateShadow(shouldAnimateShadow);
125 | }
126 | }
127 |
128 | public boolean shouldAnimateShadow() {
129 | return shouldAnimateShadow;
130 | }
131 |
132 | public void setShouldCalculateAsync(boolean shouldCalculateAsync) {
133 | this.shouldCalculateAsync = shouldCalculateAsync;
134 | if (shadowGenerator != null) {
135 | shadowGenerator.setShouldCalculateAsync(shouldCalculateAsync);
136 | }
137 | }
138 |
139 | public boolean shouldCalculateAsync() {
140 | return shouldCalculateAsync;
141 | }
142 |
143 | public void setShowShadowsWhenAllReady(boolean shouldWaitForAllReady) {
144 | this.shouldShowWhenAllReady = shouldWaitForAllReady;
145 | if (shadowGenerator != null) {
146 | shadowGenerator.setShouldShowWhenAllReady(shouldWaitForAllReady);
147 | }
148 | }
149 |
150 | public boolean shouldWaitForAllReady() {
151 | return shouldShowWhenAllReady;
152 | }
153 |
154 | public int getAnimationDuration() {
155 | return animationDuration;
156 | }
157 |
158 | public void setAnimationDuration(int animationDuration) {
159 | this.animationDuration = animationDuration;
160 | if (shadowGenerator != null) {
161 | shadowGenerator.setAnimationDuration(animationDuration);
162 | }
163 | }
164 |
165 | }
166 |
--------------------------------------------------------------------------------
/materialshadows/src/main/java/com/sdsmdg/harjot/materialshadows/MaterialShadowViewWrapper.java:
--------------------------------------------------------------------------------
1 | package com.sdsmdg.harjot.materialshadows;
2 |
3 | import android.animation.ObjectAnimator;
4 | import android.animation.ValueAnimator;
5 | import android.content.Context;
6 | import android.content.res.TypedArray;
7 | import android.graphics.Bitmap;
8 | import android.graphics.Color;
9 | import android.graphics.Path;
10 | import android.os.Handler;
11 | import android.os.Looper;
12 | import android.util.AttributeSet;
13 | import android.util.Log;
14 | import android.util.SparseArray;
15 | import android.view.View;
16 | import android.widget.RelativeLayout;
17 |
18 | import com.sdsmdg.harjot.materialshadows.outlineprovider.CustomViewOutlineProvider;
19 | import com.sdsmdg.harjot.materialshadows.shadowutils.ShadowGenerator;
20 | import com.sdsmdg.harjot.materialshadows.utilities.GrahamScan;
21 | import com.sdsmdg.harjot.materialshadows.utilities.Point2D;
22 |
23 | import java.util.ArrayList;
24 | import java.util.Collections;
25 | import java.util.List;
26 | import java.util.concurrent.ExecutorService;
27 | import java.util.concurrent.Executors;
28 | import java.util.concurrent.Future;
29 |
30 | import static com.sdsmdg.harjot.materialshadows.Constants.*;
31 |
32 | public class MaterialShadowViewWrapper extends RelativeLayout {
33 |
34 | float offsetX = DEFAULT_X_OFFSET;
35 | float offsetY = DEFAULT_Y_OFFSET;
36 |
37 | float shadowAlpha = DEFAULT_SHADOW_ALPHA;
38 |
39 | boolean shouldShowWhenAllReady = DEFAULT_SHOW_WHEN_ALL_READY;
40 | boolean shouldCalculateAsync = DEFAULT_CALCULATE_ASYNC;
41 | boolean shouldAnimateShadow = DEFAULT_ANIMATE_SHADOW;
42 |
43 | int animationDuration = DEFAULT_ANIMATION_TIME;
44 |
45 | ShadowGenerator shadowGenerator;
46 |
47 | public MaterialShadowViewWrapper(Context context) {
48 | super(context);
49 | }
50 |
51 | public MaterialShadowViewWrapper(Context context, AttributeSet attrs) {
52 | super(context, attrs);
53 | initXMLAttrs(context, attrs);
54 | }
55 |
56 | public MaterialShadowViewWrapper(Context context, AttributeSet attrs, int defStyleAttr) {
57 | super(context, attrs, defStyleAttr);
58 | initXMLAttrs(context, attrs);
59 | }
60 |
61 | void initXMLAttrs(Context context, AttributeSet attrs) {
62 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MaterialShadowViewWrapper);
63 | final int N = a.getIndexCount();
64 | for (int i = 0; i < N; ++i) {
65 | int attr = a.getIndex(i);
66 | if (attr == R.styleable.MaterialShadowViewWrapper_shadowAlpha) {
67 | shadowAlpha = a.getFloat(attr, DEFAULT_SHADOW_ALPHA);
68 | } else if (attr == R.styleable.MaterialShadowViewWrapper_shadowOffsetX) {
69 | offsetX = a.getFloat(attr, DEFAULT_X_OFFSET);
70 | } else if (attr == R.styleable.MaterialShadowViewWrapper_shadowOffsetY) {
71 | offsetY = a.getFloat(attr, DEFAULT_Y_OFFSET);
72 | } else if (attr == R.styleable.MaterialShadowViewWrapper_calculateAsync) {
73 | shouldCalculateAsync = a.getBoolean(attr, DEFAULT_CALCULATE_ASYNC);
74 | } else if (attr == R.styleable.MaterialShadowViewWrapper_animateShadow) {
75 | shouldAnimateShadow = a.getBoolean(attr, DEFAULT_ANIMATE_SHADOW);
76 | } else if (attr == R.styleable.MaterialShadowViewWrapper_showWhenAllReady) {
77 | shouldShowWhenAllReady = a.getBoolean(attr, DEFAULT_SHOW_WHEN_ALL_READY);
78 | } else if (attr == R.styleable.MaterialShadowViewWrapper_animationDuration) {
79 | animationDuration = a.getInteger(attr, DEFAULT_ANIMATION_TIME);
80 | }
81 | }
82 | a.recycle();
83 | }
84 |
85 | @Override
86 | protected void onLayout(boolean changed, int l, int t, int r, int b) {
87 | super.onLayout(changed, l, t, r, b);
88 | if (shadowGenerator == null) {
89 | shadowGenerator = new ShadowGenerator(this, offsetX, offsetY, shadowAlpha, shouldShowWhenAllReady, shouldCalculateAsync, shouldAnimateShadow, animationDuration);
90 | }
91 | shadowGenerator.generate();
92 | }
93 |
94 | @Override
95 | protected void onDetachedFromWindow() {
96 | super.onDetachedFromWindow();
97 | if (shadowGenerator != null) {
98 | shadowGenerator.releaseResources();
99 | }
100 | }
101 |
102 | public float getOffsetX() {
103 | return offsetX;
104 | }
105 |
106 | public void setOffsetX(float offsetX) {
107 | this.offsetX = offsetX;
108 | if (shadowGenerator != null) {
109 | shadowGenerator.setOffsetX(offsetX);
110 | }
111 | }
112 |
113 | public float getOffsetY() {
114 | return offsetY;
115 | }
116 |
117 | public void setOffsetY(float offsetY) {
118 | this.offsetY = offsetY;
119 | if (shadowGenerator != null) {
120 | shadowGenerator.setOffsetY(offsetY);
121 | }
122 | }
123 |
124 | public float getShadowAlpha() {
125 | return shadowAlpha;
126 | }
127 |
128 | public void setShadowAlpha(float shadowAlpha) {
129 | this.shadowAlpha = shadowAlpha;
130 | if (shadowGenerator != null) {
131 | shadowGenerator.setShadowAlpha(shadowAlpha);
132 | }
133 | }
134 |
135 | public void setShouldAnimateShadow(boolean shouldAnimateShadow) {
136 | this.shouldAnimateShadow = shouldAnimateShadow;
137 | if (shadowGenerator != null) {
138 | shadowGenerator.setShouldAnimateShadow(shouldAnimateShadow);
139 | }
140 | }
141 |
142 | public boolean shouldAnimateShadow() {
143 | return shouldAnimateShadow;
144 | }
145 |
146 | public void setShouldCalculateAsync(boolean shouldCalculateAsync) {
147 | this.shouldCalculateAsync = shouldCalculateAsync;
148 | if (shadowGenerator != null) {
149 | shadowGenerator.setShouldCalculateAsync(shouldCalculateAsync);
150 | }
151 | }
152 |
153 | public boolean shouldCalculateAsync() {
154 | return shouldCalculateAsync;
155 | }
156 |
157 | public void setShowShadowsWhenAllReady(boolean shouldWaitForAllReady) {
158 | this.shouldShowWhenAllReady = shouldWaitForAllReady;
159 | if (shadowGenerator != null) {
160 | shadowGenerator.setShouldShowWhenAllReady(shouldWaitForAllReady);
161 | }
162 | }
163 |
164 | public boolean shouldWaitForAllReady() {
165 | return shouldShowWhenAllReady;
166 | }
167 |
168 | public int getAnimationDuration() {
169 | return animationDuration;
170 | }
171 |
172 | public void setAnimationDuration(int animationDuration) {
173 | this.animationDuration = animationDuration;
174 | if (shadowGenerator != null) {
175 | shadowGenerator.setAnimationDuration(animationDuration);
176 | }
177 | }
178 | }
179 |
--------------------------------------------------------------------------------
/materialshadows/src/main/java/com/sdsmdg/harjot/materialshadows/outlineprovider/CustomViewOutlineProvider.java:
--------------------------------------------------------------------------------
1 | package com.sdsmdg.harjot.materialshadows.outlineprovider;
2 |
3 | import android.animation.ObjectAnimator;
4 | import android.animation.ValueAnimator;
5 | import android.graphics.Outline;
6 | import android.graphics.Path;
7 | import android.view.View;
8 | import android.view.ViewOutlineProvider;
9 |
10 | public class CustomViewOutlineProvider extends ViewOutlineProvider {
11 |
12 | private Path path;
13 | private float alpha;
14 |
15 | public CustomViewOutlineProvider(Path path, float alpha) {
16 | this.path = path;
17 | this.alpha = alpha;
18 | }
19 |
20 | @Override
21 | public void getOutline(View view, Outline outline) {
22 | outline.setConvexPath(path);
23 | if (alpha >= 1.0f) {
24 | alpha = 0.99f;
25 | } else if (alpha < 0.0f) {
26 | alpha = 0.0f;
27 | }
28 | outline.setAlpha(alpha);
29 | }
30 |
31 | public float getAlpha() {
32 | return alpha;
33 | }
34 |
35 | public void setAlpha(float alpha) {
36 | this.alpha = alpha;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/materialshadows/src/main/java/com/sdsmdg/harjot/materialshadows/shadowutils/ShadowGenerator.java:
--------------------------------------------------------------------------------
1 | package com.sdsmdg.harjot.materialshadows.shadowutils;
2 |
3 | import android.animation.ObjectAnimator;
4 | import android.animation.ValueAnimator;
5 | import android.graphics.Bitmap;
6 | import android.graphics.Color;
7 | import android.graphics.Path;
8 | import android.os.Handler;
9 | import android.os.Looper;
10 | import android.util.SparseArray;
11 | import android.view.View;
12 | import android.view.ViewGroup;
13 |
14 | import com.sdsmdg.harjot.materialshadows.MaterialShadowViewWrapper;
15 | import com.sdsmdg.harjot.materialshadows.outlineprovider.CustomViewOutlineProvider;
16 | import com.sdsmdg.harjot.materialshadows.utilities.GrahamScan;
17 | import com.sdsmdg.harjot.materialshadows.utilities.Point2D;
18 |
19 | import java.util.ArrayList;
20 | import java.util.Collections;
21 | import java.util.List;
22 | import java.util.concurrent.ExecutorService;
23 | import java.util.concurrent.Executors;
24 | import java.util.concurrent.Future;
25 |
26 | import static com.sdsmdg.harjot.materialshadows.Constants.DEFAULT_ANIMATE_SHADOW;
27 | import static com.sdsmdg.harjot.materialshadows.Constants.DEFAULT_ANIMATION_TIME;
28 | import static com.sdsmdg.harjot.materialshadows.Constants.DEFAULT_CALCULATE_ASYNC;
29 | import static com.sdsmdg.harjot.materialshadows.Constants.DEFAULT_SHADOW_ALPHA;
30 | import static com.sdsmdg.harjot.materialshadows.Constants.DEFAULT_SHOW_WHEN_ALL_READY;
31 | import static com.sdsmdg.harjot.materialshadows.Constants.DEFAULT_X_OFFSET;
32 | import static com.sdsmdg.harjot.materialshadows.Constants.DEFAULT_Y_OFFSET;
33 | import static com.sdsmdg.harjot.materialshadows.Constants.POS_UPDATE_ALL;
34 |
35 | public class ShadowGenerator {
36 |
37 | private final Object TASKS_LOCK = new Object();
38 |
39 | private List> tasksInProgress = new ArrayList<>();
40 | private ExecutorService workerPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
41 | private Handler uiThreadHandler = new Handler(Looper.getMainLooper());
42 |
43 | private SparseArray viewPaths;
44 |
45 | private float offsetX = DEFAULT_X_OFFSET;
46 | private float offsetY = DEFAULT_Y_OFFSET;
47 |
48 | private float shadowAlpha = DEFAULT_SHADOW_ALPHA;
49 |
50 | private boolean shouldShowWhenAllReady = DEFAULT_SHOW_WHEN_ALL_READY;
51 | private boolean shouldCalculateAsync = DEFAULT_CALCULATE_ASYNC;
52 | private boolean shouldAnimateShadow = DEFAULT_ANIMATE_SHADOW;
53 |
54 | private int animationDuration = DEFAULT_ANIMATION_TIME;
55 |
56 | private int childrenWithShadow;
57 |
58 | private ViewGroup viewGroup;
59 |
60 | public ShadowGenerator(ViewGroup viewGroup, float offsetX, float offsetY, float shadowAlpha, boolean shouldShowWhenAllReady, boolean shouldCalculateAsync, boolean shouldAnimateShadow, int animationDuration) {
61 | this.viewGroup = viewGroup;
62 | this.offsetX = offsetX;
63 | this.offsetY = offsetY;
64 | this.shadowAlpha = shadowAlpha;
65 | this.shouldShowWhenAllReady = shouldShowWhenAllReady;
66 | this.shouldCalculateAsync = shouldCalculateAsync;
67 | this.shouldAnimateShadow = shouldAnimateShadow;
68 | this.animationDuration = animationDuration;
69 | }
70 |
71 | public float getOffsetX() {
72 | return offsetX;
73 | }
74 |
75 | public void setOffsetX(float offsetX) {
76 | this.offsetX = offsetX;
77 | updateShadows(POS_UPDATE_ALL);
78 | }
79 |
80 | public float getOffsetY() {
81 | return offsetY;
82 | }
83 |
84 | public void setOffsetY(float offsetY) {
85 | this.offsetY = offsetY;
86 | updateShadows(POS_UPDATE_ALL);
87 | }
88 |
89 | public float getShadowAlpha() {
90 | return shadowAlpha;
91 | }
92 |
93 | public void setShadowAlpha(float shadowAlpha) {
94 | this.shadowAlpha = shadowAlpha;
95 | updateShadows(POS_UPDATE_ALL);
96 | }
97 |
98 | public boolean isShouldShowWhenAllReady() {
99 | return shouldShowWhenAllReady;
100 | }
101 |
102 | public void setShouldShowWhenAllReady(boolean shouldShowWhenAllReady) {
103 | this.shouldShowWhenAllReady = shouldShowWhenAllReady;
104 | }
105 |
106 | public boolean isShouldCalculateAsync() {
107 | return shouldCalculateAsync;
108 | }
109 |
110 | public void setShouldCalculateAsync(boolean shouldCalculateAsync) {
111 | this.shouldCalculateAsync = shouldCalculateAsync;
112 | }
113 |
114 | public boolean isShouldAnimateShadow() {
115 | return shouldAnimateShadow;
116 | }
117 |
118 | public void setShouldAnimateShadow(boolean shouldAnimateShadow) {
119 | this.shouldAnimateShadow = shouldAnimateShadow;
120 | }
121 |
122 | public int getAnimationDuration() {
123 | return animationDuration;
124 | }
125 |
126 | public void setAnimationDuration(int animationDuration) {
127 | this.animationDuration = animationDuration;
128 | }
129 |
130 | public void generate() {
131 | clearShadowCache(); //Maybe some children changed their size
132 | childrenWithShadow = 0;
133 | for (int i = 0; i < viewGroup.getChildCount(); i++) {
134 | View view = viewGroup.getChildAt(i);
135 | if (view instanceof MaterialShadowViewWrapper) {
136 | continue;
137 | }
138 | childrenWithShadow++;
139 | if (shouldCalculateAsync) {
140 | calculateAndRenderShadowAsync(view, i);
141 | } else {
142 | calculateAndRenderShadow(view, i);
143 | }
144 | }
145 | }
146 |
147 | public void releaseResources() {
148 | workerPool.shutdown();
149 | uiThreadHandler.removeCallbacksAndMessages(null);
150 | }
151 |
152 | private void clearShadowCache() {
153 | cancelTasksInProgress();
154 | uiThreadHandler.removeCallbacksAndMessages(null);
155 | viewPaths = new SparseArray<>();
156 | }
157 |
158 | private void updateShadows(int pos) {
159 | if (pos == POS_UPDATE_ALL) {
160 | for (int i = 0; i < viewGroup.getChildCount(); i++) {
161 | setShadowOutlineProviderAt(i);
162 | }
163 | } else {
164 | setShadowOutlineProviderAt(pos);
165 | }
166 | }
167 |
168 | private void setShadowOutlineProviderAt(int childIndex) {
169 | Path shadowPath = getViewPathWithOffsetAt(childIndex);
170 | if (shadowPath == null) {
171 | //Path calculation is still in progress
172 | return;
173 | }
174 | final View child = viewGroup.getChildAt(childIndex);
175 | CustomViewOutlineProvider outlineProvider = new CustomViewOutlineProvider(shadowPath, shadowAlpha);
176 | child.setOutlineProvider(outlineProvider);
177 |
178 | if (shouldAnimateShadow) {
179 | animateOutlineAlpha(child, outlineProvider);
180 | }
181 | }
182 |
183 | private void animateOutlineAlpha(final View child, CustomViewOutlineProvider outlineProvider) {
184 | ObjectAnimator animator = ObjectAnimator.ofFloat(outlineProvider, "alpha", 0, shadowAlpha);
185 | animator.setDuration(animationDuration);
186 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
187 | @Override
188 | public void onAnimationUpdate(ValueAnimator animation) {
189 | child.invalidateOutline();
190 | }
191 | });
192 | animator.start();
193 | }
194 |
195 | private Path getViewPathWithOffsetAt(int position) {
196 | Path noOffsetPath = viewPaths.get(position);
197 | if (noOffsetPath == null) {
198 | return null;
199 | }
200 | Path path = new Path();
201 | path.set(noOffsetPath);
202 | path.offset(offsetX, offsetY);
203 | return path;
204 | }
205 |
206 | private void calculateAndRenderShadowAsync(final View view, final int pos) {
207 | final Future[] future = new Future[1];
208 | future[0] = workerPool.submit(new Runnable() {
209 | @Override
210 | public void run() {
211 | calculateAndRenderShadow(view, pos);
212 | synchronized (TASKS_LOCK) {
213 | tasksInProgress.remove(future[0]);
214 | }
215 | }
216 | });
217 | tasksInProgress.add(future[0]);
218 | }
219 |
220 | private void cancelTasksInProgress() {
221 | synchronized (TASKS_LOCK) {
222 | for (int i = tasksInProgress.size() - 1; i >= 0; i--) {
223 | Future> task = tasksInProgress.get(i);
224 | task.cancel(true);
225 | tasksInProgress.remove(i);
226 | }
227 | }
228 | }
229 |
230 | private void calculateAndRenderShadow(final View view, int pos) {
231 | view.buildDrawingCache();
232 | if (Thread.currentThread().isInterrupted()) {
233 | return;
234 | }
235 |
236 | Path path = new Path();
237 | List arrayListOutlinePoints;
238 | Bitmap bitmap = view.getDrawingCache();
239 |
240 | try {
241 | //We need to copy it, because drawing cache will be recycled if view is detached
242 | bitmap = bitmap.copy(bitmap.getConfig(), false);
243 | arrayListOutlinePoints = getOutlinePoints(bitmap);
244 | if (arrayListOutlinePoints.isEmpty()) {
245 | return;
246 | }
247 | } catch (Exception e) {
248 | //If drawing cache has been recycled, IllegalStateException will be thrown on copy
249 | return;
250 | } finally {
251 | if (!bitmap.isRecycled()) {
252 | bitmap.recycle();
253 | }
254 | }
255 |
256 | Point2D[] outlinePoints = arrayListOutlinePoints.toArray(new Point2D[arrayListOutlinePoints.size()]);
257 |
258 | GrahamScan grahamScan = new GrahamScan(outlinePoints);
259 |
260 | List arrayListHullPoints = new ArrayList<>();
261 | for (Point2D point2D : grahamScan.hull()) {
262 | arrayListHullPoints.add(point2D);
263 | }
264 | Point2D[] hullPoints = arrayListHullPoints.toArray(new Point2D[arrayListHullPoints.size()]);
265 |
266 | path.moveTo((float) hullPoints[0].x(), (float) hullPoints[0].y());
267 | for (int i = 1; i < hullPoints.length; i++) {
268 | path.lineTo((float) hullPoints[i].x(), (float) hullPoints[i].y());
269 | }
270 |
271 | synchronized (TASKS_LOCK) {
272 | if (Thread.currentThread().isInterrupted()) {
273 | return;
274 | }
275 | uiThreadHandler.postAtFrontOfQueue(new SetViewOutlineTask(pos, path));
276 | }
277 | }
278 |
279 | private List getOutlinePoints(Bitmap bitmap) {
280 | ArrayList arrayList = new ArrayList<>();
281 |
282 | for (int i = 0; i < bitmap.getHeight(); i++) {
283 | if (Color.alpha(bitmap.getPixel(0, i)) > 0) {
284 | arrayList.add(new Point2D(0, i));
285 | }
286 |
287 | if (Color.alpha(bitmap.getPixel(bitmap.getWidth() - 1, i)) > 0) {
288 | arrayList.add(new Point2D(bitmap.getWidth() - 1, i));
289 | }
290 | }
291 |
292 | if (Thread.currentThread().isInterrupted()) {
293 | return Collections.emptyList();
294 | }
295 |
296 | for (int i = 0; i < bitmap.getHeight(); i++) {
297 | for (int j = 1; j < bitmap.getWidth() - 1; j++) {
298 | if (Color.alpha(bitmap.getPixel(j - 1, i)) == 0 && Color.alpha(bitmap.getPixel(j, i)) > 0) {
299 | arrayList.add(new Point2D(j, i));
300 | }
301 | if (Color.alpha(bitmap.getPixel(j - 1, i)) > 0 && Color.alpha(bitmap.getPixel(j, i)) == 0) {
302 | arrayList.add(new Point2D(j - 1, i));
303 | }
304 | }
305 | }
306 |
307 | return arrayList;
308 | }
309 |
310 | private class SetViewOutlineTask implements Runnable {
311 |
312 | private int viewPos;
313 | private Path shadowPath;
314 |
315 | private SetViewOutlineTask(int pos, Path path) {
316 | this.viewPos = pos;
317 | this.shadowPath = path;
318 | }
319 |
320 | @Override
321 | public void run() {
322 | if (viewGroup.isAttachedToWindow()) {
323 | viewPaths.put(viewPos, shadowPath);
324 | if (shouldShowWhenAllReady) {
325 | if (viewPaths.size() == childrenWithShadow) {
326 | updateShadows(POS_UPDATE_ALL);
327 | }
328 | return;
329 | }
330 | updateShadows(viewPos);
331 | }
332 | }
333 | }
334 |
335 | }
336 |
--------------------------------------------------------------------------------
/materialshadows/src/main/java/com/sdsmdg/harjot/materialshadows/utilities/GrahamScan.java:
--------------------------------------------------------------------------------
1 | package com.sdsmdg.harjot.materialshadows.utilities;
2 |
3 | import java.util.ArrayDeque;
4 | import java.util.Arrays;
5 | import java.util.Deque;
6 | import java.util.Stack;
7 |
8 | public class GrahamScan {
9 | private Deque hull = new ArrayDeque<>();
10 |
11 | public GrahamScan(Point2D[] pts) {
12 |
13 | // defensive copy
14 | int N = pts.length;
15 | Point2D[] points = new Point2D[N];
16 | System.arraycopy(pts, 0, points, 0, N);
17 | Arrays.sort(points);
18 |
19 | Arrays.sort(points, 1, N, points[0].POLAR_ORDER);
20 |
21 | hull.push(points[0]); // p[0] is first extreme point
22 | int k1;
23 | for (k1 = 1; k1 < N; k1++)
24 | if (!points[0].equals(points[k1]))
25 | break;
26 | if (k1 == N)
27 | return; // all points equal
28 |
29 | int k2;
30 | for (k2 = k1 + 1; k2 < N; k2++)
31 | if (Point2D.ccw(points[0], points[k1], points[k2]) != 0)
32 | break;
33 | hull.push(points[k2 - 1]); // points[k2-1] is second extreme point
34 |
35 | for (int i = k2; i < N; i++) {
36 | Point2D top = hull.pop();
37 | while (Point2D.ccw(hull.peek(), top, points[i]) <= 0)
38 | {
39 | top = hull.pop();
40 | }
41 | hull.push(top);
42 | hull.push(points[i]);
43 | }
44 |
45 | assert isConvex();
46 | }
47 |
48 | public Iterable hull() {
49 | Stack s = new Stack();
50 | for (Point2D p : hull)
51 | s.push(p);
52 | return s;
53 | }
54 |
55 | private boolean isConvex() {
56 | int N = hull.size();
57 | if (N <= 2) {
58 | return true;
59 | }
60 |
61 | Point2D[] points = new Point2D[N];
62 | int n = 0;
63 | for (Point2D p : hull()){
64 | points[n++] = p;
65 | }
66 |
67 | for (int i = 0; i < N; i++){
68 | if (Point2D.ccw(points[i], points[(i + 1) % N], points[(i + 2) % N]) <= 0){
69 | return false;
70 | }
71 | }
72 | return true;
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/materialshadows/src/main/java/com/sdsmdg/harjot/materialshadows/utilities/Point2D.java:
--------------------------------------------------------------------------------
1 | package com.sdsmdg.harjot.materialshadows.utilities;
2 |
3 | import java.util.Comparator;
4 |
5 | public class Point2D implements Comparable {
6 | public final Comparator POLAR_ORDER = new PolarOrder();
7 |
8 | private final double x; // x coordinate
9 | private final double y; // y coordinate
10 |
11 | public Point2D(double x, double y) {
12 | if (Double.isInfinite(x) || Double.isInfinite(y))
13 | throw new IllegalArgumentException("Coordinates must be finite");
14 | if (Double.isNaN(x) || Double.isNaN(y))
15 | throw new IllegalArgumentException("Coordinates cannot be NaN");
16 | if (x == 0.0)
17 | x = 0.0; // convert -0.0 to +0.0
18 | if (y == 0.0)
19 | y = 0.0; // convert -0.0 to +0.0
20 | this.x = x;
21 | this.y = y;
22 | }
23 |
24 | public double x() {
25 | return x;
26 | }
27 |
28 | public double y() {
29 | return y;
30 | }
31 |
32 | public double r() {
33 | return Math.sqrt(x * x + y * y);
34 | }
35 |
36 | public static int ccw(Point2D a, Point2D b, Point2D c) {
37 | double area2 = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
38 | if (area2 < 0)
39 | return -1;
40 | else if (area2 > 0)
41 | return +1;
42 | else
43 | return 0;
44 | }
45 |
46 | public int compareTo(Point2D that) {
47 | if (this.y < that.y)
48 | return -1;
49 | if (this.y > that.y)
50 | return +1;
51 | if (this.x < that.x)
52 | return -1;
53 | if (this.x > that.x)
54 | return +1;
55 | return 0;
56 | }
57 |
58 | private class PolarOrder implements Comparator {
59 | public int compare(Point2D q1, Point2D q2) {
60 | double dx1 = q1.x - x;
61 | double dy1 = q1.y - y;
62 | double dx2 = q2.x - x;
63 | double dy2 = q2.y - y;
64 |
65 | if (dy1 >= 0 && dy2 < 0)
66 | return -1; // q1 above; q2 below
67 | else if (dy2 >= 0 && dy1 < 0)
68 | return +1; // q1 below; q2 above
69 | else if (dy1 == 0 && dy2 == 0) { // 3-collinear and horizontal
70 | if (dx1 >= 0 && dx2 < 0)
71 | return -1;
72 | else if (dx2 >= 0 && dx1 < 0)
73 | return +1;
74 | else
75 | return 0;
76 | } else
77 | return -ccw(Point2D.this, q1, q2); // both above or below
78 | }
79 | }
80 |
81 | public boolean equals(Object other) {
82 | if (other == this)
83 | return true;
84 | if (other == null)
85 | return false;
86 | if (other.getClass() != this.getClass())
87 | return false;
88 | Point2D that = (Point2D) other;
89 | return this.x == that.x && this.y == that.y;
90 | }
91 |
92 | public String toString() {
93 | return "(" + x + ", " + y + ")";
94 | }
95 |
96 | public int hashCode() {
97 | int hashX = ((Double) x).hashCode();
98 | int hashY = ((Double) y).hashCode();
99 | return 31 * hashX + hashY;
100 | }
101 |
102 | }
--------------------------------------------------------------------------------
/materialshadows/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/materialshadows/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | MaterialShadows
3 |
4 |
--------------------------------------------------------------------------------
/materialshadows/src/test/java/com/sdsmdg/harjot/materialshadows/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.sdsmdg.harjot.materialshadows;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/screens/cover.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/screens/cover.png
--------------------------------------------------------------------------------
/screens/example_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/screens/example_1.png
--------------------------------------------------------------------------------
/screens/example_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/screens/example_2.png
--------------------------------------------------------------------------------
/screens/example_3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/screens/example_3.png
--------------------------------------------------------------------------------
/screens/example_4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harjot-oberai/MaterialShadows/62870f336acb75fb41fa426f6779a1111b1f1f26/screens/example_4.png
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':materialshadows'
2 |
--------------------------------------------------------------------------------