├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ ├── styles.xml │ │ │ │ └── colors_all.xml │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── drawable-v21 │ │ │ │ ├── item_selector.xml │ │ │ │ └── state_list_animator.xml │ │ │ ├── drawable │ │ │ │ ├── bg_round_rect_normal.xml │ │ │ │ ├── bg_round_rect_pressed.xml │ │ │ │ ├── bg_round_rect_white.xml │ │ │ │ ├── bg_round_rect_selector.xml │ │ │ │ ├── item_selector.xml │ │ │ │ └── ic_launcher_background.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── zyp │ │ │ │ └── cardview │ │ │ │ └── demo │ │ │ │ └── MainActivity.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── zyp │ │ │ └── cardview │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── zyp │ │ └── cardview │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── cardview ├── .gitignore ├── src │ └── main │ │ ├── AndroidManifest.xml │ │ ├── res │ │ ├── values-v23 │ │ │ └── values-v23.xml │ │ └── values │ │ │ └── values.xml │ │ └── java │ │ └── com │ │ └── zyp │ │ └── cardview │ │ ├── YcCardViewDelegate.java │ │ ├── YcCardViewJellybeanMr1.java │ │ ├── YcCardViewImpl.java │ │ ├── YcCardViewApi21.java │ │ ├── YcRoundRectDrawable.java │ │ ├── YcCardViewEclairMr1.java │ │ ├── YcRoundRectDrawableWithShadow.java │ │ └── YcCardView.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── shadow.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .idea ├── vcs.xml ├── modules.xml ├── runConfigurations.xml └── misc.xml ├── gradle.properties ├── .gitignore ├── gradlew.bat ├── README.md ├── gradlew └── LICENSE /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /cardview/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':cardview' 2 | -------------------------------------------------------------------------------- /shadow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzyyppqq/CardViewShadowColor/HEAD/shadow.png -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | CardViewShadowColor 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzyyppqq/CardViewShadowColor/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzyyppqq/CardViewShadowColor/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzyyppqq/CardViewShadowColor/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /cardview/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzyyppqq/CardViewShadowColor/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzyyppqq/CardViewShadowColor/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzyyppqq/CardViewShadowColor/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzyyppqq/CardViewShadowColor/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzyyppqq/CardViewShadowColor/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzyyppqq/CardViewShadowColor/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzyyppqq/CardViewShadowColor/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzyyppqq/CardViewShadowColor/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v21/item_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /cardview/src/main/res/values-v23/values-v23.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_round_rect_normal.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Sep 21 15:57:31 CST 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 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_round_rect_pressed.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_round_rect_white.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_round_rect_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/item_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/test/java/com/zyp/cardview/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.zyp.cardview; 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 | } -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyp/cardview/demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.zyp.cardview.demo; 2 | 3 | import android.annotation.TargetApi; 4 | import android.graphics.Outline; 5 | import android.os.Build; 6 | import android.os.Bundle; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.view.View; 9 | import android.view.ViewOutlineProvider; 10 | 11 | public class MainActivity extends AppCompatActivity { 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_main); 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v21/state_list_animator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | 11 | 16 | 17 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /cardview/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/zyp/cardview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.zyp.cardview; 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 | * Instrumented 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.zyp.cardview", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "com.zyp.cardview.demo" 7 | minSdkVersion 14 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | 20 | lintOptions { 21 | abortOnError false 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(include: ['*.jar'], dir: 'libs') 27 | implementation 'com.android.support:appcompat-v7:27.1.1' 28 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 29 | testImplementation 'junit:junit:4.12' 30 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 31 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 32 | implementation project(':cardview') 33 | implementation 'com.android.support:cardview-v7:27.1.1' 34 | } 35 | -------------------------------------------------------------------------------- /cardview/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 27 5 | 6 | defaultConfig { 7 | minSdkVersion 14 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | 14 | } 15 | 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | 23 | lintOptions { 24 | abortOnError false 25 | } 26 | 27 | } 28 | 29 | dependencies { 30 | implementation fileTree(dir: 'libs', include: ['*.jar']) 31 | 32 | implementation 'com.android.support:appcompat-v7:27.1.1' 33 | 34 | } 35 | 36 | apply plugin: 'com.novoda.bintray-release' 37 | publish { 38 | userOrg = rootProject.userOrg 39 | groupId = rootProject.groupId 40 | artifactId = rootProject.artifactId 41 | uploadName = rootProject.uploadName 42 | publishVersion = rootProject.publishVersion 43 | desc = rootProject.description 44 | website = rootProject.website 45 | licences = rootProject.licences 46 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # IntelliJ 36 | *.iml 37 | .idea/workspace.xml 38 | .idea/tasks.xml 39 | .idea/gradle.xml 40 | .idea/assetWizardSettings.xml 41 | .idea/dictionaries 42 | .idea/libraries 43 | .idea/caches 44 | 45 | # Keystore files 46 | # Uncomment the following line if you do not want to check your keystore files in. 47 | #*.jks 48 | 49 | # External native build folder generated in Android Studio 2.2 and later 50 | .externalNativeBuild 51 | 52 | # Google Services (e.g. APIs or Firebase) 53 | google-services.json 54 | 55 | # Freeline 56 | freeline.py 57 | freeline/ 58 | freeline_project_description.json 59 | 60 | # fastlane 61 | fastlane/report.xml 62 | fastlane/Preview.html 63 | fastlane/screenshots 64 | fastlane/test_output 65 | fastlane/readme.md 66 | -------------------------------------------------------------------------------- /cardview/src/main/java/com/zyp/cardview/YcCardViewDelegate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyp.cardview; 17 | 18 | import android.graphics.drawable.Drawable; 19 | import android.view.View; 20 | 21 | /** 22 | * Interface provided by CardView to implementations. 23 | *

