Repository: Hitomis/CrazyShadow
Branch: master
Commit: 98140b76fed9
Files: 40
Total size: 95.5 KB
Directory structure:
gitextract_svsycx0l/
├── .gitignore
├── README.md
├── app/
│ ├── .gitignore
│ ├── build.gradle
│ ├── proguard-rules.pro
│ └── src/
│ ├── androidTest/
│ │ └── java/
│ │ └── com/
│ │ └── hitomi/
│ │ └── crazyshadow/
│ │ └── ApplicationTest.java
│ ├── main/
│ │ ├── AndroidManifest.xml
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── hitomi/
│ │ │ └── crazyshadow/
│ │ │ └── MainActivity.java
│ │ └── res/
│ │ ├── drawable/
│ │ │ ├── shape_float.xml
│ │ │ └── shape_wrap.xml
│ │ ├── layout/
│ │ │ └── activity_main.xml
│ │ ├── values/
│ │ │ ├── colors.xml
│ │ │ ├── dimens.xml
│ │ │ ├── strings.xml
│ │ │ └── styles.xml
│ │ └── values-w820dp/
│ │ └── dimens.xml
│ └── test/
│ └── java/
│ └── com/
│ └── hitomi/
│ └── crazyshadow/
│ └── ExampleUnitTest.java
├── build.gradle
├── cslibrary/
│ ├── .gitignore
│ ├── build.gradle
│ ├── proguard-rules.pro
│ └── src/
│ └── main/
│ ├── AndroidManifest.xml
│ ├── java/
│ │ └── com/
│ │ └── hitomi/
│ │ └── cslibrary/
│ │ ├── CrazyShadow.java
│ │ ├── base/
│ │ │ ├── CrazyShadowAttr.java
│ │ │ ├── CrazyShadowDirection.java
│ │ │ ├── ShadowHandler.java
│ │ │ └── view/
│ │ │ ├── CornerShadowView.java
│ │ │ └── EdgeShadowView.java
│ │ ├── draw/
│ │ │ ├── RoundRectShadowDrawable.java
│ │ │ └── ShadowDrawer.java
│ │ ├── floating/
│ │ │ └── ShadowFloating.java
│ │ └── wrap/
│ │ └── ShadowWrapper.java
│ └── res/
│ ├── color/
│ │ └── background.xml
│ └── values/
│ └── strings.xml
├── gradle/
│ └── wrapper/
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
================================================
FILE: README.md
================================================
# CrazyShadow
[](https://jitpack.io/#Hitomis/CrazyShadow)
CrazyShadow 是一个专门为 View 添加阴影效果的库。 CrazyShadow 支持自定义阴影实现方式、阴影颜色、阴影经度大小、阴影附加方位。
说明:
CrazyShadow 主要是因为自己一直想为最近编写的 [ActivitySwitch](https://github.com/Hitomis/ActivitySwitcher)
添加阴影效果,之前在网上一直没有找到合适的可以用的参考案例。所以只能自己去编写这样一个 library。
而在实际运用在,发现理想总是丰满的,现实很骨感。虽然可以实现优美的效果以及多功能性,但是使用场景比较单一:只能
为传统的矩形UI(即最终显示效果)添加阴影。为什么结果会是这样呢?大家都知道,一个 View 显示的模型肯定是一个矩形,不
管如何自定义 View 都有 left、top、right、bottom 以及 width、height 等一些概念,而这些概念也定位了 View 的模
型必定为一个矩形。所以 阴影效果的添加依据模型必定也是一个矩形。
虽然有了很多限制,我想既然都做了,还是尽自己最大努力吧,目前 CrazyShadow 支持三种方式为 View 添加阴影效果。
1. wrap : 以包装 View 的方式添加阴影效果。该方式会改变 View 的大小。因为如果保持 View 大小不变同时又在 View四周添加了阴影,那么 View 的尺寸定会比之前大,这样可能会引起 View 周围其他 View 的位置变化。
2. float : 以 View 的根视图(ContentFrameLayout) 中添加一层阴影效果。不过因为与 View 不在一个布局层面上,所以当发生用户交互使原 View 的位置发生改变后,阴影还是会留在原来的位置。此种方式只能在位置不会发生改变的 View 上使用。慎用啊
3. drawable : 直接创建一个附带阴影效果的矩形(支持圆角) drawable 作为 View 的 background, 简单粗暴。不过因为 background 的最大尺寸为原 View 的尺寸,所以设置了阴影后,原 View 大小会依据配置的阴影大小发生变化,并且原 View 的位置会向上的偏移来显示出阴影的效果。
# Preview
# Import
### Gradle
Step 1. Add the JitPack repository to your build file
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Step 2. Add the dependency
dependencies {
compile 'com.github.Hitomis:CrazyShadow:v1.0.1'
}
### Maven
Step 1. Add the JitPack repository to your build file
jitpack.io
https://jitpack.io
Step 2. Add the dependency
com.github.Hitomis
CrazyShadow
v1.0.1
# Usage
new CrazyShadow.Builder()
.setContext(this)
.setDirection(CrazyShadowDirection.ALL)
.setShadowRadius(dip2Px(3))
.setCorner(dip2Px(5))
.setBackground(Color.parseColor("#96a993"))
.setImpl(CrazyShadow.IMPL_DRAW)
.action(findViewById(R.id.relay_draw1));
更多示例代码可以查看 [MainActivity](https://github.com/Hitomis/CrazyShadow/blob/master/app/src/main/java/com/hitomi/crazyshadow/MainActivity.java)
# Attribute
| 属性 | 说明 |
| :--: | :--: |
| impl | 以何种方式添加阴影,支持 wrap、float、drawable 三种方式 |
| baseShadowColor | 阴影的基本颜色,即最深的颜色,与 colors 表示为同一个作用, 如果baseShadowColor 与 colors 都不设置,阴影会使用默认颜色|
| background | 修改 View 的背景色,如果使用 drawable 方式添加阴影,那么该属性必须添加 |
| colors | 绘制阴影时需要的一个颜色由深到浅且长度为3的数组, 该属性与 baseShadowColor 起同一个作用,如果单单只设置 baseShadowColor 也会自动转换成为 colors |
| corner | 阴影顶点的内侧弧度。以适配被设置的 View 是圆角的情况, 对使用 drawable 方式设置阴影时,该属性表示为圆角矩形背景的圆角角度 |
| shadowRadius | 阴影大小 |
| direction | 设定阴影在 View 上显示的方位, 支持的方位详情可以查看 [CrazyShadowDirection](https://github.com/Hitomis/CrazyShadow/blob/master/cslibrary/src/main/java/com/hitomi/cslibrary/base/CrazyShadowDirection.java) |
# Method
| 方法 | 说明 |
| :--: | :--: |
| make | 为 View 添加阴影效果, 使用 Builder 的 action 方法时会自动调用 |
| remove | 移除阴影|
| hide | 隐藏阴影,与 remove 不同的是,hide 只是隐藏了 View 周围的阴影效果,并没有移除 |
| show | 如果调用了 hide ,可以再使用 show 将阴影效果显示出来 |
#Licence
Copyright 2016 Hitomis, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
================================================
FILE: app/.gitignore
================================================
/build
================================================
FILE: app/build.gradle
================================================
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.hitomi.crazyshadow"
minSdkVersion 14
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.1.1'
compile project(':cslibrary')
}
================================================
FILE: app/proguard-rules.pro
================================================
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in E:\androidstuido_sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
================================================
FILE: app/src/androidTest/java/com/hitomi/crazyshadow/ApplicationTest.java
================================================
package com.hitomi.crazyshadow;
import android.app.Application;
import android.test.ApplicationTestCase;
/**
* Testing Fundamentals
*/
public class ApplicationTest extends ApplicationTestCase {
public ApplicationTest() {
super(Application.class);
}
}
================================================
FILE: app/src/main/AndroidManifest.xml
================================================
================================================
FILE: app/src/main/java/com/hitomi/crazyshadow/MainActivity.java
================================================
package com.hitomi.crazyshadow;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.hitomi.cslibrary.CrazyShadow;
import com.hitomi.cslibrary.base.CrazyShadowDirection;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private View titleView;
private View drawView0, drawView1;
private View floatView0, floatView1;
private View wrapView0, wrapView1;
private View shadowView0, shadowView1;
private CrazyShadow titleCrazyShadow;
private CrazyShadow drawCrazyShadow0, drawCrazyShadow1;
private CrazyShadow floatCrazyShadow0, floatCrazyShadow1;
private CrazyShadow wrapCrazyShadow0, wrapCrazyShadow1;
private CrazyShadow shadowCrazyShadow0, shadowCrazyShadow1;
private boolean titleFlag = true, draw0Flag = true, draw1Flag = true, float0Flag = true, float1Flag = true;
private boolean wrap0Flag = true, wrap1Flag = true, shadow0Flag = true, shadow1Flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initShadow();
initListener();
}
private void initListener() {
titleView.setOnClickListener(this);
drawView0.setOnClickListener(this);
drawView1.setOnClickListener(this);
floatView0.setOnClickListener(this);
floatView1.setOnClickListener(this);
wrapView0.setOnClickListener(this);
wrapView1.setOnClickListener(this);
shadowView0.setOnClickListener(this);
shadowView1.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.relay_title:
if (titleFlag) {
titleCrazyShadow.hide();
} else {
titleCrazyShadow.show();
}
titleFlag = !titleFlag;
break;
case R.id.relay_draw0:
if (draw0Flag) {
drawCrazyShadow0.hide();
} else {
drawCrazyShadow0.show();
}
draw0Flag = !draw0Flag;
break;
case R.id.relay_draw1:
if (draw1Flag) {
drawCrazyShadow1.hide();
} else {
drawCrazyShadow1.show();
}
draw1Flag = !draw1Flag;
break;
case R.id.relay_float0:
if (float0Flag) {
floatCrazyShadow0.hide();
} else {
floatCrazyShadow0.show();
}
float0Flag = !float0Flag;
break;
case R.id.relay_float1:
if (float1Flag) {
floatCrazyShadow1.hide();
} else {
floatCrazyShadow1.show();
}
float1Flag = !float1Flag;
break;
case R.id.relay_wrap0:
if (wrap0Flag) {
wrapCrazyShadow0.hide();
} else {
wrapCrazyShadow0.show();
}
wrap0Flag = !wrap0Flag;
break;
case R.id.relay_wrap1:
if (wrap1Flag) {
wrapCrazyShadow1.hide();
} else {
wrapCrazyShadow1.show();
}
wrap1Flag = !wrap1Flag;
break;
case R.id.relay_shadow0:
if (shadow0Flag) {
shadowCrazyShadow0.hide();
} else {
shadowCrazyShadow0.show();
}
shadow0Flag = !shadow0Flag;
break;
case R.id.relay_shadow1:
if (shadow1Flag) {
shadowCrazyShadow1.hide();
} else {
shadowCrazyShadow1.show();
}
shadow1Flag = !shadow1Flag;
break;
}
}
private void initShadow() {
titleCrazyShadow = new CrazyShadow.Builder()
.setContext(this)
.setDirection(CrazyShadowDirection.BOTTOM)
.setShadowRadius(dip2Px(5))
.setImpl(CrazyShadow.IMPL_WRAP)
.action(titleView);
drawCrazyShadow0 = new CrazyShadow.Builder()
.setContext(this)
.setDirection(CrazyShadowDirection.ALL)
.setShadowRadius(dip2Px(3))
.setBaseShadowColor(Color.RED)
.setImpl(CrazyShadow.IMPL_DRAW)
.action(drawView0);
drawCrazyShadow1 = new CrazyShadow.Builder()
.setContext(this)
.setDirection(CrazyShadowDirection.ALL)
.setShadowRadius(dip2Px(3))
.setCorner(dip2Px(5))
.setBackground(Color.parseColor("#96a993"))
.setImpl(CrazyShadow.IMPL_DRAW)
.action(drawView1);
floatCrazyShadow0 = new CrazyShadow.Builder()
.setContext(this)
.setDirection(CrazyShadowDirection.ALL)
.setShadowRadius(dip2Px(3))
.setImpl(CrazyShadow.IMPL_FLOAT)
.action(floatView0);
floatCrazyShadow1 = new CrazyShadow.Builder()
.setContext(this)
.setDirection(CrazyShadowDirection.ALL)
.setShadowRadius(dip2Px(3))
.setCorner(dip2Px(4))
.setImpl(CrazyShadow.IMPL_FLOAT)
.action(floatView1);
wrapCrazyShadow0 = new CrazyShadow.Builder()
.setContext(this)
.setDirection(CrazyShadowDirection.ALL)
.setShadowRadius(dip2Px(3))
.setImpl(CrazyShadow.IMPL_WRAP)
.action(wrapView0);
wrapCrazyShadow1 = new CrazyShadow.Builder()
.setContext(this)
.setDirection(CrazyShadowDirection.ALL)
.setShadowRadius(dip2Px(5))
.setCorner(dip2Px(8))
.setImpl(CrazyShadow.IMPL_WRAP)
.action(wrapView1);
shadowCrazyShadow0 = new CrazyShadow.Builder()
.setContext(this)
.setDirection(CrazyShadowDirection.ALL)
.setShadowRadius(dip2Px(3))
.setBackground(Color.parseColor("#a0a0a0"))
.setBaseShadowColor(Color.parseColor("#a0a0a0"))
.setImpl(CrazyShadow.IMPL_WRAP)
.action(findViewById(R.id.relay_shadow0));
shadowCrazyShadow1 = new CrazyShadow.Builder()
.setContext(this)
.setDirection(CrazyShadowDirection.TOP)
.setShadowRadius(dip2Px(5))
.setImpl(CrazyShadow.IMPL_WRAP)
.action(findViewById(R.id.relay_shadow1));
}
private void initView() {
titleView = findViewById(R.id.relay_title);
drawView0 = findViewById(R.id.relay_draw0);
drawView1 = findViewById(R.id.relay_draw1);
floatView0 = findViewById(R.id.relay_float0);
floatView1 = findViewById(R.id.relay_float1);
wrapView0 = findViewById(R.id.relay_wrap0);
wrapView1 = findViewById(R.id.relay_wrap1);
shadowView0 = findViewById(R.id.relay_shadow0);
shadowView1 = findViewById(R.id.relay_shadow1);
}
public int dip2Px(float dpValue) {
final float scale = getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
================================================
FILE: app/src/main/res/drawable/shape_float.xml
================================================
================================================
FILE: app/src/main/res/drawable/shape_wrap.xml
================================================
================================================
FILE: app/src/main/res/layout/activity_main.xml
================================================
================================================
FILE: app/src/main/res/values/colors.xml
================================================
#3F51B5
#303F9F
#FF4081
================================================
FILE: app/src/main/res/values/dimens.xml
================================================
16dp
16dp
================================================
FILE: app/src/main/res/values/strings.xml
================================================
CrazyShadow
================================================
FILE: app/src/main/res/values/styles.xml
================================================
================================================
FILE: app/src/main/res/values-w820dp/dimens.xml
================================================
64dp
================================================
FILE: app/src/test/java/com/hitomi/crazyshadow/ExampleUnitTest.java
================================================
package com.hitomi.crazyshadow;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* To work on unit tests, switch the Test Artifact in the Build Variants view.
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
================================================
FILE: build.gradle
================================================
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
================================================
FILE: cslibrary/.gitignore
================================================
/build
================================================
FILE: cslibrary/build.gradle
================================================
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
group='com.github.Hitomi'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
}
================================================
FILE: cslibrary/proguard-rules.pro
================================================
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in E:\androidstuido_sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
================================================
FILE: cslibrary/src/main/AndroidManifest.xml
================================================
================================================
FILE: cslibrary/src/main/java/com/hitomi/cslibrary/CrazyShadow.java
================================================
package com.hitomi.cslibrary;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import com.hitomi.cslibrary.base.CrazyShadowAttr;
import com.hitomi.cslibrary.base.CrazyShadowDirection;
import com.hitomi.cslibrary.base.ShadowHandler;
import com.hitomi.cslibrary.draw.ShadowDrawer;
import com.hitomi.cslibrary.floating.ShadowFloating;
import com.hitomi.cslibrary.wrap.ShadowWrapper;
/**
* Created by hitomi on 2016/10/19.
*
* email : 196425254@qq.com
*
* https://github.com/Hitomis
*/
public class CrazyShadow {
/**
* 以 {@link android.view.View#setBackground(Drawable)} 的形式为你的
* View 添加阴影背景,同时可以设置圆角 [{@link com.hitomi.cslibrary.draw.ShadowDrawer}]
*/
public static final String IMPL_DRAW = "drawer";
/**
* 以包装的形式为你的 View 添加阴影 [{@link com.hitomi.cslibrary.wrap.ShadowWrapper}]
*/
public static final String IMPL_WRAP = "wrapper";
/**
* 以浮动修饰的形式为你的 View 添加阴影 [{@link ShadowFloating}]
*/
public static final String IMPL_FLOAT = "floating";
private Context context;
private ShadowHandler shadowHandler;
private boolean makeShadow;
private CrazyShadow(Context context) {
this.context = context;
}
private void createShadowHandler(CrazyShadowAttr attr) {
if (attr.getImpl().equals(IMPL_DRAW)) {
shadowHandler = new ShadowDrawer(attr);
} else if (attr.getImpl().equals(IMPL_WRAP)) {
shadowHandler = new ShadowWrapper(context, attr);
} else {
shadowHandler = new ShadowFloating(context, attr);
}
}
/**
* 为 View 添加阴影效果
* @param view
*/
public void make(View view) {
if (!makeShadow) {
shadowHandler.makeShadow(view);
makeShadow = true;
}
}
/**
* 将添加的阴影效果移除
*/
public void remove() {
if (makeShadow) {
shadowHandler.removeShadow();
makeShadow = false;
}
}
/**
* 显示阴影效果
*/
public void show() {
shadowHandler.showShadow();
}
/**
* 隐藏阴影效果
*/
public void hide() {
shadowHandler.hideShadow();
}
public static class Builder {
private Context context;
/**
* {@link #IMPL_DRAW}
* {@link #IMPL_WRAP}
* {@link #IMPL_FLOAT}
*/
private String impl;
/**
* 阴影的基本色
*/
private int baseShadowColor;
/**
* 背景色
*/
private int background;
/**
* 表示阴影的一个颜色由深到浅且长度为3的数组
*/
private int[] colors;
/**
* 对 {@link #IMPL_DRAW} 形式表示为背景的圆角角度.
* 对 {@link #IMPL_WRAP} 与 {@link #IMPL_FLOAT}
* 表示为阴影顶点的内侧弧度。以适配被设置的 View 是圆角的情况
*/
private float corner;
/**
* 阴影半径
*/
private float shadowRadius;
/**
* 阴影设置在 View 的方向
*/
@CrazyShadowDirection
private int direction;
public Builder setContext(Context context) {
this.context = context;
return this;
}
/**
* 以何种方式添加阴影:
* {@link #IMPL_DRAW}
* {@link #IMPL_WRAP}
* {@link #IMPL_FLOAT}
* @param impl
* @return Builder
*/
public Builder setImpl(String impl) {
this.impl = impl;
return this;
}
/**
* 阴影的基本颜色,即最深的颜色, {@link CrazyShadowAttr#setBaseShadowColor(int)}
* 方法会自动计算出绘制阴影时需要的 {@link #colors}
* @param baseColor
* @return Builder
*/
public Builder setBaseShadowColor(int baseColor) {
this.baseShadowColor = baseColor;
return this;
}
/**
* 背景色
* @param background
* @return Builder
*/
public Builder setBackground(int background) {
this.background = background;
return this;
}
/**
* 绘制阴影时需要的一个颜色由深到浅且长度为3的数组
* @param colors
* @return Builder
*/
public Builder setColors(int[] colors) {
this.colors = colors;
return this;
}
/**
* 对 {@link #IMPL_DRAW} 形式表示为背景的圆角角度.
* 对 {@link #IMPL_WRAP} 与 {@link #IMPL_FLOAT}
* 表示为阴影顶点的内侧弧度。以适配被设置的 View 是圆角的情况
* @param corner [unit : pixels]
* @return Builder
*/
public Builder setCorner(float corner) {
this.corner = corner;
return this;
}
/**
* 设置阴影半径
* @param shadowRadius [unit : pixels]
* @return Builder
*/
public Builder setShadowRadius(float shadowRadius) {
this.shadowRadius = shadowRadius;
return this;
}
/**
* 设置阴影的方向,具体查看 {@link CrazyShadowAttr}
* @param direction
* @return Builder
*/
public Builder setDirection(int direction) {
this.direction = direction;
return this;
}
private CrazyShadow create() {
if (colors == null && baseShadowColor == 0)
// 默认的颜色。由深到浅
//分别为开始颜色,中间颜色,结束颜色
colors = new int[]{0x63000000, 0x32000000, 0x00000000};
CrazyShadowAttr attr = new CrazyShadowAttr();
attr.setImpl(impl);
attr.setBaseShadowColor(baseShadowColor);
attr.setBackground(background);
attr.setColors(colors);
attr.setCorner(corner);
attr.setShadowRadius(shadowRadius);
attr.setDirection(direction);
CrazyShadow crazyShadow = new CrazyShadow(context);
crazyShadow.createShadowHandler(attr);
return crazyShadow;
}
/**
* 绘制阴影的启动方法,你需要保证参数已经正确设置完毕
* @param view 被添加阴影的 View
*/
public CrazyShadow action(View view) {
CrazyShadow crazyShadow = create();
crazyShadow.make(view);
return crazyShadow;
}
}
}
================================================
FILE: cslibrary/src/main/java/com/hitomi/cslibrary/base/CrazyShadowAttr.java
================================================
package com.hitomi.cslibrary.base;
import android.support.v4.graphics.ColorUtils;
/**
* Created by hitomi on 2016/10/19.
*/
public class CrazyShadowAttr {
/**
* 以何种方式添加阴影:
* {@link com.hitomi.cslibrary.CrazyShadow#IMPL_DRAW}
* {@link com.hitomi.cslibrary.CrazyShadow#IMPL_WRAP}
* {@link com.hitomi.cslibrary.CrazyShadow#IMPL_FLOAT}
*/
private String impl;
/**
* 阴影的基本颜色,即最深的颜色, {@link #setBaseShadowColor(int)} 方法会自动计算出
* 绘制阴影时需要的 {@link #colors}
*/
private int baseShadowColor;
/**
* 仅仅对 {@link com.hitomi.cslibrary.CrazyShadow#IMPL_DRAW} 形式的方设置阴影时需要的属性
*/
private int background;
/**
* 绘制阴影时需要的一个颜色由深到浅且长度为3的数组
*/
private int[] colors;
/**
* 对 {@link com.hitomi.cslibrary.CrazyShadow#IMPL_DRAW} 形式表示为背景的圆角角度.
* 对 {@link com.hitomi.cslibrary.CrazyShadow#IMPL_WRAP} 与
* {@link com.hitomi.cslibrary.CrazyShadow#IMPL_FLOAT}
* 表示为阴影顶点的内侧弧度。以适配被设置的 View 是圆角的情况
*/
private float corner;
/**
* 阴影半径
*/
private float shadowRadius;
/**
* 设置阴影的方向,具体查看 {@link CrazyShadowAttr}
*/
@CrazyShadowDirection
private int direction;
public String getImpl() {
return impl;
}
public void setImpl(String impl) {
this.impl = impl;
}
public void setBaseShadowColor(int baseShadowColor) {
this.baseShadowColor = baseShadowColor;
if (colors == null) {
colors = new int[3];
colors[0] = ColorUtils.setAlphaComponent(baseShadowColor, 255);
colors[1] = ColorUtils.setAlphaComponent(baseShadowColor, 128);
colors[2] = ColorUtils.setAlphaComponent(baseShadowColor, 0);
}
}
public int getBackground() {
return background;
}
public void setBackground(int background) {
this.background = background;
}
public int[] getColors() {
return colors;
}
public void setColors(int[] colors) {
if (colors != null)
this.colors = colors;
}
public float getCorner() {
return corner;
}
public void setCorner(float corner) {
this.corner = corner;
}
public float getShadowRadius() {
return shadowRadius;
}
public void setShadowRadius(float shadowRadius) {
this.shadowRadius = shadowRadius;
}
public int getDirection() {
return direction;
}
public void setDirection(@CrazyShadowDirection int direction) {
this.direction = direction;
}
public boolean containLeft() {
return direction == CrazyShadowDirection.ALL ||
direction == CrazyShadowDirection.LEFT ||
direction == CrazyShadowDirection.LEFT_TOP ||
direction == CrazyShadowDirection.BOTTOM_LEFT ||
direction == CrazyShadowDirection.BOTTOM_LEFT_TOP ||
direction == CrazyShadowDirection.RIGHT_BOTTOM_LEFT ||
direction == CrazyShadowDirection.LEFT_TOP_RIGHT;
}
public boolean containTop() {
return direction == CrazyShadowDirection.ALL ||
direction == CrazyShadowDirection.TOP ||
direction == CrazyShadowDirection.LEFT_TOP ||
direction == CrazyShadowDirection.TOP_RIGHT ||
direction == CrazyShadowDirection.LEFT_TOP_RIGHT ||
direction == CrazyShadowDirection.BOTTOM_LEFT_TOP ||
direction == CrazyShadowDirection.TOP_RIGHT_BOTTOM;
}
public boolean containRight() {
return direction == CrazyShadowDirection.ALL ||
direction == CrazyShadowDirection.RIGHT ||
direction == CrazyShadowDirection.TOP_RIGHT ||
direction == CrazyShadowDirection.RIGHT_BOTTOM ||
direction == CrazyShadowDirection.TOP_RIGHT_BOTTOM ||
direction == CrazyShadowDirection.LEFT_TOP_RIGHT ||
direction == CrazyShadowDirection.RIGHT_BOTTOM_LEFT;
}
public boolean containBottom() {
return direction == CrazyShadowDirection.ALL ||
direction == CrazyShadowDirection.BOTTOM ||
direction == CrazyShadowDirection.BOTTOM_LEFT ||
direction == CrazyShadowDirection.RIGHT_BOTTOM ||
direction == CrazyShadowDirection.RIGHT_BOTTOM_LEFT ||
direction == CrazyShadowDirection.TOP_RIGHT_BOTTOM ||
direction == CrazyShadowDirection.BOTTOM_LEFT_TOP;
}
}
================================================
FILE: cslibrary/src/main/java/com/hitomi/cslibrary/base/CrazyShadowDirection.java
================================================
package com.hitomi.cslibrary.base;
import android.support.annotation.IntDef;
/**
* Created by hitomi on 2016/10/10.
*/
@IntDef(flag = true, value = {CrazyShadowDirection.LEFT, CrazyShadowDirection.TOP,
CrazyShadowDirection.RIGHT, CrazyShadowDirection.BOTTOM,
CrazyShadowDirection.LEFT_TOP, CrazyShadowDirection.TOP_RIGHT,
CrazyShadowDirection.RIGHT_BOTTOM, CrazyShadowDirection.BOTTOM_LEFT,
CrazyShadowDirection.BOTTOM_LEFT_TOP, CrazyShadowDirection.LEFT_TOP_RIGHT,
CrazyShadowDirection.TOP_RIGHT_BOTTOM, CrazyShadowDirection.RIGHT_BOTTOM_LEFT,
CrazyShadowDirection.ALL})
public @interface CrazyShadowDirection {
int LEFT = 1;
int TOP = 1 << 1;
int RIGHT = 1 << 2;
int BOTTOM = 1 << 3;
int LEFT_TOP = 1 << 4;
int TOP_RIGHT = 1 << 5;
int RIGHT_BOTTOM = 1 << 6;
int BOTTOM_LEFT = 1 << 7;
int BOTTOM_LEFT_TOP = 1 << 8;
int LEFT_TOP_RIGHT = 1 << 9;
int TOP_RIGHT_BOTTOM = 1 << 10;
int RIGHT_BOTTOM_LEFT = 1 << 11;
int ALL = 1 << 12;
}
================================================
FILE: cslibrary/src/main/java/com/hitomi/cslibrary/base/ShadowHandler.java
================================================
package com.hitomi.cslibrary.base;
import android.view.View;
/**
* Created by hitomi on 2016/10/19.
*/
public interface ShadowHandler {
void makeShadow(View view);
void removeShadow();
void hideShadow();
void showShadow();
}
================================================
FILE: cslibrary/src/main/java/com/hitomi/cslibrary/base/view/CornerShadowView.java
================================================
package com.hitomi.cslibrary.base.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.view.View;
import com.hitomi.cslibrary.base.CrazyShadowDirection;
/**
* Created by hitomi on 2016/10/18.
*/
public class CornerShadowView extends View {
private Paint shadowPaint;
private Path cornerShadowPath;
private int degrees;
private int[] shadowColors;
private float cornerRadius;
private float shadowSize;
private CornerShadowView(Context context) {
super(context);
init();
}
private void init() {
shadowPaint = new Paint(Paint.DITHER_FLAG);
shadowPaint.setStyle(Paint.Style.FILL);
cornerShadowPath = new Path();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measureSize = (int) (shadowSize + cornerRadius);
setMeasuredDimension(measureSize, measureSize);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.translate(getMeasuredWidth(), getMeasuredHeight());
canvas.rotate(degrees, -getMeasuredWidth() / 2f, -getMeasuredHeight() / 2f);
canvas.drawPath(cornerShadowPath, shadowPaint);
canvas.restore();
}
private void buildRingSectorShadow() {
RectF innerBounds = new RectF(-cornerRadius, -cornerRadius,
cornerRadius, cornerRadius);
RectF outerBounds = new RectF(innerBounds);
outerBounds.inset(-shadowSize, -shadowSize);
cornerShadowPath.reset();
cornerShadowPath.setFillType(Path.FillType.EVEN_ODD);
cornerShadowPath.moveTo(-cornerRadius, 0);
cornerShadowPath.rLineTo(-shadowSize, 0);
// outer arc
cornerShadowPath.arcTo(outerBounds, 180f, 90f, false);
// inner arc
cornerShadowPath.arcTo(innerBounds, 270f, -90f, false);
cornerShadowPath.close();
float startRatio = cornerRadius / (cornerRadius + shadowSize);
shadowPaint.setShader(new RadialGradient(0, 0, cornerRadius + shadowSize,
shadowColors,
new float[]{0f, startRatio, 1f}
, Shader.TileMode.CLAMP));
}
public void setShadowColors(int[] shadowColors) {
this.shadowColors = shadowColors;
}
public void setCornerRadius(float cornerRadius) {
this.cornerRadius = cornerRadius;
}
public void setShadowSize(float shadowSize) {
this.shadowSize = shadowSize;
}
public void setDirection(@CrazyShadowDirection int direction) {
switch (direction) {
case CrazyShadowDirection.LEFT_TOP:
degrees = 0;
break;
case CrazyShadowDirection.TOP_RIGHT:
degrees = 90;
break;
case CrazyShadowDirection.RIGHT_BOTTOM:
degrees = 180;
break;
case CrazyShadowDirection.BOTTOM_LEFT:
degrees = 270;
break;
default:
degrees = 0;
}
}
public static class Builder {
private Context context;
private int[] shadowColors;
private float cornerRadius;
private float shadowSize;
@CrazyShadowDirection
private int direction;
public Builder setContext(Context context) {
this.context = context;
return this;
}
public Builder setShadowColors(int[] shadowColors) {
this.shadowColors = shadowColors;
return this;
}
public Builder setCornerRadius(float cornerRadius) {
this.cornerRadius = cornerRadius;
return this;
}
public Builder setShadowSize(float shadowSize) {
this.shadowSize = shadowSize;
return this;
}
public Builder setDirection(@CrazyShadowDirection int direction) {
this.direction = direction;
return this;
}
public CornerShadowView create() {
// 创建 CornerShadowView
CornerShadowView cornerShadowView = new CornerShadowView(context);
cornerShadowView.setShadowColors(shadowColors);
cornerShadowView.setCornerRadius(cornerRadius);
cornerShadowView.setShadowSize(shadowSize);
cornerShadowView.setDirection(direction);
cornerShadowView.buildRingSectorShadow();
return cornerShadowView;
}
}
}
================================================
FILE: cslibrary/src/main/java/com/hitomi/cslibrary/base/view/EdgeShadowView.java
================================================
package com.hitomi.cslibrary.base.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.view.View;
import com.hitomi.cslibrary.base.CrazyShadowDirection;
/**
* Created by hitomi on 2016/10/17.
*/
public class EdgeShadowView extends View {
private Paint shadowPaint;
private int[] shadowColors;
private float shadowRadius;
private float shadowSize;
private float cornerRadius;
@CrazyShadowDirection
private int direction;
private EdgeShadowView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measureWidth, measureHeight;
if (direction == CrazyShadowDirection.LEFT || direction == CrazyShadowDirection.RIGHT) {
measureWidth = Math.round(shadowRadius);
measureHeight = Math.round(shadowSize);
} else {
measureWidth = Math.round(shadowSize);
measureHeight = Math.round(shadowRadius);
}
setMeasuredDimension(measureWidth, measureHeight);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
switch (direction) {
case CrazyShadowDirection.TOP:
canvas.translate(0, cornerRadius + shadowRadius);
break;
case CrazyShadowDirection.RIGHT:
canvas.translate(-cornerRadius, 0);
canvas.rotate(90);
break;
case CrazyShadowDirection.BOTTOM:
canvas.translate(shadowSize, -cornerRadius);
canvas.rotate(180);
break;
case CrazyShadowDirection.LEFT:
canvas.translate(cornerRadius + shadowRadius, shadowSize);
canvas.rotate(270f);
break;
default:
}
canvas.drawRect(0, -cornerRadius -shadowRadius, shadowSize, -cornerRadius, shadowPaint);
canvas.restore();
}
private void buildEdgeShadowTool() {
shadowPaint = new Paint();
shadowPaint.setStyle(Paint.Style.FILL);
shadowPaint.setShader(new LinearGradient(0, -cornerRadius + shadowRadius, 0, -cornerRadius - shadowRadius,
shadowColors,
new float[]{0f, .5f, 1f}, Shader.TileMode.CLAMP));
}
public void setShadowColors(int[] shadowColors) {
this.shadowColors = shadowColors;
}
public void setShadowRadius(float shadowRadius) {
this.shadowRadius = shadowRadius;
}
public void setCornerRadius(float cornerRadius) {
this.cornerRadius = cornerRadius;
}
public void setShadowSize(float shadowSize) {
this.shadowSize = shadowSize;
}
public void setDirection(@CrazyShadowDirection int direction) {
this.direction = direction;
}
public static class Builder {
private Context context;
private int[] shadowColors;
private float shadowRadius;
private float shadowSize;
private float cornerRadius;
@CrazyShadowDirection
private int direction;
public Builder setContext(Context context) {
this.context = context;
return this;
}
public Builder setShadowColors(int[] shadowColors) {
this.shadowColors = shadowColors;
return this;
}
public Builder setShadowRadius(float shadowRadius) {
this.shadowRadius = shadowRadius;
return this;
}
public Builder setShadowSize(float shadowSize) {
this.shadowSize = shadowSize;
return this;
}
public Builder setCornerRadius(float cornerRadius) {
this.cornerRadius = cornerRadius;
return this;
}
public Builder setDirection(@CrazyShadowDirection int direction) {
this.direction = direction;
return this;
}
public EdgeShadowView create() {
// 创建 EdgeShadowView
EdgeShadowView edgeShadowView = new EdgeShadowView(context);
edgeShadowView.setShadowColors(shadowColors);
edgeShadowView.setShadowRadius(shadowRadius);
edgeShadowView.setShadowSize(shadowSize);
edgeShadowView.setCornerRadius(cornerRadius);
edgeShadowView.setDirection(direction);
edgeShadowView.buildEdgeShadowTool();
return edgeShadowView;
}
}
}
================================================
FILE: cslibrary/src/main/java/com/hitomi/cslibrary/draw/RoundRectShadowDrawable.java
================================================
package com.hitomi.cslibrary.draw;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
/**
* Created by hitomi on 2016/10/17.
*/
public class RoundRectShadowDrawable extends Drawable {
// used to calculate content padding
final static double COS_45 = Math.cos(Math.toRadians(45));
final static float SHADOW_MULTIPLIER = 1.5f;
final int insetShadow; // extra shadow to avoid gaps between card and shadow
final RectF bounds;
Paint paint;
Paint cornerShadowPaint;
Paint edgeShadowPaint;
float cornerRadius;
Path cornerShadowPath;
// updated value with inset
float maxShadowSize;
// actual value set by developer
float rawMaxShadowSize;
// multiplied value to account for shadow offset
float shadowSize;
// actual value set by developer
float rawShadowSize;
private int[] shadowColors;
private boolean dirty = true;
private boolean addPaddingForCorners = true;
public RoundRectShadowDrawable(int background, int[] shadowColors, float radius, float shadowSize, float maxShadowSize) {
insetShadow = 10;
this.shadowColors = shadowColors;
paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
paint.setColor(background);
cornerShadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
cornerShadowPaint.setStyle(Paint.Style.FILL);
cornerRadius = (int) (radius + .5f);
bounds = new RectF();
edgeShadowPaint = new Paint(cornerShadowPaint);
edgeShadowPaint.setAntiAlias(false);
setShadowSize(shadowSize, maxShadowSize);
}
/**
* Casts the value to an even integer.
*/
private int toEven(float value) {
int i = (int) (value + .5f);
if (i % 2 == 1) {
return i - 1;
}
return i;
}
public void setAddPaddingForCorners(boolean addPaddingForCorners) {
this.addPaddingForCorners = addPaddingForCorners;
invalidateSelf();
}
@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
cornerShadowPaint.setAlpha(alpha);
edgeShadowPaint.setAlpha(alpha);
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
dirty = true;
}
void setShadowSize(float shadowSize, float maxShadowSize) {
if (shadowSize < 0f) {
throw new IllegalArgumentException("Invalid shadow size " + shadowSize +
". Must be >= 0");
}
if (maxShadowSize < 0f) {
throw new IllegalArgumentException("Invalid max shadow size " + maxShadowSize +
". Must be >= 0");
}
shadowSize = toEven(shadowSize);
maxShadowSize = toEven(maxShadowSize);
if (shadowSize > maxShadowSize) {
shadowSize = maxShadowSize;
}
if (rawShadowSize == shadowSize && rawMaxShadowSize == maxShadowSize) {
return;
}
rawShadowSize = shadowSize;
rawMaxShadowSize = maxShadowSize;
this.shadowSize = (int) (shadowSize * SHADOW_MULTIPLIER + insetShadow + .5f);
this.maxShadowSize = maxShadowSize + insetShadow;
dirty = true;
invalidateSelf();
}
@Override
public boolean getPadding(Rect padding) {
int vOffset = (int) Math.ceil(calculateVerticalPadding(rawMaxShadowSize, cornerRadius,
addPaddingForCorners));
int hOffset = (int) Math.ceil(calculateHorizontalPadding(rawMaxShadowSize, cornerRadius,
addPaddingForCorners));
padding.set(hOffset, vOffset, hOffset, vOffset);
return true;
}
float calculateVerticalPadding(float maxShadowSize, float cornerRadius,
boolean addPaddingForCorners) {
if (addPaddingForCorners) {
return (float) (maxShadowSize * SHADOW_MULTIPLIER + (1 - COS_45) * cornerRadius);
} else {
return maxShadowSize * SHADOW_MULTIPLIER;
}
}
float calculateHorizontalPadding(float maxShadowSize, float cornerRadius,
boolean addPaddingForCorners) {
if (addPaddingForCorners) {
return (float) (maxShadowSize + (1 - COS_45) * cornerRadius);
} else {
return maxShadowSize;
}
}
@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
public void draw(Canvas canvas) {
if (dirty) {
buildComponents(getBounds());
dirty = false;
}
canvas.translate(0, rawShadowSize / 2);
drawShadow(canvas);
canvas.translate(0, -rawShadowSize / 2);
canvas.drawRoundRect(bounds, cornerRadius, cornerRadius, paint);
}
private void drawShadow(Canvas canvas) {
final float edgeShadowTop = -cornerRadius - shadowSize;
final float inset = cornerRadius + insetShadow + rawShadowSize / 2;
final boolean drawHorizontalEdges = bounds.width() - 2 * inset > 0;
final boolean drawVerticalEdges = bounds.height() - 2 * inset > 0;
// LT
int saved = canvas.save();
canvas.translate(bounds.left + inset, bounds.top + inset);
canvas.drawPath(cornerShadowPath, cornerShadowPaint);
if (drawHorizontalEdges) {
canvas.drawRect(0, edgeShadowTop,
bounds.width() - 2 * inset, -cornerRadius,
edgeShadowPaint);
}
canvas.restoreToCount(saved);
// RB
saved = canvas.save();
canvas.translate(bounds.right - inset, bounds.bottom - inset);
canvas.rotate(180f);
canvas.drawPath(cornerShadowPath, cornerShadowPaint);
if (drawHorizontalEdges) {
canvas.drawRect(0, edgeShadowTop,
bounds.width() - 2 * inset, -cornerRadius,
edgeShadowPaint);
}
canvas.restoreToCount(saved);
// LB
saved = canvas.save();
canvas.translate(bounds.left + inset, bounds.bottom - inset);
canvas.rotate(270f);
canvas.drawPath(cornerShadowPath, cornerShadowPaint);
if (drawVerticalEdges) {
canvas.drawRect(0, edgeShadowTop,
bounds.height() - 2 * inset, -cornerRadius, edgeShadowPaint);
}
canvas.restoreToCount(saved);
// RT
saved = canvas.save();
canvas.translate(bounds.right - inset, bounds.top + inset);
canvas.rotate(90f);
canvas.drawPath(cornerShadowPath, cornerShadowPaint);
if (drawVerticalEdges) {
canvas.drawRect(0, edgeShadowTop,
bounds.height() - 2 * inset, -cornerRadius, edgeShadowPaint);
}
canvas.restoreToCount(saved);
}
private void buildShadowCorners() {
RectF innerBounds = new RectF(-cornerRadius, -cornerRadius, cornerRadius, cornerRadius);
RectF outerBounds = new RectF(innerBounds);
outerBounds.inset(-shadowSize, -shadowSize);
if (cornerShadowPath == null) {
cornerShadowPath = new Path();
} else {
cornerShadowPath.reset();
}
cornerShadowPath.setFillType(Path.FillType.EVEN_ODD);
cornerShadowPath.moveTo(-cornerRadius, 0);
cornerShadowPath.rLineTo(-shadowSize, 0);
// outer arc
cornerShadowPath.arcTo(outerBounds, 180f, 90f, false);
// inner arc
cornerShadowPath.arcTo(innerBounds, 270f, -90f, false);
cornerShadowPath.close();
float startRatio = cornerRadius / (cornerRadius + shadowSize);
cornerShadowPaint.setShader(new RadialGradient(0, 0, cornerRadius + shadowSize,
shadowColors,
new float[]{0f, startRatio, 1f}
, Shader.TileMode.CLAMP));
// we offset the content shadowSize/2 pixels up to make it more realistic.
// this is why edge shadow shader has some extra space
// When drawing bottom edge shadow, we use that extra space.
edgeShadowPaint.setShader(new LinearGradient(0, -cornerRadius + shadowSize, 0,
-cornerRadius - shadowSize,
shadowColors,
new float[]{0f, .5f, 1f}, Shader.TileMode.CLAMP));
edgeShadowPaint.setAntiAlias(false);
}
private void buildComponents(Rect bounds) {
// Card is offset SHADOW_MULTIPLIER * maxShadowSize to account for the shadow shift.
// We could have different top-bottom offsets to avoid extra gap above but in that case
// center aligning Views inside the CardView would be problematic.
final float verticalOffset = rawMaxShadowSize * SHADOW_MULTIPLIER;
this.bounds.set(bounds.left + rawMaxShadowSize, bounds.top + verticalOffset,
bounds.right - rawMaxShadowSize, bounds.bottom - verticalOffset);
buildShadowCorners();
}
float getCornerRadius() {
return cornerRadius;
}
void setCornerRadius(float radius) {
if (radius < 0f) {
throw new IllegalArgumentException("Invalid radius " + radius + ". Must be >= 0");
}
radius = (int) (radius + .5f);
if (cornerRadius == radius) {
return;
}
cornerRadius = radius;
dirty = true;
invalidateSelf();
}
void getMaxShadowAndCornerPadding(Rect into) {
getPadding(into);
}
float getShadowSize() {
return rawShadowSize;
}
void setShadowSize(float size) {
setShadowSize(size, rawMaxShadowSize);
}
float getMaxShadowSize() {
return rawMaxShadowSize;
}
void setMaxShadowSize(float size) {
setShadowSize(rawShadowSize, size);
}
float getMinWidth() {
final float content = 2 *
Math.max(rawMaxShadowSize, cornerRadius + insetShadow + rawMaxShadowSize / 2);
return content + (rawMaxShadowSize + insetShadow) * 2;
}
float getMinHeight() {
final float content = 2 * Math.max(rawMaxShadowSize, cornerRadius + insetShadow
+ rawMaxShadowSize * SHADOW_MULTIPLIER / 2);
return content + (rawMaxShadowSize * SHADOW_MULTIPLIER + insetShadow) * 2;
}
}
================================================
FILE: cslibrary/src/main/java/com/hitomi/cslibrary/draw/ShadowDrawer.java
================================================
package com.hitomi.cslibrary.draw;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import com.hitomi.cslibrary.base.CrazyShadowAttr;
import com.hitomi.cslibrary.base.ShadowHandler;
/**
* Created by hitomi on 2016/10/21.
*
* 使用 Drawable 添加阴影效果,同时支持圆角模式.
*
* 因为背景的最大尺寸为原 View 的尺寸,所以设置了阴影
* 后,原 View 大小会依据配置的阴影尺寸发生变化,并且
* 原 View 的位置会发生向上的偏移来显示出阴影的效果
*/
public class ShadowDrawer implements ShadowHandler {
private CrazyShadowAttr attr;
private View view;
private Drawable orignalDrawable, shadowDrawable;
public ShadowDrawer(CrazyShadowAttr attr) {
this.attr = attr;
}
@Override
public void makeShadow(View view) {
this.view = view;
orignalDrawable = view.getBackground();
int background;
if (attr.getBackground() != 0) {
background = attr.getBackground();
} else {
ColorDrawable colorDrawable = (ColorDrawable) view.getBackground();
if (colorDrawable == null) {
background = Color.WHITE;
} else {
background = colorDrawable.getColor();
}
}
shadowDrawable = new RoundRectShadowDrawable(
background, attr.getColors(), attr.getCorner(),
attr.getShadowRadius(), attr.getShadowRadius());
view.setBackgroundDrawable(shadowDrawable);
}
@Override
public void removeShadow() {
if (view != null && view.getBackground() instanceof RoundRectShadowDrawable)
view.setBackgroundDrawable(orignalDrawable);
}
@Override
public void hideShadow() {
if (view != null && view.getBackground() instanceof RoundRectShadowDrawable)
view.setBackgroundDrawable(orignalDrawable);
}
@Override
public void showShadow() {
if (view == null || shadowDrawable == null) return ;
view.setBackgroundDrawable(shadowDrawable);
}
}
================================================
FILE: cslibrary/src/main/java/com/hitomi/cslibrary/floating/ShadowFloating.java
================================================
package com.hitomi.cslibrary.floating;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import com.hitomi.cslibrary.base.view.CornerShadowView;
import com.hitomi.cslibrary.base.CrazyShadowAttr;
import com.hitomi.cslibrary.base.CrazyShadowDirection;
import com.hitomi.cslibrary.base.view.EdgeShadowView;
import com.hitomi.cslibrary.base.ShadowHandler;
import java.util.ArrayList;
import java.util.List;
/**
* Created by hitomi on 2016/10/19.
*
* 考虑到不希望改变原 View 大小的情况,而又需要阴影效果的情况下。
* 此种方案应运而生,其原理是在原 View 根布局层面中添加阴影效果。
* 从 Z 轴的角度看就像是浮在原 View 的周围一样。不过因为与原
* View 不在一个布局层面上,所以当发生用户交互使原 View 的位置
* 发生改变后,阴影还是会留在原来的位置。
*
*/
public class ShadowFloating implements ShadowHandler {
private CrazyShadowAttr attr;
private Context context;
private View contentView;
private List shadowViewList;
private Drawable orignalDrawable;
private boolean init;
public ShadowFloating(Context context, CrazyShadowAttr attr) {
this.context = context;
this.attr = attr;
shadowViewList = new ArrayList<>();
}
private void addShadow() {
addEdgeShadow();
addCornerShadow();
}
private FrameLayout getParentContainer() {
Activity activity = (Activity) context;
return (FrameLayout) activity.findViewById(Window.ID_ANDROID_CONTENT);
}
private void addEdgeShadow() {
EdgeShadowView.Builder edgeShadowBuilder = new EdgeShadowView.Builder()
.setContext(context)
.setShadowColors(attr.getColors())
.setCornerRadius(attr.getCorner())
.setShadowRadius(attr.getShadowRadius());
FrameLayout parentLayout = getParentContainer();
if (attr.containLeft())
decorateLeft(edgeShadowBuilder, parentLayout);
if (attr.containTop())
decorateTop(edgeShadowBuilder, parentLayout);
if (attr.containRight())
decorateRight(edgeShadowBuilder, parentLayout);
if (attr.containBottom())
decorateBottom(edgeShadowBuilder, parentLayout);
}
private void decorateLeft(EdgeShadowView.Builder edgeShadowBuilder, FrameLayout parentLayout) {
FrameLayout.LayoutParams parentLp = new FrameLayout.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
float shadowSize;
float leftMargin, topMargin;
leftMargin = contentView.getLeft() - attr.getShadowRadius();
if (attr.getDirection() == CrazyShadowDirection.LEFT) {
shadowSize = contentView.getHeight();
topMargin = contentView.getTop();
} else if (attr.getDirection() == CrazyShadowDirection.ALL
|| attr.getDirection() == CrazyShadowDirection.BOTTOM_LEFT_TOP) {
shadowSize = contentView.getHeight() - 2 * attr.getCorner();
topMargin = contentView.getTop() + attr.getCorner();
} else {
shadowSize = contentView.getHeight() - attr.getCorner();
if (attr.getDirection() == CrazyShadowDirection.LEFT_TOP || attr.getDirection() == CrazyShadowDirection.LEFT_TOP_RIGHT) {
topMargin = contentView.getTop() + attr.getCorner();
} else {
topMargin = contentView.getTop();
}
}
parentLp.leftMargin = (int) leftMargin;
parentLp.topMargin = (int) topMargin;
View leftEdgeShadow = edgeShadowBuilder
.setShadowSize(shadowSize)
.setDirection(CrazyShadowDirection.LEFT)
.create();
shadowViewList.add(leftEdgeShadow);
parentLayout.addView(leftEdgeShadow, parentLp);
}
private void decorateTop(EdgeShadowView.Builder edgeShadowBuilder, FrameLayout parentLayout) {
FrameLayout.LayoutParams parentLp = new FrameLayout.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
float shadowSize;
float leftMargin, topMargin;
topMargin = contentView.getTop() - attr.getShadowRadius();
if (attr.getDirection() == CrazyShadowDirection.TOP) {
shadowSize = contentView.getWidth();
leftMargin = contentView.getLeft();
} else if (attr.getDirection() == CrazyShadowDirection.ALL
|| attr.getDirection() == CrazyShadowDirection.LEFT_TOP_RIGHT) {
shadowSize = contentView.getWidth() - 2 * attr.getCorner();
leftMargin = contentView.getLeft() + attr.getCorner();
} else {
shadowSize = contentView.getWidth() - attr.getCorner();
if (attr.getDirection() == CrazyShadowDirection.LEFT_TOP || attr.getDirection() == CrazyShadowDirection.BOTTOM_LEFT_TOP) {
leftMargin = contentView.getLeft() + attr.getCorner();
} else {
leftMargin = contentView.getLeft();
}
}
parentLp.leftMargin = (int) leftMargin;
parentLp.topMargin = (int) topMargin;
View topEdgeShadow = edgeShadowBuilder
.setShadowSize(shadowSize)
.setDirection(CrazyShadowDirection.TOP)
.create();
shadowViewList.add(topEdgeShadow);
parentLayout.addView(topEdgeShadow, parentLp);
}
private void decorateRight(EdgeShadowView.Builder edgeShadowBuilder, FrameLayout parentLayout) {
FrameLayout.LayoutParams parentLp = new FrameLayout.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
float shadowSize;
float leftMargin, topMargin;
leftMargin = contentView.getRight();
if (attr.getDirection() == CrazyShadowDirection.RIGHT) {
shadowSize = contentView.getHeight();
topMargin = contentView.getTop();
} else if (attr.getDirection() == CrazyShadowDirection.ALL
|| attr.getDirection() == CrazyShadowDirection.TOP_RIGHT_BOTTOM) {
shadowSize = contentView.getHeight() - 2 * attr.getCorner();
topMargin = contentView.getTop() + attr.getCorner();
} else {
shadowSize = contentView.getHeight() - attr.getCorner();
if (attr.getDirection() == CrazyShadowDirection.TOP_RIGHT || attr.getDirection() == CrazyShadowDirection.LEFT_TOP_RIGHT) {
topMargin = contentView.getTop() + attr.getCorner();
} else {
topMargin = contentView.getTop();
}
}
parentLp.leftMargin = (int) leftMargin;
parentLp.topMargin = (int) topMargin;
View rightEdgeShadow = edgeShadowBuilder
.setShadowSize(shadowSize)
.setDirection(CrazyShadowDirection.RIGHT)
.create();
shadowViewList.add(rightEdgeShadow);
parentLayout.addView(rightEdgeShadow, parentLp);
}
private void decorateBottom(EdgeShadowView.Builder edgeShadowBuilder, FrameLayout parentLayout) {
FrameLayout.LayoutParams parentLp = new FrameLayout.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
float shadowSize;
float leftMargin, topMargin;
topMargin = contentView.getBottom();
if (attr.getDirection() == CrazyShadowDirection.BOTTOM) {
shadowSize = contentView.getWidth();
leftMargin = contentView.getLeft();
} else if (attr.getDirection() == CrazyShadowDirection.ALL
|| attr.getDirection() == CrazyShadowDirection.RIGHT_BOTTOM_LEFT) {
shadowSize = contentView.getWidth() - 2 * attr.getCorner();
leftMargin = contentView.getLeft() + attr.getCorner();
} else {
shadowSize = contentView.getWidth() - attr.getCorner();
if (attr.getDirection() == CrazyShadowDirection.BOTTOM_LEFT || attr.getDirection() == CrazyShadowDirection.BOTTOM_LEFT_TOP) {
leftMargin = contentView.getLeft() + attr.getCorner();
} else {
leftMargin = contentView.getLeft();
}
}
parentLp.leftMargin = (int) leftMargin;
parentLp.topMargin = (int) topMargin;
View bottomEdgeShadow = edgeShadowBuilder
.setShadowSize(shadowSize)
.setDirection(CrazyShadowDirection.BOTTOM)
.create();
shadowViewList.add(bottomEdgeShadow);
parentLayout.addView(bottomEdgeShadow, parentLp);
}
private void addCornerShadow() {
CornerShadowView.Builder cornerShadowBuilder = new CornerShadowView.Builder()
.setContext(context)
.setShadowColors(attr.getColors())
.setShadowSize(attr.getShadowRadius())
.setCornerRadius(attr.getCorner());
FrameLayout parentLayout = getParentContainer();
if (attr.containLeft() && attr.containTop())
decorateLeftTop(cornerShadowBuilder, parentLayout);
if (attr.containRight() && attr.containTop())
decorateRightTop(cornerShadowBuilder, parentLayout);
if (attr.containRight() && attr.containBottom())
decorateRightBottom(cornerShadowBuilder, parentLayout);
if (attr.containLeft() && attr.containBottom())
decorateLeftBottom(cornerShadowBuilder, parentLayout);
}
private void decorateLeftTop(CornerShadowView.Builder cornerShadowBuilder, FrameLayout parentLayout) {
FrameLayout.LayoutParams parentLp = new FrameLayout.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
parentLp.leftMargin = (int) (contentView.getLeft() - attr.getShadowRadius());
parentLp.topMargin = (int) (contentView.getTop() - attr.getShadowRadius());
View leftTopCorner = cornerShadowBuilder
.setDirection(CrazyShadowDirection.LEFT_TOP)
.create();
shadowViewList.add(leftTopCorner);
parentLayout.addView(leftTopCorner, parentLp);
}
private void decorateRightTop(CornerShadowView.Builder cornerShadowBuilder, FrameLayout parentLayout) {
FrameLayout.LayoutParams parentLp = new FrameLayout.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
parentLp.leftMargin = (int) (contentView.getRight() - attr.getCorner());
parentLp.topMargin = (int) (contentView.getTop() - attr.getShadowRadius());
View rightTopCorner = cornerShadowBuilder
.setDirection(CrazyShadowDirection.TOP_RIGHT)
.create();
shadowViewList.add(rightTopCorner);
parentLayout.addView(rightTopCorner, parentLp);
}
private void decorateRightBottom(CornerShadowView.Builder cornerShadowBuilder, FrameLayout parentLayout) {
FrameLayout.LayoutParams parentLp = new FrameLayout.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
parentLp.leftMargin = (int) (contentView.getRight() - attr.getCorner());
parentLp.topMargin = (int) (contentView.getBottom() - attr.getCorner());
View rightBottomCorner = cornerShadowBuilder
.setDirection(CrazyShadowDirection.RIGHT_BOTTOM)
.create();
shadowViewList.add(rightBottomCorner);
parentLayout.addView(rightBottomCorner, parentLp);
}
private void decorateLeftBottom(CornerShadowView.Builder cornerShadowBuilder, FrameLayout parentLayout) {
FrameLayout.LayoutParams parentLp = new FrameLayout.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
parentLp.leftMargin = (int) (contentView.getLeft() - attr.getShadowRadius());
parentLp.topMargin = (int) (contentView.getBottom() - attr.getCorner());
View leftBottomCorner = cornerShadowBuilder
.setDirection(CrazyShadowDirection.BOTTOM_LEFT)
.create();
shadowViewList.add(leftBottomCorner);
parentLayout.addView(leftBottomCorner, parentLp);
}
@Override
public void makeShadow(View view) {
contentView = view;
init = true;
if (attr.getBackground() !=0) {
orignalDrawable = contentView.getBackground();
contentView.setBackgroundColor(attr.getBackground());
}
contentView.getViewTreeObserver().addOnGlobalLayoutListener(new OnMeasureListener());
}
@Override
public void removeShadow() {
FrameLayout parentLayout = getParentContainer();
for (View shadowView : shadowViewList) {
parentLayout.removeView(shadowView);
}
if (attr.getBackground() != 0) {
contentView.setBackgroundDrawable(orignalDrawable);
}
}
@Override
public void hideShadow() {
for (View shadowView : shadowViewList) {
shadowView.setAlpha(0);
}
if (attr.getBackground() != 0) {
contentView.setBackgroundDrawable(orignalDrawable);
}
}
@Override
public void showShadow() {
for (View shadowView : shadowViewList) {
shadowView.setAlpha(1);
}
if (attr.getBackground() !=0 && contentView != null) {
contentView.setBackgroundColor(attr.getBackground());
}
}
private class OnMeasureListener implements ViewTreeObserver.OnGlobalLayoutListener {
@Override
public void onGlobalLayout() {
if (init) {
addShadow();
init = false;
contentView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
}
}
================================================
FILE: cslibrary/src/main/java/com/hitomi/cslibrary/wrap/ShadowWrapper.java
================================================
package com.hitomi.cslibrary.wrap;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import com.hitomi.cslibrary.base.view.CornerShadowView;
import com.hitomi.cslibrary.base.CrazyShadowAttr;
import com.hitomi.cslibrary.base.CrazyShadowDirection;
import com.hitomi.cslibrary.base.view.EdgeShadowView;
import com.hitomi.cslibrary.base.ShadowHandler;
/**
* Created by hitomi on 2016/10/17.
*
* 使用包装的方式为 View 添加阴影。
*
* 其原理是根据配置的阴影尺寸大小来修改原 View 的大小后,在原 View 的四周
* 附加阴影效果。
*
*/
public class ShadowWrapper implements ShadowHandler {
private Context context;
private View contentView;
private RelativeLayout shadowLayout;
private Drawable orignalDrawable;
private OnMeasureListener measureListener;
private CrazyShadowAttr attr;
private boolean init;
public ShadowWrapper(Context context, CrazyShadowAttr attr) {
this.context = context;
this.attr = attr;
measureListener = new OnMeasureListener();
}
private void prepareLayout() {
ViewGroup parent = (ViewGroup) contentView.getParent();
int orignalIndex = parent.indexOfChild(contentView);
parent.removeView(contentView);
shadowLayout = new RelativeLayout(context);
ViewGroup.LayoutParams contentViewLp = contentView.getLayoutParams();
contentViewLp.width = contentView.getWidth();
contentViewLp.height = contentView.getHeight();
shadowLayout.setLayoutParams(contentViewLp);
parent.addView(shadowLayout, orignalIndex);
shadowLayout.addView(contentView, getContentViewLayoutParams());
}
@NonNull
private RelativeLayout.LayoutParams getContentViewLayoutParams() {
int width, height;
RelativeLayout.LayoutParams rlp;
int direction = attr.getDirection();
if (direction == CrazyShadowDirection.LEFT || direction == CrazyShadowDirection.RIGHT) {
width = (int) (contentView.getWidth() - attr.getShadowRadius());
height = contentView.getHeight();
rlp = new RelativeLayout.LayoutParams(width, height);
if (direction == CrazyShadowDirection.LEFT)
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
} else if (direction == CrazyShadowDirection.TOP || direction == CrazyShadowDirection.BOTTOM) {
width = contentView.getWidth();
height = (int) (contentView.getHeight() - attr.getShadowRadius());
rlp = new RelativeLayout.LayoutParams(width, height);
if (direction == CrazyShadowDirection.TOP)
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
} else if (direction == CrazyShadowDirection.LEFT_TOP || direction == CrazyShadowDirection.TOP_RIGHT) {
width = (int) (contentView.getWidth() - attr.getShadowRadius());
height = (int) (contentView.getHeight() - attr.getShadowRadius());
rlp = new RelativeLayout.LayoutParams(width, height);
if (direction == CrazyShadowDirection.LEFT_TOP) {
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
} else {
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
}
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
} else if (direction == CrazyShadowDirection.BOTTOM_LEFT || direction == CrazyShadowDirection.RIGHT_BOTTOM) {
width = (int) (contentView.getWidth() - attr.getShadowRadius());
height = (int) (contentView.getHeight() - attr.getShadowRadius());
rlp = new RelativeLayout.LayoutParams(width, height);
if (direction == CrazyShadowDirection.BOTTOM_LEFT)
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
} else if (direction == CrazyShadowDirection.BOTTOM_LEFT_TOP || direction == CrazyShadowDirection.TOP_RIGHT_BOTTOM) {
width = (int) (contentView.getWidth() - attr.getShadowRadius());
height = (int) (contentView.getHeight() - attr.getShadowRadius() * 2);
rlp = new RelativeLayout.LayoutParams(width, height);
if (direction == CrazyShadowDirection.BOTTOM_LEFT_TOP) {
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}
rlp.addRule(RelativeLayout.CENTER_VERTICAL);
} else if (direction == CrazyShadowDirection.LEFT_TOP_RIGHT || direction == CrazyShadowDirection.RIGHT_BOTTOM_LEFT) {
width = (int) (contentView.getWidth() - attr.getShadowRadius() * 2);
height = (int) (contentView.getHeight() - attr.getShadowRadius());
rlp = new RelativeLayout.LayoutParams(width, height);
if (direction == CrazyShadowDirection.LEFT_TOP_RIGHT) {
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
}
rlp.addRule(RelativeLayout.CENTER_HORIZONTAL);
} else { // All
width = (int) (contentView.getWidth() - attr.getShadowRadius() * 2);
height = (int) (contentView.getHeight() - attr.getShadowRadius() * 2);
rlp = new RelativeLayout.LayoutParams(width, height);
rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
}
return rlp;
}
private void addShadow() {
addEdgeShadow();
addCornerShadow();
}
private void addEdgeShadow() {
EdgeShadowView.Builder edgeShadowBuilder = new EdgeShadowView.Builder()
.setContext(context)
.setShadowColors(attr.getColors())
.setCornerRadius(attr.getCorner())
.setShadowRadius(attr.getShadowRadius());
if (attr.containLeft())
decorateLeft(edgeShadowBuilder);
if (attr.containTop())
decorateTop(edgeShadowBuilder);
if (attr.containRight())
decorateRight(edgeShadowBuilder);
if (attr.containBottom())
decorateBottom(edgeShadowBuilder);
}
private void decorateLeft(EdgeShadowView.Builder edgeShadowBuilder) {
RelativeLayout.LayoutParams leftRlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
float shadowSize;
if (attr.getDirection() == CrazyShadowDirection.LEFT) {
shadowSize = contentView.getHeight();
} else if (attr.getDirection() == CrazyShadowDirection.ALL
|| attr.getDirection() == CrazyShadowDirection.BOTTOM_LEFT_TOP) {
shadowSize = contentView.getHeight() - 2 * (attr.getShadowRadius() + attr.getCorner());
leftRlp.addRule(RelativeLayout.CENTER_VERTICAL);
} else {
shadowSize = contentView.getHeight() - attr.getShadowRadius() - attr.getCorner();
if (attr.getDirection() == CrazyShadowDirection.LEFT_TOP || attr.getDirection() == CrazyShadowDirection.LEFT_TOP_RIGHT) {
leftRlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
}
}
if (shadowSize <= 0) return ;
EdgeShadowView leftEdgeShadow = edgeShadowBuilder
.setShadowSize(shadowSize)
.setDirection(CrazyShadowDirection.LEFT)
.create();
shadowLayout.addView(leftEdgeShadow, leftRlp);
}
private void decorateTop(EdgeShadowView.Builder edgeShadowBuilder) {
RelativeLayout.LayoutParams topRlp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
float shadowSize;
if (attr.getDirection() == CrazyShadowDirection.TOP) {
shadowSize = contentView.getWidth();
} else if (attr.getDirection() == CrazyShadowDirection.ALL
|| attr.getDirection() == CrazyShadowDirection.LEFT_TOP_RIGHT) {
shadowSize = contentView.getWidth() - 2 * (attr.getShadowRadius() + attr.getCorner());
topRlp.addRule(RelativeLayout.CENTER_HORIZONTAL);
} else {
shadowSize = contentView.getWidth() - attr.getShadowRadius() - attr.getCorner();
if (attr.getDirection() == CrazyShadowDirection.LEFT_TOP || attr.getDirection() == CrazyShadowDirection.BOTTOM_LEFT_TOP) {
topRlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}
}
if (shadowSize <= 0) return ;
EdgeShadowView topEdgeShadow = edgeShadowBuilder
.setShadowSize(shadowSize)
.setDirection(CrazyShadowDirection.TOP)
.create();
shadowLayout.addView(topEdgeShadow, topRlp);
}
private void decorateRight(EdgeShadowView.Builder edgeShadowBuilder) {
RelativeLayout.LayoutParams rightRlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rightRlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
float shadowSize;
if (attr.getDirection() == CrazyShadowDirection.RIGHT) {
shadowSize = contentView.getHeight();
} else if (attr.getDirection() == CrazyShadowDirection.ALL
|| attr.getDirection() == CrazyShadowDirection.TOP_RIGHT_BOTTOM) {
shadowSize = contentView.getHeight() - 2 * (attr.getShadowRadius() + attr.getCorner());
rightRlp.addRule(RelativeLayout.CENTER_VERTICAL);
} else {
shadowSize = contentView.getHeight() - attr.getShadowRadius() - attr.getCorner();
if (attr.getDirection() == CrazyShadowDirection.TOP_RIGHT || attr.getDirection() == CrazyShadowDirection.LEFT_TOP_RIGHT) {
rightRlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
}
}
if (shadowSize <= 0) return ;
EdgeShadowView rightEdgeShadow = edgeShadowBuilder
.setShadowSize(shadowSize)
.setDirection(CrazyShadowDirection.RIGHT)
.create();
shadowLayout.addView(rightEdgeShadow, rightRlp);
}
private void decorateBottom(EdgeShadowView.Builder edgeShadowBuilder) {
RelativeLayout.LayoutParams bottomRlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
bottomRlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
float shadowSize;
if (attr.getDirection() == CrazyShadowDirection.BOTTOM) {
shadowSize = contentView.getWidth();
} else if (attr.getDirection() == CrazyShadowDirection.ALL
|| attr.getDirection() == CrazyShadowDirection.RIGHT_BOTTOM_LEFT) {
shadowSize = contentView.getWidth() - 2 * (attr.getShadowRadius() + attr.getCorner());
bottomRlp.addRule(RelativeLayout.CENTER_HORIZONTAL);
} else {
shadowSize = contentView.getWidth() - attr.getShadowRadius() - attr.getCorner();
if (attr.getDirection() == CrazyShadowDirection.BOTTOM_LEFT || attr.getDirection() == CrazyShadowDirection.BOTTOM_LEFT_TOP) {
bottomRlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}
}
if (shadowSize <= 0) return ;
EdgeShadowView bottomEdgeShadow = edgeShadowBuilder
.setShadowSize(shadowSize)
.setDirection(CrazyShadowDirection.BOTTOM)
.create();
shadowLayout.addView(bottomEdgeShadow, bottomRlp);
}
private void addCornerShadow() {
CornerShadowView.Builder cornerShadowBuilder = new CornerShadowView.Builder()
.setContext(context)
.setShadowColors(attr.getColors())
.setShadowSize(attr.getShadowRadius())
.setCornerRadius(attr.getCorner());
if (attr.containLeft() && attr.containTop())
decorateLeftTop(cornerShadowBuilder);
if (attr.containRight() && attr.containTop())
decorateRightTop(cornerShadowBuilder);
if (attr.containRight() && attr.containBottom())
decorateRightBottom(cornerShadowBuilder);
if (attr.containLeft() && attr.containBottom())
decorateLeftBottom(cornerShadowBuilder);
}
private void decorateLeftTop(CornerShadowView.Builder cornerShadowBuilder) {
CornerShadowView leftTopCornerShadow = cornerShadowBuilder
.setDirection(CrazyShadowDirection.LEFT_TOP)
.create();
RelativeLayout.LayoutParams leftTopRlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
, ViewGroup.LayoutParams.WRAP_CONTENT);
leftTopRlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
leftTopRlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
shadowLayout.addView(leftTopCornerShadow, leftTopRlp);
}
private void decorateRightTop(CornerShadowView.Builder cornerShadowbuilder) {
CornerShadowView rightTopCornerShadow = cornerShadowbuilder
.setDirection(CrazyShadowDirection.TOP_RIGHT)
.create();
RelativeLayout.LayoutParams rightTopRlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
, ViewGroup.LayoutParams.WRAP_CONTENT);
rightTopRlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rightTopRlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
shadowLayout.addView(rightTopCornerShadow, rightTopRlp);
}
private void decorateRightBottom(CornerShadowView.Builder cornerShadowbuilder) {
CornerShadowView RightBottomCornerShadow = cornerShadowbuilder
.setDirection(CrazyShadowDirection.RIGHT_BOTTOM)
.create();
RelativeLayout.LayoutParams rightBottomRlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
, ViewGroup.LayoutParams.WRAP_CONTENT);
rightBottomRlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rightBottomRlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
shadowLayout.addView(RightBottomCornerShadow, rightBottomRlp);
}
private void decorateLeftBottom(CornerShadowView.Builder cornerShadowbuilder) {
CornerShadowView leftBottomCornerShadow = cornerShadowbuilder
.setDirection(CrazyShadowDirection.BOTTOM_LEFT)
.create();
RelativeLayout.LayoutParams leftBottomRlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
, ViewGroup.LayoutParams.WRAP_CONTENT);
leftBottomRlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
leftBottomRlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
shadowLayout.addView(leftBottomCornerShadow, leftBottomRlp);
}
@Override
public void makeShadow(View view) {
contentView = view;
init = true;
if (attr.getBackground() != 0) {
orignalDrawable = contentView.getBackground();
contentView.setBackgroundColor(attr.getBackground());
}
contentView.getViewTreeObserver().addOnGlobalLayoutListener(measureListener);
}
@Override
public void removeShadow() {
shadowLayout.removeView(contentView);
ViewGroup parent = (ViewGroup) shadowLayout.getParent();
int orignalIndex = parent.indexOfChild(shadowLayout);
parent.removeView(shadowLayout);
contentView.setLayoutParams(shadowLayout.getLayoutParams());
parent.addView(contentView, orignalIndex);
if (attr.getBackground() != 0) {
contentView.setBackgroundDrawable(orignalDrawable);
}
}
@Override
public void hideShadow() {
setShadowViewAlpha(0);
ViewGroup.LayoutParams contentViewLp = contentView.getLayoutParams();
contentViewLp.width = shadowLayout.getLayoutParams().width;
contentViewLp.height = shadowLayout.getLayoutParams().height;
contentView.setLayoutParams(contentViewLp);
if (attr.getBackground() != 0) {
contentView.setBackgroundDrawable(orignalDrawable);
}
}
@Override
public void showShadow() {
setShadowViewAlpha(1);
contentView.setLayoutParams(getContentViewLayoutParams());
if (attr.getBackground() != 0 && contentView != null) {
contentView.setBackgroundColor(attr.getBackground());
}
}
private void setShadowViewAlpha(int alpha) {
int childCount = shadowLayout.getChildCount();
View child;
for (int i = 0; i < childCount; i++) {
child = shadowLayout.getChildAt(i);
if (child instanceof EdgeShadowView || child instanceof CornerShadowView) {
child.setAlpha(alpha);
}
}
}
private class OnMeasureListener implements ViewTreeObserver.OnGlobalLayoutListener {
@Override
public void onGlobalLayout() {
if (init) {
prepareLayout();
addShadow();
init = false;
contentView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
}
}
================================================
FILE: cslibrary/src/main/res/color/background.xml
================================================
================================================
FILE: cslibrary/src/main/res/values/strings.xml
================================================
csLibrary
================================================
FILE: gradle/wrapper/gradle-wrapper.properties
================================================
#Mon Dec 05 08:49:55 CST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
================================================
FILE: gradle.properties
================================================
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
================================================
FILE: gradlew
================================================
#!/usr/bin/env bash
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn ( ) {
echo "$*"
}
die ( ) {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
esac
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
================================================
FILE: gradlew.bat
================================================
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windowz variants
if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
goto execute
:4NT_args
@rem Get arguments from the 4NT Shell from JP Software
set CMD_LINE_ARGS=%$
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
================================================
FILE: settings.gradle
================================================
include ':app', ':cslibrary'