From ae86c08faf91ad08105ac8e7784771232e02f493 Mon Sep 17 00:00:00 2001 From: GIGAMOLE Date: Sat, 13 Aug 2016 18:54:30 +0300 Subject: [PATCH] Update 1.0.2 --- README.md | 4 +- app/src/main/res/layout/activity_main.xml | 2 +- library/build.gradle | 4 +- .../com/gigamole/library/ShadowLayout.java | 38 +++++++++++++------ 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 7fd9263..d5825cb 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ dependencies { Or Gradle Maven Central: ```groovy -compile 'com.github.devlight.shadowlayout:library:1.0.1' +compile 'com.github.devlight.shadowlayout:library:1.0.2' ``` Or Maven: @@ -46,7 +46,7 @@ Or Maven: com.github.devlight.shadowlayout library - 1.0.1 + 1.0.2 aar ``` diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c213054..ea63348 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -95,7 +95,7 @@ android:layout_weight="1" android:background="#7C7C7C" app:sl_shadow_angle="225" - app:sl_shadow_color="#000" + app:sl_shadow_color="#5b000000" app:sl_shadow_distance="4dp" app:sl_shadow_radius="5dp"> diff --git a/library/build.gradle b/library/build.gradle index 9abcdef..1f7c158 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -19,7 +19,7 @@ apply plugin: "com.jfrog.bintray" apply plugin: 'com.github.dcendents.android-maven' apply plugin: 'maven' -version = "1.0.1" +version = "1.0.2" android { compileSdkVersion 23 @@ -29,7 +29,7 @@ android { minSdkVersion 11 targetSdkVersion 23 versionCode 1 - versionName "1.0.1" + versionName "1.0.2" } buildTypes { release { diff --git a/library/src/main/java/com/gigamole/library/ShadowLayout.java b/library/src/main/java/com/gigamole/library/ShadowLayout.java index 05a54f5..343b6c0 100644 --- a/library/src/main/java/com/gigamole/library/ShadowLayout.java +++ b/library/src/main/java/com/gigamole/library/ShadowLayout.java @@ -35,15 +35,16 @@ public class ShadowLayout extends FrameLayout { // Default shadow values - private final static float DEFAULT_SHADOW_RADIUS = 30.0f; - private final static float DEFAULT_SHADOW_DISTANCE = 15.0f; - private final static float DEFAULT_SHADOW_ANGLE = 45.0f; + private final static float DEFAULT_SHADOW_RADIUS = 30.0F; + private final static float DEFAULT_SHADOW_DISTANCE = 15.0F; + private final static float DEFAULT_SHADOW_ANGLE = 45.0F; private final static int DEFAULT_SHADOW_COLOR = Color.DKGRAY; // Shadow bounds values - private final static float MAX_ANGLE = 360.0f; - private final static float MIN_RADIUS = 0.1f; - private final static float MIN_ANGLE = 0.0f; + private final static int MAX_ALPHA = 255; + private final static float MAX_ANGLE = 360.0F; + private final static float MIN_RADIUS = 0.1F; + private final static float MIN_ANGLE = 0.0F; // Shadow paint private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG) { { @@ -53,9 +54,9 @@ public class ShadowLayout extends FrameLayout { }; // Shadow bitmap and canvas private Bitmap mBitmap; - private Canvas mCanvas = new Canvas(); + private final Canvas mCanvas = new Canvas(); // View bounds - private Rect mBounds = new Rect(); + private final Rect mBounds = new Rect(); // Check whether need to redraw shadow private boolean mInvalidateShadow = true; @@ -64,6 +65,7 @@ public class ShadowLayout extends FrameLayout { // Shadow variables private int mShadowColor; + private int mShadowAlpha; private float mShadowRadius; private float mShadowDistance; private float mShadowAngle; @@ -170,8 +172,8 @@ public int getShadowColor() { public void setShadowColor(final int shadowColor) { mShadowColor = shadowColor; + mShadowAlpha = Color.alpha(shadowColor); - mPaint.setColor(shadowColor); resetShadow(); } @@ -186,8 +188,8 @@ public float getShadowDy() { // Reset shadow layer private void resetShadow() { // Detect shadow axis offset - mShadowDx = (float) ((mShadowDistance) * Math.cos(mShadowAngle / 180.0f * Math.PI)); - mShadowDy = (float) ((mShadowDistance) * Math.sin(mShadowAngle / 180.0f * Math.PI)); + mShadowDx = (float) ((mShadowDistance) * Math.cos(mShadowAngle / 180.0F * Math.PI)); + mShadowDy = (float) ((mShadowDistance) * Math.sin(mShadowAngle / 180.0F * Math.PI)); // Set padding for shadow bitmap final int padding = (int) (mShadowDistance + mShadowRadius); @@ -195,6 +197,15 @@ private void resetShadow() { requestLayout(); } + private int adjustShadowAlpha(final boolean adjust) { + return Color.argb( + adjust ? MAX_ALPHA : mShadowAlpha, + Color.red(mShadowColor), + Color.green(mShadowColor), + Color.blue(mShadowColor) + ); + } + @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); @@ -241,6 +252,7 @@ protected void dispatchDraw(final Canvas canvas) { mCanvas.drawColor(0, PorterDuff.Mode.CLEAR); // Draw extracted alpha bounds of our local canvas + mPaint.setColor(adjustShadowAlpha(false)); mCanvas.drawBitmap(extractedAlpha, mShadowDx, mShadowDy, mPaint); // Recycle and clear extracted alpha @@ -251,9 +263,11 @@ protected void dispatchDraw(final Canvas canvas) { } } + // Reset alpha to draw child with full alpha + mPaint.setColor(adjustShadowAlpha(true)); // Draw shadow bitmap if (mCanvas != null && mBitmap != null && !mBitmap.isRecycled()) - canvas.drawBitmap(mBitmap, 0.0f, 0.0f, null); + canvas.drawBitmap(mBitmap, 0.0F, 0.0F, mPaint); } // Draw child`s