24 | * Necessary to resolve circular dependency between base CardView and platform implementations. 25 | */ 26 | interface YcCardViewDelegate { 27 | void setCardBackground(Drawable drawable); 28 | Drawable getCardBackground(); 29 | boolean getUseCompatPadding(); 30 | boolean getPreventCornerOverlap(); 31 | void setShadowPadding(int left, int top, int right, int bottom); 32 | void setMinWidthHeightInternal(int width, int height); 33 | View getCardView(); 34 | } -------------------------------------------------------------------------------- /cardview/src/main/java/com/zyp/cardview/YcCardViewJellybeanMr1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyp.cardview; 17 | 18 | import android.graphics.Canvas; 19 | import android.graphics.Paint; 20 | import android.graphics.RectF; 21 | 22 | class YcCardViewJellybeanMr1 extends YcCardViewEclairMr1 { 23 | 24 | @Override 25 | public void initStatic() { 26 | YcRoundRectDrawableWithShadow.sRoundRectHelper 27 | = new YcRoundRectDrawableWithShadow.RoundRectHelper() { 28 | @Override 29 | public void drawRoundRect(Canvas canvas, RectF bounds, float cornerRadius, 30 | Paint paint) { 31 | canvas.drawRoundRect(bounds, cornerRadius, cornerRadius, paint); 32 | } 33 | }; 34 | } 35 | } -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | -------------------------------------------------------------------------------- /cardview/src/main/java/com/zyp/cardview/YcCardViewImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyp.cardview; 17 | 18 | import android.content.Context; 19 | 20 | /** 21 | * Interface for platform specific CardView implementations. 22 | */ 23 | interface YcCardViewImpl { 24 | void initialize(YcCardViewDelegate cardView, Context context, int backgroundColor, float radius, 25 | float elevation, float maxElevation, int startShadowColor, int endShadowColor); 26 | 27 | void setRadius(YcCardViewDelegate cardView, float radius); 28 | 29 | float getRadius(YcCardViewDelegate cardView); 30 | 31 | void setElevation(YcCardViewDelegate cardView, float elevation); 32 | 33 | float getElevation(YcCardViewDelegate cardView); 34 | 35 | void initStatic(); 36 | 37 | void setMaxElevation(YcCardViewDelegate cardView, float maxElevation); 38 | 39 | float getMaxElevation(YcCardViewDelegate cardView); 40 | 41 | float getMinWidth(YcCardViewDelegate cardView); 42 | 43 | float getMinHeight(YcCardViewDelegate cardView); 44 | 45 | void updatePadding(YcCardViewDelegate cardView); 46 | 47 | void onCompatPaddingChanged(YcCardViewDelegate cardView); 48 | 49 | void onPreventCornerOverlapChanged(YcCardViewDelegate cardView); 50 | 51 | void setBackgroundColor(YcCardViewDelegate cardView, int color); 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /cardview/src/main/res/values/values.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FF424242 4 | #FFFFFFFF 5 | 6 | #00ffffff 7 | #37000000 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 1dp 27 | 2dp 28 | 2dp 29 | 30 | 37 | 38 | 40 | 41 | 44 | 45 | 48 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CardViewShadowColor 2 | CardView 支持修改阴影颜色 3 | 4 | ## 基于CardView version 24.2.1 改写 5 | 6 | ![color shadow](./shadow.png) 7 | 8 | - 为了更方便的使用支持改变阴影颜色的CardView,基于CardView 24.2.1版本改写了支持改变阴影颜色的库, 为了和官方的CardView库共存,且不冲突,把所有属性、color及style的命名都加了前缀yc,当然也可以单独使用,使用方式如下 9 | 10 | builb.gradle 11 | ``` 12 | 最好和官方的CardView库一起使用,不需要特殊修改阴影颜色的地方,使用官方的CardView, 13 | 需要修改View阴影的地方才使用YcCardView 14 | implementation 'com.zyp.cardview:cardview:1.0.1' 15 | ``` 16 | 17 | pom.xml 18 | ``` 19 | 20 | com.zyp.cardview 21 | cardview 22 | 1.0.1 23 | pom 24 | 25 | 26 | ``` 27 | 28 | ``` 29 | 30 | 31 | 32 | 45 | 46 | 52 | 53 | 54 | 55 | 68 | 69 | 70 | 75 | 76 | 77 | ``` 78 | -------------------------------------------------------------------------------- /cardview/src/main/java/com/zyp/cardview/YcCardViewApi21.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyp.cardview; 17 | 18 | import android.content.Context; 19 | import android.util.Log; 20 | import android.view.View; 21 | 22 | class YcCardViewApi21 implements YcCardViewImpl { 23 | 24 | @Override 25 | public void initialize(YcCardViewDelegate cardView, Context context, int backgroundColor, 26 | float radius, float elevation, float maxElevation, int startShadowColor, int endShadowColor) { 27 | Log.e("AAA","CardViewApi21"); 28 | final YcRoundRectDrawable background = new YcRoundRectDrawable(backgroundColor, radius); 29 | cardView.setCardBackground(background); 30 | 31 | View view = cardView.getCardView(); 32 | view.setClipToOutline(true); 33 | view.setElevation(elevation); 34 | setMaxElevation(cardView, maxElevation); 35 | } 36 | 37 | @Override 38 | public void setRadius(YcCardViewDelegate cardView, float radius) { 39 | getCardBackground(cardView).setRadius(radius); 40 | } 41 | 42 | @Override 43 | public void initStatic() { 44 | } 45 | 46 | @Override 47 | public void setMaxElevation(YcCardViewDelegate cardView, float maxElevation) { 48 | getCardBackground(cardView).setPadding(maxElevation, 49 | cardView.getUseCompatPadding(), cardView.getPreventCornerOverlap()); 50 | updatePadding(cardView); 51 | } 52 | 53 | @Override 54 | public float getMaxElevation(YcCardViewDelegate cardView) { 55 | return getCardBackground(cardView).getPadding(); 56 | } 57 | 58 | @Override 59 | public float getMinWidth(YcCardViewDelegate cardView) { 60 | return getRadius(cardView) * 2; 61 | } 62 | 63 | @Override 64 | public float getMinHeight(YcCardViewDelegate cardView) { 65 | return getRadius(cardView) * 2; 66 | } 67 | 68 | @Override 69 | public float getRadius(YcCardViewDelegate cardView) { 70 | return getCardBackground(cardView).getRadius(); 71 | } 72 | 73 | @Override 74 | public void setElevation(YcCardViewDelegate cardView, float elevation) { 75 | cardView.getCardView().setElevation(elevation); 76 | } 77 | 78 | @Override 79 | public float getElevation(YcCardViewDelegate cardView) { 80 | return cardView.getCardView().getElevation(); 81 | } 82 | 83 | @Override 84 | public void updatePadding(YcCardViewDelegate cardView) { 85 | if (!cardView.getUseCompatPadding()) { 86 | cardView.setShadowPadding(0, 0, 0, 0); 87 | return; 88 | } 89 | float elevation = getMaxElevation(cardView); 90 | final float radius = getRadius(cardView); 91 | int hPadding = (int) Math.ceil(YcRoundRectDrawableWithShadow 92 | .calculateHorizontalPadding(elevation, radius, cardView.getPreventCornerOverlap())); 93 | int vPadding = (int) Math.ceil(YcRoundRectDrawableWithShadow 94 | .calculateVerticalPadding(elevation, radius, cardView.getPreventCornerOverlap())); 95 | cardView.setShadowPadding(hPadding, vPadding, hPadding, vPadding); 96 | } 97 | 98 | @Override 99 | public void onCompatPaddingChanged(YcCardViewDelegate cardView) { 100 | setMaxElevation(cardView, getMaxElevation(cardView)); 101 | } 102 | 103 | @Override 104 | public void onPreventCornerOverlapChanged(YcCardViewDelegate cardView) { 105 | setMaxElevation(cardView, getMaxElevation(cardView)); 106 | } 107 | 108 | @Override 109 | public void setBackgroundColor(YcCardViewDelegate cardView, int color) { 110 | getCardBackground(cardView).setColor(color); 111 | } 112 | 113 | private YcRoundRectDrawable getCardBackground(YcCardViewDelegate cardView) { 114 | return ((YcRoundRectDrawable) cardView.getCardBackground()); 115 | } 116 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 26 | 27 | 33 | 34 | 35 | 36 | 49 | 50 | 55 | 56 | 57 | 58 | 67 | 68 | 76 | 77 | 78 | 87 | 88 | 96 | 97 | 98 | 99 | 111 | 112 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 11 | 16 | 21 | 26 | 31 | 36 | 41 | 46 | 51 | 56 | 61 | 66 | 71 | 76 | 81 | 86 | 91 | 96 | 101 | 106 | 111 | 116 | 121 | 126 | 131 | 136 | 141 | 146 | 151 | 156 | 161 | 166 | 171 | 172 | -------------------------------------------------------------------------------- /cardview/src/main/java/com/zyp/cardview/YcRoundRectDrawable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyp.cardview; 17 | 18 | import android.content.res.ColorStateList; 19 | import android.graphics.Canvas; 20 | import android.graphics.Color; 21 | import android.graphics.ColorFilter; 22 | import android.graphics.Outline; 23 | import android.graphics.Paint; 24 | import android.graphics.PixelFormat; 25 | import android.graphics.PorterDuff; 26 | import android.graphics.PorterDuffColorFilter; 27 | import android.graphics.Rect; 28 | import android.graphics.RectF; 29 | import android.graphics.drawable.Drawable; 30 | import android.util.Log; 31 | 32 | 33 | /** 34 | * Very simple drawable that draws a rounded rectangle background with arbitrary corners and also 35 | * reports proper outline for Lollipop. 36 | *

37 | * Simpler and uses less resources compared to GradientDrawable or ShapeDrawable. 38 | */ 39 | class YcRoundRectDrawable extends Drawable { 40 | private float mRadius; 41 | private final Paint mPaint; 42 | private final RectF mBoundsF; 43 | private final Rect mBoundsI; 44 | private float mPadding; 45 | private boolean mInsetForPadding = false; 46 | private boolean mInsetForRadius = true; 47 | 48 | private PorterDuffColorFilter mTintFilter; 49 | private ColorStateList mTint; 50 | private PorterDuff.Mode mTintMode = PorterDuff.Mode.SRC_IN; 51 | 52 | public YcRoundRectDrawable(int backgroundColor, float radius) { 53 | Log.e("AAA","RoundRectDrawable"); 54 | mRadius = radius; 55 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); 56 | mPaint.setColor(backgroundColor); 57 | mBoundsF = new RectF(); 58 | mBoundsI = new Rect(); 59 | } 60 | 61 | void setPadding(float padding, boolean insetForPadding, boolean insetForRadius) { 62 | if (padding == mPadding && mInsetForPadding == insetForPadding && 63 | mInsetForRadius == insetForRadius) { 64 | return; 65 | } 66 | mPadding = padding; 67 | mInsetForPadding = insetForPadding; 68 | mInsetForRadius = insetForRadius; 69 | updateBounds(null); 70 | invalidateSelf(); 71 | } 72 | 73 | float getPadding() { 74 | return mPadding; 75 | } 76 | 77 | @Override 78 | public void draw(Canvas canvas) { 79 | final Paint paint = mPaint; 80 | 81 | final boolean clearColorFilter; 82 | if (mTintFilter != null && paint.getColorFilter() == null) { 83 | paint.setColorFilter(mTintFilter); 84 | clearColorFilter = true; 85 | } else { 86 | clearColorFilter = false; 87 | } 88 | 89 | canvas.drawRoundRect(mBoundsF, mRadius, mRadius, paint); 90 | 91 | if (clearColorFilter) { 92 | paint.setColorFilter(null); 93 | } 94 | } 95 | 96 | private void updateBounds(Rect bounds) { 97 | if (bounds == null) { 98 | bounds = getBounds(); 99 | } 100 | mBoundsF.set(bounds.left, bounds.top, bounds.right, bounds.bottom); 101 | mBoundsI.set(bounds); 102 | if (mInsetForPadding) { 103 | float vInset = YcRoundRectDrawableWithShadow.calculateVerticalPadding(mPadding, mRadius, mInsetForRadius); 104 | float hInset = YcRoundRectDrawableWithShadow.calculateHorizontalPadding(mPadding, mRadius, mInsetForRadius); 105 | mBoundsI.inset((int) Math.ceil(hInset), (int) Math.ceil(vInset)); 106 | // to make sure they have same bounds. 107 | mBoundsF.set(mBoundsI); 108 | } 109 | } 110 | 111 | @Override 112 | protected void onBoundsChange(Rect bounds) { 113 | super.onBoundsChange(bounds); 114 | updateBounds(bounds); 115 | } 116 | 117 | @Override 118 | public void getOutline(Outline outline) { 119 | outline.setRoundRect(mBoundsI, mRadius); 120 | } 121 | 122 | void setRadius(float radius) { 123 | if (radius == mRadius) { 124 | return; 125 | } 126 | mRadius = radius; 127 | updateBounds(null); 128 | invalidateSelf(); 129 | } 130 | 131 | @Override 132 | public void setAlpha(int alpha) { 133 | mPaint.setAlpha(alpha); 134 | } 135 | 136 | @Override 137 | public void setColorFilter(ColorFilter cf) { 138 | mPaint.setColorFilter(cf); 139 | } 140 | 141 | @Override 142 | public int getOpacity() { 143 | return PixelFormat.TRANSLUCENT; 144 | } 145 | 146 | public float getRadius() { 147 | return mRadius; 148 | } 149 | 150 | public void setColor(int color) { 151 | mPaint.setColor(color); 152 | invalidateSelf(); 153 | } 154 | 155 | @Override 156 | public void setTintList(ColorStateList tint) { 157 | mTint = tint; 158 | mTintFilter = createTintFilter(mTint, mTintMode); 159 | invalidateSelf(); 160 | } 161 | 162 | @Override 163 | public void setTintMode(PorterDuff.Mode tintMode) { 164 | mTintMode = tintMode; 165 | mTintFilter = createTintFilter(mTint, mTintMode); 166 | invalidateSelf(); 167 | } 168 | 169 | @Override 170 | protected boolean onStateChange(int[] stateSet) { 171 | if (mTint != null && mTintMode != null) { 172 | mTintFilter = createTintFilter(mTint, mTintMode); 173 | return true; 174 | } 175 | return false; 176 | } 177 | 178 | @Override 179 | public boolean isStateful() { 180 | return (mTint != null && mTint.isStateful()) || super.isStateful(); 181 | } 182 | 183 | /** 184 | * Ensures the tint filter is consistent with the current tint color and 185 | * mode. 186 | */ 187 | private PorterDuffColorFilter createTintFilter(ColorStateList tint, PorterDuff.Mode tintMode) { 188 | if (tint == null || tintMode == null) { 189 | return null; 190 | } 191 | final int color = tint.getColorForState(getState(), Color.TRANSPARENT); 192 | return new PorterDuffColorFilter(color, tintMode); 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /cardview/src/main/java/com/zyp/cardview/YcCardViewEclairMr1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyp.cardview; 17 | 18 | import android.content.Context; 19 | import android.graphics.Canvas; 20 | import android.graphics.Paint; 21 | import android.graphics.Rect; 22 | import android.graphics.RectF; 23 | 24 | class YcCardViewEclairMr1 implements YcCardViewImpl { 25 | 26 | final RectF sCornerRect = new RectF(); 27 | 28 | @Override 29 | public void initStatic() { 30 | // Draws a round rect using 7 draw operations. This is faster than using 31 | // canvas.drawRoundRect before JBMR1 because API 11-16 used alpha mask textures to draw 32 | // shapes. 33 | YcRoundRectDrawableWithShadow.sRoundRectHelper = 34 | new YcRoundRectDrawableWithShadow.RoundRectHelper() { 35 | @Override 36 | public void drawRoundRect(Canvas canvas, RectF bounds, float cornerRadius, 37 | Paint paint) { 38 | final float twoRadius = cornerRadius * 2; 39 | final float innerWidth = bounds.width() - twoRadius - 1; 40 | final float innerHeight = bounds.height() - twoRadius - 1; 41 | if (cornerRadius >= 1f) { 42 | // increment corner radius to account for half pixels. 43 | float roundedCornerRadius = cornerRadius + .5f; 44 | sCornerRect.set(-roundedCornerRadius, -roundedCornerRadius, roundedCornerRadius, 45 | roundedCornerRadius); 46 | int saved = canvas.save(); 47 | canvas.translate(bounds.left + roundedCornerRadius, 48 | bounds.top + roundedCornerRadius); 49 | canvas.drawArc(sCornerRect, 180, 90, true, paint); 50 | canvas.translate(innerWidth, 0); 51 | canvas.rotate(90); 52 | canvas.drawArc(sCornerRect, 180, 90, true, paint); 53 | canvas.translate(innerHeight, 0); 54 | canvas.rotate(90); 55 | canvas.drawArc(sCornerRect, 180, 90, true, paint); 56 | canvas.translate(innerWidth, 0); 57 | canvas.rotate(90); 58 | canvas.drawArc(sCornerRect, 180, 90, true, paint); 59 | canvas.restoreToCount(saved); 60 | //draw top and bottom pieces 61 | canvas.drawRect(bounds.left + roundedCornerRadius - 1f, bounds.top, 62 | bounds.right - roundedCornerRadius + 1f, 63 | bounds.top + roundedCornerRadius, paint); 64 | canvas.drawRect(bounds.left + roundedCornerRadius - 1f, 65 | bounds.bottom - roundedCornerRadius + 1f, 66 | bounds.right - roundedCornerRadius + 1f, bounds.bottom, paint); 67 | } 68 | // center 69 | canvas.drawRect(bounds.left, bounds.top + Math.max(0, cornerRadius - 1f), 70 | bounds.right, bounds.bottom - cornerRadius + 1f, paint); 71 | } 72 | }; 73 | } 74 | 75 | @Override 76 | public void initialize(YcCardViewDelegate cardView, Context context, int backgroundColor, 77 | float radius, float elevation, float maxElevation, int startShadowColor, int endShadowColor) { 78 | YcRoundRectDrawableWithShadow background = createBackground(context, backgroundColor, radius, 79 | elevation, maxElevation,startShadowColor,endShadowColor); 80 | background.setAddPaddingForCorners(cardView.getPreventCornerOverlap()); 81 | cardView.setCardBackground(background); 82 | updatePadding(cardView); 83 | } 84 | 85 | private YcRoundRectDrawableWithShadow createBackground(Context context, int backgroundColor, 86 | float radius, float elevation, float maxElevation, int startShadowColor, int endShadowColor) { 87 | return new YcRoundRectDrawableWithShadow(context.getResources(), backgroundColor, radius, 88 | elevation, maxElevation,startShadowColor,endShadowColor); 89 | } 90 | 91 | @Override 92 | public void updatePadding(YcCardViewDelegate cardView) { 93 | Rect shadowPadding = new Rect(); 94 | getShadowBackground(cardView).getMaxShadowAndCornerPadding(shadowPadding); 95 | cardView.setMinWidthHeightInternal((int) Math.ceil(getMinWidth(cardView)), 96 | (int) Math.ceil(getMinHeight(cardView))); 97 | cardView.setShadowPadding(shadowPadding.left, shadowPadding.top, 98 | shadowPadding.right, shadowPadding.bottom); 99 | } 100 | 101 | @Override 102 | public void onCompatPaddingChanged(YcCardViewDelegate cardView) { 103 | // NO OP 104 | } 105 | 106 | @Override 107 | public void onPreventCornerOverlapChanged(YcCardViewDelegate cardView) { 108 | getShadowBackground(cardView).setAddPaddingForCorners(cardView.getPreventCornerOverlap()); 109 | updatePadding(cardView); 110 | } 111 | 112 | @Override 113 | public void setBackgroundColor(YcCardViewDelegate cardView, int color) { 114 | getShadowBackground(cardView).setColor(color); 115 | } 116 | 117 | @Override 118 | public void setRadius(YcCardViewDelegate cardView, float radius) { 119 | getShadowBackground(cardView).setCornerRadius(radius); 120 | updatePadding(cardView); 121 | } 122 | 123 | @Override 124 | public float getRadius(YcCardViewDelegate cardView) { 125 | return getShadowBackground(cardView).getCornerRadius(); 126 | } 127 | 128 | @Override 129 | public void setElevation(YcCardViewDelegate cardView, float elevation) { 130 | getShadowBackground(cardView).setShadowSize(elevation); 131 | } 132 | 133 | @Override 134 | public float getElevation(YcCardViewDelegate cardView) { 135 | return getShadowBackground(cardView).getShadowSize(); 136 | } 137 | 138 | @Override 139 | public void setMaxElevation(YcCardViewDelegate cardView, float maxElevation) { 140 | getShadowBackground(cardView).setMaxShadowSize(maxElevation); 141 | updatePadding(cardView); 142 | } 143 | 144 | @Override 145 | public float getMaxElevation(YcCardViewDelegate cardView) { 146 | return getShadowBackground(cardView).getMaxShadowSize(); 147 | } 148 | 149 | @Override 150 | public float getMinWidth(YcCardViewDelegate cardView) { 151 | return getShadowBackground(cardView).getMinWidth(); 152 | } 153 | 154 | @Override 155 | public float getMinHeight(YcCardViewDelegate cardView) { 156 | return getShadowBackground(cardView).getMinHeight(); 157 | } 158 | 159 | private YcRoundRectDrawableWithShadow getShadowBackground(YcCardViewDelegate cardView) { 160 | return ((YcRoundRectDrawableWithShadow) cardView.getCardBackground()); 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors_all.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ffffff 4 | #fffff0 5 | #ffffe0 6 | #ffff00 7 | #fffafa 8 | #fffaf0 9 | #fffacd 10 | #fff8dc 11 | #fff5ee 12 | #fff0f5 13 | #ffefd5 14 | #ffebcd 15 | #ffe4e1 16 | #ffe4c4 17 | #ffe4b5 18 | #ffdead 19 | #ffdab9 20 | #ffd700 21 | #ffc0cb 22 | #ffb6c1 23 | #ffa500 24 | #ffa07a 25 | #ff8c00 26 | #ff7f50 27 | #ff69b4 28 | #ff6347 29 | #ff4500 30 | #ff1493 31 | #ff00ff 32 | #ff00ff 33 | #ff0000 34 | #fdf5e6 35 | #fafad2 36 | #faf0e6 37 | #faebd7 38 | #fa8072 39 | #f8f8ff 40 | #f5fffa 41 | #f5f5f5 42 | #f5f5dc 43 | #f5deb3 44 | #f4a460 45 | #f0ffff 46 | #f0fff0 47 | #f0f8ff 48 | #f0e68c 49 | #f08080 50 | #eee8aa 51 | #ee82ee 52 | #e9967a 53 | #e6e6fa 54 | #e0ffff 55 | #deb887 56 | #dda0dd 57 | #dcdcdc 58 | #dc143c 59 | #db7093 60 | #daa520 61 | #da70d6 62 | #d8bfd8 63 | #d3d3d3 64 | #d3d3d3 65 | #d2b48c 66 | #d2691e 67 | #cd853f 68 | #cd5c5c 69 | #c71585 70 | #c0c0c0 71 | #bdb76b 72 | #bc8f8f 73 | #ba55d3 74 | #b8860b 75 | #b22222 76 | #b0e0e6 77 | #b0c4de 78 | #afeeee 79 | #adff2f 80 | #add8e6 81 | #a9a9a9 82 | #a9a9a9 83 | #a52a2a 84 | #a0522d 85 | #9932cc 86 | #98fb98 87 | #9400d3 88 | #9370db 89 | #90ee90 90 | #8fbc8f 91 | #8b4513 92 | #8b008b 93 | #8b0000 94 | #8a2be2 95 | #87cefa 96 | #87ceeb 97 | #808080 98 | #808080 99 | #808000 100 | #800080 101 | #800000 102 | #7fffd4 103 | #7fff00 104 | #7cfc00 105 | #7b68ee 106 | #778899 107 | #778899 108 | #708090 109 | #708090 110 | #6b8e23 111 | #6a5acd 112 | #696969 113 | #696969 114 | #66cdaa 115 | #6495ed 116 | #5f9ea0 117 | #556b2f 118 | #4b0082 119 | #48d1cc 120 | #483d8b 121 | #4682b4 122 | #4169e1 123 | #40e0d0 124 | #3cb371 125 | #32cd32 126 | #2f4f4f 127 | #2f4f4f 128 | #2e8b57 129 | #228b22 130 | #20b2aa 131 | #1e90ff 132 | #191970 133 | #00ffff 134 | #00ffff 135 | #00ff7f 136 | #00ff00 137 | #00fa9a 138 | #00ced1 139 | #00bfff 140 | #008b8b 141 | #008080 142 | #008000 143 | #006400 144 | #0000ff 145 | #0000cd 146 | #00008b 147 | #000080 148 | #000000 149 | 150 | #1f000000 151 | #1f000000 152 | #00000000 153 | 154 | --------------------- 155 | 156 | 本文来自 Fade龖龘 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq_36255612/article/details/53606305?utm_source=copy 157 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /cardview/src/main/java/com/zyp/cardview/YcRoundRectDrawableWithShadow.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.zyp.cardview; 17 | 18 | import android.content.res.Resources; 19 | import android.graphics.Canvas; 20 | import android.graphics.ColorFilter; 21 | import android.graphics.LinearGradient; 22 | import android.graphics.Paint; 23 | import android.graphics.Path; 24 | import android.graphics.PixelFormat; 25 | import android.graphics.RadialGradient; 26 | import android.graphics.Rect; 27 | import android.graphics.RectF; 28 | import android.graphics.Shader; 29 | import android.graphics.drawable.Drawable; 30 | import android.util.Log; 31 | 32 | /** 33 | * A rounded rectangle drawable which also includes a shadow around. 34 | */ 35 | class YcRoundRectDrawableWithShadow extends Drawable { 36 | // used to calculate content padding 37 | final static double COS_45 = Math.cos(Math.toRadians(45)); 38 | 39 | final static float SHADOW_MULTIPLIER = 1.5f; 40 | 41 | final int mInsetShadow; // extra shadow to avoid gaps between card and shadow 42 | 43 | /* 44 | * This helper is set by CardView implementations. 45 | *

46 | * Prior to API 17, canvas.drawRoundRect is expensive; which is why we need this interface 47 | * to draw efficient rounded rectangles before 17. 48 | * */ 49 | static RoundRectHelper sRoundRectHelper; 50 | 51 | Paint mPaint; 52 | 53 | Paint mCornerShadowPaint; 54 | 55 | Paint mEdgeShadowPaint; 56 | 57 | final RectF mCardBounds; 58 | 59 | float mCornerRadius; 60 | 61 | Path mCornerShadowPath; 62 | 63 | // updated value with inset 64 | float mMaxShadowSize; 65 | 66 | // actual value set by developer 67 | float mRawMaxShadowSize; 68 | 69 | // multiplied value to account for shadow offset 70 | float mShadowSize; 71 | 72 | // actual value set by developer 73 | float mRawShadowSize; 74 | 75 | private boolean mDirty = true; 76 | 77 | private int mShadowStartColor; 78 | 79 | private int mShadowEndColor; 80 | 81 | private boolean mAddPaddingForCorners = true; 82 | 83 | /** 84 | * If shadow size is set to a value above max shadow, we print a warning 85 | */ 86 | private boolean mPrintedShadowClipWarning = false; 87 | 88 | YcRoundRectDrawableWithShadow(Resources resources, int backgroundColor, float radius, 89 | float shadowSize, float maxShadowSize, int startShadowColor, int endShadowColor) { 90 | Log.e("AAA","RoundRectDrawableWithShadow"); 91 | if (startShadowColor != 0) { 92 | mShadowStartColor = startShadowColor; 93 | }else { 94 | mShadowStartColor = resources.getColor(R.color.yc_cardview_shadow_start_color); 95 | } 96 | if (endShadowColor != 0) { 97 | mShadowEndColor = endShadowColor; 98 | }else { 99 | mShadowEndColor = resources.getColor(R.color.yc_cardview_shadow_end_color); 100 | } 101 | mInsetShadow = resources.getDimensionPixelSize(R.dimen.yc_cardview_compat_inset_shadow); 102 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); 103 | mPaint.setColor(backgroundColor); 104 | mCornerShadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); 105 | mCornerShadowPaint.setStyle(Paint.Style.FILL); 106 | mCornerRadius = (int) (radius + .5f); 107 | mCardBounds = new RectF(); 108 | mEdgeShadowPaint = new Paint(mCornerShadowPaint); 109 | mEdgeShadowPaint.setAntiAlias(false); 110 | setShadowSize(shadowSize, maxShadowSize); 111 | } 112 | 113 | /** 114 | * Casts the value to an even integer. 115 | */ 116 | private int toEven(float value) { 117 | int i = (int) (value + .5f); 118 | if (i % 2 == 1) { 119 | return i - 1; 120 | } 121 | return i; 122 | } 123 | 124 | public void setAddPaddingForCorners(boolean addPaddingForCorners) { 125 | mAddPaddingForCorners = addPaddingForCorners; 126 | invalidateSelf(); 127 | } 128 | 129 | @Override 130 | public void setAlpha(int alpha) { 131 | mPaint.setAlpha(alpha); 132 | mCornerShadowPaint.setAlpha(alpha); 133 | mEdgeShadowPaint.setAlpha(alpha); 134 | } 135 | 136 | @Override 137 | protected void onBoundsChange(Rect bounds) { 138 | super.onBoundsChange(bounds); 139 | mDirty = true; 140 | } 141 | 142 | void setShadowSize(float shadowSize, float maxShadowSize) { 143 | if (shadowSize < 0f) { 144 | throw new IllegalArgumentException("Invalid shadow size " + shadowSize + 145 | ". Must be >= 0"); 146 | } 147 | if (maxShadowSize < 0f) { 148 | throw new IllegalArgumentException("Invalid max shadow size " + maxShadowSize + 149 | ". Must be >= 0"); 150 | } 151 | shadowSize = toEven(shadowSize); 152 | maxShadowSize = toEven(maxShadowSize); 153 | if (shadowSize > maxShadowSize) { 154 | shadowSize = maxShadowSize; 155 | if (!mPrintedShadowClipWarning) { 156 | mPrintedShadowClipWarning = true; 157 | } 158 | } 159 | if (mRawShadowSize == shadowSize && mRawMaxShadowSize == maxShadowSize) { 160 | return; 161 | } 162 | mRawShadowSize = shadowSize; 163 | mRawMaxShadowSize = maxShadowSize; 164 | mShadowSize = (int)(shadowSize * SHADOW_MULTIPLIER + mInsetShadow + .5f); 165 | mMaxShadowSize = maxShadowSize + mInsetShadow; 166 | mDirty = true; 167 | invalidateSelf(); 168 | } 169 | 170 | @Override 171 | public boolean getPadding(Rect padding) { 172 | int vOffset = (int) Math.ceil(calculateVerticalPadding(mRawMaxShadowSize, mCornerRadius, 173 | mAddPaddingForCorners)); 174 | int hOffset = (int) Math.ceil(calculateHorizontalPadding(mRawMaxShadowSize, mCornerRadius, 175 | mAddPaddingForCorners)); 176 | padding.set(hOffset, vOffset, hOffset, vOffset); 177 | return true; 178 | } 179 | 180 | static float calculateVerticalPadding(float maxShadowSize, float cornerRadius, 181 | boolean addPaddingForCorners) { 182 | if (addPaddingForCorners) { 183 | return (float) (maxShadowSize * SHADOW_MULTIPLIER + (1 - COS_45) * cornerRadius); 184 | } else { 185 | return maxShadowSize * SHADOW_MULTIPLIER; 186 | } 187 | } 188 | 189 | static float calculateHorizontalPadding(float maxShadowSize, float cornerRadius, 190 | boolean addPaddingForCorners) { 191 | if (addPaddingForCorners) { 192 | return (float) (maxShadowSize + (1 - COS_45) * cornerRadius); 193 | } else { 194 | return maxShadowSize; 195 | } 196 | } 197 | 198 | @Override 199 | public void setColorFilter(ColorFilter cf) { 200 | mPaint.setColorFilter(cf); 201 | } 202 | 203 | @Override 204 | public int getOpacity() { 205 | return PixelFormat.TRANSLUCENT; 206 | } 207 | 208 | void setCornerRadius(float radius) { 209 | if (radius < 0f) { 210 | throw new IllegalArgumentException("Invalid radius " + radius + 211 | ". Must be >= 0"); 212 | } 213 | radius = (int) (radius + .5f); 214 | if (mCornerRadius == radius) { 215 | return; 216 | } 217 | mCornerRadius = radius; 218 | mDirty = true; 219 | invalidateSelf(); 220 | } 221 | 222 | @Override 223 | public void draw(Canvas canvas) { 224 | if (mDirty) { 225 | buildComponents(getBounds()); 226 | mDirty = false; 227 | } 228 | canvas.translate(0, mRawShadowSize / 2); 229 | drawShadow(canvas); 230 | canvas.translate(0, -mRawShadowSize / 2); 231 | sRoundRectHelper.drawRoundRect(canvas, mCardBounds, mCornerRadius, mPaint); 232 | } 233 | 234 | private void drawShadow(Canvas canvas) { 235 | final float edgeShadowTop = -mCornerRadius - mShadowSize; 236 | final float inset = mCornerRadius + mInsetShadow + mRawShadowSize / 2; 237 | final boolean drawHorizontalEdges = mCardBounds.width() - 2 * inset > 0; 238 | final boolean drawVerticalEdges = mCardBounds.height() - 2 * inset > 0; 239 | // LT 240 | int saved = canvas.save(); 241 | canvas.translate(mCardBounds.left + inset, mCardBounds.top + inset); 242 | canvas.drawPath(mCornerShadowPath, mCornerShadowPaint); 243 | if (drawHorizontalEdges) { 244 | canvas.drawRect(0, edgeShadowTop, 245 | mCardBounds.width() - 2 * inset, -mCornerRadius, 246 | mEdgeShadowPaint); 247 | } 248 | canvas.restoreToCount(saved); 249 | // RB 250 | saved = canvas.save(); 251 | canvas.translate(mCardBounds.right - inset, mCardBounds.bottom - inset); 252 | canvas.rotate(180f); 253 | canvas.drawPath(mCornerShadowPath, mCornerShadowPaint); 254 | if (drawHorizontalEdges) { 255 | canvas.drawRect(0, edgeShadowTop, 256 | mCardBounds.width() - 2 * inset, -mCornerRadius + mShadowSize, 257 | mEdgeShadowPaint); 258 | } 259 | canvas.restoreToCount(saved); 260 | // LB 261 | saved = canvas.save(); 262 | canvas.translate(mCardBounds.left + inset, mCardBounds.bottom - inset); 263 | canvas.rotate(270f); 264 | canvas.drawPath(mCornerShadowPath, mCornerShadowPaint); 265 | if (drawVerticalEdges) { 266 | canvas.drawRect(0, edgeShadowTop, 267 | mCardBounds.height() - 2 * inset, -mCornerRadius, mEdgeShadowPaint); 268 | } 269 | canvas.restoreToCount(saved); 270 | // RT 271 | saved = canvas.save(); 272 | canvas.translate(mCardBounds.right - inset, mCardBounds.top + inset); 273 | canvas.rotate(90f); 274 | canvas.drawPath(mCornerShadowPath, mCornerShadowPaint); 275 | if (drawVerticalEdges) { 276 | canvas.drawRect(0, edgeShadowTop, 277 | mCardBounds.height() - 2 * inset, -mCornerRadius, mEdgeShadowPaint); 278 | } 279 | canvas.restoreToCount(saved); 280 | } 281 | 282 | private void buildShadowCorners() { 283 | RectF innerBounds = new RectF(-mCornerRadius, -mCornerRadius, mCornerRadius, mCornerRadius); 284 | RectF outerBounds = new RectF(innerBounds); 285 | outerBounds.inset(-mShadowSize, -mShadowSize); 286 | 287 | if (mCornerShadowPath == null) { 288 | mCornerShadowPath = new Path(); 289 | } else { 290 | mCornerShadowPath.reset(); 291 | } 292 | mCornerShadowPath.setFillType(Path.FillType.EVEN_ODD); 293 | mCornerShadowPath.moveTo(-mCornerRadius, 0); 294 | mCornerShadowPath.rLineTo(-mShadowSize, 0); 295 | // outer arc 296 | mCornerShadowPath.arcTo(outerBounds, 180f, 90f, false); 297 | // inner arc 298 | mCornerShadowPath.arcTo(innerBounds, 270f, -90f, false); 299 | mCornerShadowPath.close(); 300 | float startRatio = mCornerRadius / (mCornerRadius + mShadowSize); 301 | mCornerShadowPaint.setShader(new RadialGradient(0, 0, mCornerRadius + mShadowSize, 302 | new int[]{mShadowStartColor, mShadowStartColor, mShadowEndColor}, 303 | new float[]{0f, startRatio, 1f} 304 | , Shader.TileMode.CLAMP)); 305 | 306 | // we offset the content shadowSize/2 pixels up to make it more realistic. 307 | // this is why edge shadow shader has some extra space 308 | // When drawing bottom edge shadow, we use that extra space. 309 | mEdgeShadowPaint.setShader(new LinearGradient(0, -mCornerRadius + mShadowSize, 0, 310 | -mCornerRadius - mShadowSize, 311 | new int[]{mShadowStartColor, mShadowStartColor, mShadowEndColor}, 312 | new float[]{0f, .5f, 1f}, Shader.TileMode.CLAMP)); 313 | mEdgeShadowPaint.setAntiAlias(false); 314 | } 315 | 316 | private void buildComponents(Rect bounds) { 317 | // Card is offset SHADOW_MULTIPLIER * maxShadowSize to account for the shadow shift. 318 | // We could have different top-bottom offsets to avoid extra gap above but in that case 319 | // center aligning Views inside the CardView would be problematic. 320 | final float verticalOffset = mRawMaxShadowSize * SHADOW_MULTIPLIER; 321 | mCardBounds.set(bounds.left + mRawMaxShadowSize, bounds.top + verticalOffset, 322 | bounds.right - mRawMaxShadowSize, bounds.bottom - verticalOffset); 323 | buildShadowCorners(); 324 | } 325 | 326 | float getCornerRadius() { 327 | return mCornerRadius; 328 | } 329 | 330 | void getMaxShadowAndCornerPadding(Rect into) { 331 | getPadding(into); 332 | } 333 | 334 | void setShadowSize(float size) { 335 | setShadowSize(size, mRawMaxShadowSize); 336 | } 337 | 338 | void setMaxShadowSize(float size) { 339 | setShadowSize(mRawShadowSize, size); 340 | } 341 | 342 | float getShadowSize() { 343 | return mRawShadowSize; 344 | } 345 | 346 | float getMaxShadowSize() { 347 | return mRawMaxShadowSize; 348 | } 349 | 350 | float getMinWidth() { 351 | final float content = 2 * 352 | Math.max(mRawMaxShadowSize, mCornerRadius + mInsetShadow + mRawMaxShadowSize / 2); 353 | return content + (mRawMaxShadowSize + mInsetShadow) * 2; 354 | } 355 | 356 | float getMinHeight() { 357 | final float content = 2 * Math.max(mRawMaxShadowSize, mCornerRadius + mInsetShadow 358 | + mRawMaxShadowSize * SHADOW_MULTIPLIER / 2); 359 | return content + (mRawMaxShadowSize * SHADOW_MULTIPLIER + mInsetShadow) * 2; 360 | } 361 | 362 | public void setColor(int color) { 363 | mPaint.setColor(color); 364 | invalidateSelf(); 365 | } 366 | 367 | static interface RoundRectHelper { 368 | void drawRoundRect(Canvas canvas, RectF bounds, float cornerRadius, Paint paint); 369 | } 370 | } 371 | -------------------------------------------------------------------------------- /cardview/src/main/java/com/zyp/cardview/YcCardView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.zyp.cardview; 18 | 19 | import android.content.Context; 20 | import android.content.res.TypedArray; 21 | import android.graphics.Color; 22 | import android.graphics.Rect; 23 | import android.graphics.drawable.Drawable; 24 | import android.os.Build; 25 | import android.util.AttributeSet; 26 | import android.view.View; 27 | import android.widget.FrameLayout; 28 | 29 | /** 30 | * A FrameLayout with a rounded corner background and shadow. 31 | *

32 | * CardView uses elevation property on Lollipop for shadows and falls back to a 33 | * custom emulated shadow implementation on older platforms. 34 | *

35 | * Due to expensive nature of rounded corner clipping, on platforms before Lollipop, CardView does 36 | * not clip its children that intersect with rounded corners. Instead, it adds padding to avoid such 37 | * intersection (See {@link #setPreventCornerOverlap(boolean)} to change this behavior). 38 | *

39 | * Before Lollipop, CardView adds padding to its content and draws shadows to that area. This 40 | * padding amount is equal to maxCardElevation + (1 - cos45) * cornerRadius on the 41 | * sides and maxCardElevation * 1.5 + (1 - cos45) * cornerRadius on top and bottom. 42 | *

43 | * Since padding is used to offset content for shadows, you cannot set padding on CardView. 44 | * Instead, you can use content padding attributes in XML or 45 | * {@link #setContentPadding(int, int, int, int)} in code to set the padding between the edges of 46 | * the CardView and children of CardView. 47 | *

48 | * Note that, if you specify exact dimensions for the CardView, because of the shadows, its content 49 | * area will be different between platforms before Lollipop and after Lollipop. By using api version 50 | * specific resource values, you can avoid these changes. Alternatively, If you want CardView to add 51 | * inner padding on platforms Lollipop and after as well, you can call 52 | * {@link #setUseCompatPadding(boolean)} and pass true. 53 | *

54 | * To change CardView's elevation in a backward compatible way, use 55 | * {@link #setCardElevation(float)}. CardView will use elevation API on Lollipop and before 56 | * Lollipop, it will change the shadow size. To avoid moving the View while shadow size is changing, 57 | * shadow size is clamped by {@link #getMaxCardElevation()}. If you want to change elevation 58 | * dynamically, you should call {@link #setMaxCardElevation(float)} when CardView is initialized. 59 | * 60 | * ref com.zyp.cardview.R.styleable#YcCardView_cardBackgroundColor 61 | * ref com.zyp.cardview.R.styleable#YcCardView_cardCornerRadius 62 | * ref com.zyp.cardview.R.styleable#YcCardView_cardElevation 63 | * ref com.zyp.cardview.R.styleable#YcCardView_cardMaxElevation 64 | * ref com.zyp.cardview.R.styleable#YcCardView_cardUseCompatPadding 65 | * ref com.zyp.cardview.R.styleable#YcCardView_cardPreventCornerOverlap 66 | * ref com.zyp.cardview.R.styleable#YcCardView_contentPadding 67 | * ref com.zyp.cardview.R.styleable#YcCardView_contentPaddingLeft 68 | * ref com.zyp.cardview.R.styleable#YcCardView_contentPaddingTop 69 | * ref com.zyp.cardview.R.styleable#YcCardView_contentPaddingRight 70 | * ref com.zyp.cardview.R.styleable#YcCardView_contentPaddingBottom 71 | */ 72 | public class YcCardView extends FrameLayout { 73 | 74 | private static final int[] COLOR_BACKGROUND_ATTR = {android.R.attr.colorBackground}; 75 | private static final YcCardViewImpl IMPL; 76 | 77 | static { 78 | // if (Build.VERSION.SDK_INT >= 21) { 79 | // IMPL = new YcCardViewApi21(); 80 | // } else 81 | if (Build.VERSION.SDK_INT >= 17) { 82 | IMPL = new YcCardViewJellybeanMr1(); 83 | } else { 84 | IMPL = new YcCardViewEclairMr1(); 85 | } 86 | 87 | IMPL.initStatic(); 88 | } 89 | 90 | private boolean mCompatPadding; 91 | 92 | private boolean mPreventCornerOverlap; 93 | 94 | /** 95 | * CardView requires to have a particular minimum size to draw shadows before API 21. If 96 | * developer also sets min width/height, they might be overridden. 97 | *

98 | * CardView works around this issue by recording user given parameters and using an internal 99 | * method to set them. 100 | */ 101 | private int mUserSetMinWidth, mUserSetMinHeight; 102 | 103 | private final Rect mContentPadding = new Rect(); 104 | 105 | private final Rect mShadowBounds = new Rect(); 106 | private int startShadowColor; 107 | private int endShadowColor; 108 | 109 | public YcCardView(Context context) { 110 | super(context); 111 | initialize(context, null, 0); 112 | } 113 | 114 | public YcCardView(Context context, AttributeSet attrs) { 115 | super(context, attrs); 116 | initialize(context, attrs, 0); 117 | } 118 | 119 | public YcCardView(Context context, AttributeSet attrs, int defStyleAttr) { 120 | super(context, attrs, defStyleAttr); 121 | initialize(context, attrs, defStyleAttr); 122 | } 123 | 124 | @Override 125 | public void setPadding(int left, int top, int right, int bottom) { 126 | // NO OP 127 | } 128 | 129 | public void setPaddingRelative(int start, int top, int end, int bottom) { 130 | // NO OP 131 | } 132 | 133 | /** 134 | * Returns whether CardView will add inner padding on platforms Lollipop and after. 135 | * 136 | * @return true if CardView adds inner padding on platforms Lollipop and after to 137 | * have same dimensions with platforms before Lollipop. 138 | */ 139 | public boolean getUseCompatPadding() { 140 | return mCompatPadding; 141 | } 142 | 143 | /** 144 | * CardView adds additional padding to draw shadows on platforms before Lollipop. 145 | *

146 | * This may cause Cards to have different sizes between Lollipop and before Lollipop. If you 147 | * need to align CardView with other Views, you may need api version specific dimension 148 | * resources to account for the changes. 149 | * As an alternative, you can set this flag to true and CardView will add the same 150 | * padding values on platforms Lollipop and after. 151 | *

152 | * Since setting this flag to true adds unnecessary gaps in the UI, default value is 153 | * false. 154 | * 155 | * @param useCompatPadding true if CardView should add padding for the shadows on 156 | * platforms Lollipop and above. 157 | * ref com.zyp.cardview.R.styleable#YcCardView_ycCardUseCompatPadding 158 | */ 159 | public void setUseCompatPadding(boolean useCompatPadding) { 160 | if (mCompatPadding != useCompatPadding) { 161 | mCompatPadding = useCompatPadding; 162 | IMPL.onCompatPaddingChanged(mCardViewDelegate); 163 | } 164 | } 165 | 166 | /** 167 | * Sets the padding between the Card's edges and the children of CardView. 168 | *

169 | * Depending on platform version or {@link #getUseCompatPadding()} settings, CardView may 170 | * update these values before calling {@link View#setPadding(int, int, int, int)}. 171 | * 172 | * @param left The left padding in pixels 173 | * @param top The top padding in pixels 174 | * @param right The right padding in pixels 175 | * @param bottom The bottom padding in pixels 176 | * ref com.zyp.cardview.R.styleable#YcCardView_contentPadding 177 | * ref com.zyp.cardview.R.styleable#YcCardView_contentPaddingLeft 178 | * ref com.zyp.cardview.R.styleable#YcCardView_contentPaddingTop 179 | * ref com.zyp.cardview.R.styleable#YcCardView_contentPaddingRight 180 | * ref com.zyp.cardview.R.styleable#YcCardView_contentPaddingBottom 181 | */ 182 | public void setContentPadding(int left, int top, int right, int bottom) { 183 | mContentPadding.set(left, top, right, bottom); 184 | IMPL.updatePadding(mCardViewDelegate); 185 | } 186 | 187 | @Override 188 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 189 | if (!(IMPL instanceof YcCardViewApi21)) { 190 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); 191 | switch (widthMode) { 192 | case MeasureSpec.EXACTLY: 193 | case MeasureSpec.AT_MOST: 194 | final int minWidth = (int) Math.ceil(IMPL.getMinWidth(mCardViewDelegate)); 195 | widthMeasureSpec = MeasureSpec.makeMeasureSpec(Math.max(minWidth, 196 | MeasureSpec.getSize(widthMeasureSpec)), widthMode); 197 | break; 198 | } 199 | 200 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); 201 | switch (heightMode) { 202 | case MeasureSpec.EXACTLY: 203 | case MeasureSpec.AT_MOST: 204 | final int minHeight = (int) Math.ceil(IMPL.getMinHeight(mCardViewDelegate)); 205 | heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.max(minHeight, 206 | MeasureSpec.getSize(heightMeasureSpec)), heightMode); 207 | break; 208 | } 209 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 210 | } else { 211 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 212 | } 213 | } 214 | 215 | private void initialize(Context context, AttributeSet attrs, int defStyleAttr) { 216 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.YcCardView, defStyleAttr, 217 | R.style.YcCardView); 218 | int backgroundColor; 219 | if (a.hasValue(R.styleable.YcCardView_ycCardBackgroundColor)) { 220 | backgroundColor = a.getColor(R.styleable.YcCardView_ycCardBackgroundColor, 0); 221 | } else { 222 | // There isn't one set, so we'll compute one based on the theme 223 | final TypedArray aa = getContext().obtainStyledAttributes(COLOR_BACKGROUND_ATTR); 224 | final int themeColorBackground = aa.getColor(0, 0); 225 | aa.recycle(); 226 | 227 | // If the theme colorBackground is light, use our own light color, otherwise dark 228 | final float[] hsv = new float[3]; 229 | Color.colorToHSV(themeColorBackground, hsv); 230 | backgroundColor = hsv[2] > 0.5f 231 | ? getResources().getColor(R.color.yc_cardview_light_background) 232 | : getResources().getColor(R.color.yc_cardview_dark_background); 233 | } 234 | startShadowColor = a.getColor(R.styleable.YcCardView_ycStartShadowColor, 0); 235 | endShadowColor = a.getColor(R.styleable.YcCardView_ycEndShadowColor, 0); 236 | float radius = a.getDimension(R.styleable.YcCardView_ycCardCornerRadius, 0); 237 | float elevation = a.getDimension(R.styleable.YcCardView_ycCardElevation, 0); 238 | float maxElevation = a.getDimension(R.styleable.YcCardView_ycCardMaxElevation, 0); 239 | mCompatPadding = a.getBoolean(R.styleable.YcCardView_ycCardUseCompatPadding, false); 240 | mPreventCornerOverlap = a.getBoolean(R.styleable.YcCardView_ycCardPreventCornerOverlap, true); 241 | int defaultPadding = a.getDimensionPixelSize(R.styleable.YcCardView_ycContentPadding, 0); 242 | mContentPadding.left = a.getDimensionPixelSize(R.styleable.YcCardView_ycContentPaddingLeft, 243 | defaultPadding); 244 | mContentPadding.top = a.getDimensionPixelSize(R.styleable.YcCardView_ycContentPaddingTop, 245 | defaultPadding); 246 | mContentPadding.right = a.getDimensionPixelSize(R.styleable.YcCardView_ycContentPaddingRight, 247 | defaultPadding); 248 | mContentPadding.bottom = a.getDimensionPixelSize(R.styleable.YcCardView_contentPaddingBottom, 249 | defaultPadding); 250 | if (elevation > maxElevation) { 251 | maxElevation = elevation; 252 | } 253 | mUserSetMinWidth = a.getDimensionPixelSize(R.styleable.YcCardView_android_minWidth, 0); 254 | mUserSetMinHeight = a.getDimensionPixelSize(R.styleable.YcCardView_android_minHeight, 0); 255 | a.recycle(); 256 | 257 | IMPL.initialize(mCardViewDelegate, context, backgroundColor, radius, 258 | elevation, maxElevation, startShadowColor, endShadowColor); 259 | } 260 | 261 | @Override 262 | public void setMinimumWidth(int minWidth) { 263 | mUserSetMinWidth = minWidth; 264 | super.setMinimumWidth(minWidth); 265 | } 266 | 267 | @Override 268 | public void setMinimumHeight(int minHeight) { 269 | mUserSetMinHeight = minHeight; 270 | super.setMinimumHeight(minHeight); 271 | } 272 | 273 | /** 274 | * Updates the background color of the CardView 275 | * 276 | * @param color The new color to set for the card background 277 | * ref com.zyp.cardview.R.styleable#YcCardView_cardBackgroundColor 278 | */ 279 | public void setCardBackgroundColor(int color) { 280 | IMPL.setBackgroundColor(mCardViewDelegate, color); 281 | } 282 | 283 | /** 284 | * Returns the inner padding after the Card's left edge 285 | * 286 | * @return the inner padding after the Card's left edge 287 | */ 288 | public int getContentPaddingLeft() { 289 | return mContentPadding.left; 290 | } 291 | 292 | /** 293 | * Returns the inner padding before the Card's right edge 294 | * 295 | * @return the inner padding before the Card's right edge 296 | */ 297 | public int getContentPaddingRight() { 298 | return mContentPadding.right; 299 | } 300 | 301 | /** 302 | * Returns the inner padding after the Card's top edge 303 | * 304 | * @return the inner padding after the Card's top edge 305 | */ 306 | public int getContentPaddingTop() { 307 | return mContentPadding.top; 308 | } 309 | 310 | /** 311 | * Returns the inner padding before the Card's bottom edge 312 | * 313 | * @return the inner padding before the Card's bottom edge 314 | */ 315 | public int getContentPaddingBottom() { 316 | return mContentPadding.bottom; 317 | } 318 | 319 | /** 320 | * Updates the corner radius of the CardView. 321 | * 322 | * @param radius The radius in pixels of the corners of the rectangle shape 323 | * ref com.zyp.cardview.R.styleable#YcCardView_cardCornerRadius 324 | * @see #setRadius(float) 325 | */ 326 | public void setRadius(float radius) { 327 | IMPL.setRadius(mCardViewDelegate, radius); 328 | } 329 | 330 | /** 331 | * Returns the corner radius of the CardView. 332 | * 333 | * @return Corner radius of the CardView 334 | * @see #getRadius() 335 | */ 336 | public float getRadius() { 337 | return IMPL.getRadius(mCardViewDelegate); 338 | } 339 | 340 | /** 341 | * Updates the backward compatible elevation of the CardView. 342 | * 343 | * @param elevation The backward compatible elevation in pixels. 344 | * ref com.zyp.cardview.R.styleable#YcCardView_cardElevation 345 | * @see #getCardElevation() 346 | * @see #setMaxCardElevation(float) 347 | */ 348 | public void setCardElevation(float elevation) { 349 | IMPL.setElevation(mCardViewDelegate, elevation); 350 | } 351 | 352 | /** 353 | * Returns the backward compatible elevation of the CardView. 354 | * 355 | * @return Elevation of the CardView 356 | * @see #setCardElevation(float) 357 | * @see #getMaxCardElevation() 358 | */ 359 | public float getCardElevation() { 360 | return IMPL.getElevation(mCardViewDelegate); 361 | } 362 | 363 | /** 364 | * Updates the backward compatible maximum elevation of the CardView. 365 | *

366 | * Calling this method has no effect if device OS version is Lollipop or newer and 367 | * {@link #getUseCompatPadding()} is false. 368 | * 369 | * @param maxElevation The backward compatible maximum elevation in pixels. 370 | * ref com.zyp.cardview.R.styleable#YcCardView_cardMaxElevation 371 | * @see #setCardElevation(float) 372 | * @see #getMaxCardElevation() 373 | */ 374 | public void setMaxCardElevation(float maxElevation) { 375 | IMPL.setMaxElevation(mCardViewDelegate, maxElevation); 376 | } 377 | 378 | /** 379 | * Returns the backward compatible maximum elevation of the CardView. 380 | * 381 | * @return Maximum elevation of the CardView 382 | * @see #setMaxCardElevation(float) 383 | * @see #getCardElevation() 384 | */ 385 | public float getMaxCardElevation() { 386 | return IMPL.getMaxElevation(mCardViewDelegate); 387 | } 388 | 389 | /** 390 | * Returns whether CardView should add extra padding to content to avoid overlaps with rounded 391 | * corners on pre-Lollipop platforms. 392 | * 393 | * @return True if CardView prevents overlaps with rounded corners on platforms before Lollipop. 394 | * Default value is true. 395 | */ 396 | public boolean getPreventCornerOverlap() { 397 | return mPreventCornerOverlap; 398 | } 399 | 400 | /** 401 | * On pre-Lollipop platforms, CardView does not clip the bounds of the Card for the rounded 402 | * corners. Instead, it adds padding to content so that it won't overlap with the rounded 403 | * corners. You can disable this behavior by setting this field to false. 404 | *

405 | * Setting this value on Lollipop and above does not have any effect unless you have enabled 406 | * compatibility padding. 407 | * 408 | * @param preventCornerOverlap Whether CardView should add extra padding to content to avoid 409 | * overlaps with the CardView corners. 410 | * ref com.zyp.cardview.R.styleable#YcCardView_cardPreventCornerOverlap 411 | * @see #setUseCompatPadding(boolean) 412 | */ 413 | public void setPreventCornerOverlap(boolean preventCornerOverlap) { 414 | if (preventCornerOverlap != mPreventCornerOverlap) { 415 | mPreventCornerOverlap = preventCornerOverlap; 416 | IMPL.onPreventCornerOverlapChanged(mCardViewDelegate); 417 | } 418 | } 419 | 420 | private final YcCardViewDelegate mCardViewDelegate = new YcCardViewDelegate() { 421 | private Drawable mCardBackground; 422 | 423 | @Override 424 | public void setCardBackground(Drawable drawable) { 425 | mCardBackground = drawable; 426 | setBackgroundDrawable(drawable); 427 | } 428 | 429 | @Override 430 | public boolean getUseCompatPadding() { 431 | return YcCardView.this.getUseCompatPadding(); 432 | } 433 | 434 | @Override 435 | public boolean getPreventCornerOverlap() { 436 | return YcCardView.this.getPreventCornerOverlap(); 437 | } 438 | 439 | @Override 440 | public void setShadowPadding(int left, int top, int right, int bottom) { 441 | mShadowBounds.set(left, top, right, bottom); 442 | YcCardView.super.setPadding(left + mContentPadding.left, top + mContentPadding.top, 443 | right + mContentPadding.right, bottom + mContentPadding.bottom); 444 | } 445 | 446 | @Override 447 | public void setMinWidthHeightInternal(int width, int height) { 448 | if (width > mUserSetMinWidth) { 449 | YcCardView.super.setMinimumWidth(width); 450 | } 451 | if (height > mUserSetMinHeight) { 452 | YcCardView.super.setMinimumHeight(height); 453 | } 454 | } 455 | 456 | @Override 457 | public Drawable getCardBackground() { 458 | return mCardBackground; 459 | } 460 | 461 | @Override 462 | public View getCardView() { 463 | return YcCardView.this; 464 | } 465 | }; 466 | } 467 | --------------------------------------------------------------------------------