Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/shadow color #32

Merged
merged 4 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions ticketview/src/main/java/com/vipulasri/ticketview/BlurBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.vipulasri.ticketview;

import android.content.Context;
import android.graphics.Bitmap;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;

public class BlurBuilder {

public static Bitmap blur(Context context, Bitmap image , float radius) {
if(image==null || image.isRecycled())return image;

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {

RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation input = Allocation.createFromBitmap(rs, image);
Allocation output = Allocation.createTyped(rs, input.getType());
blur.setRadius(radius);
blur.setInput(input);
blur.forEach(output);
output.copyTo(image);
input.destroy();
output.destroy();


return image;
}else{
return image;
}
}

}
43 changes: 22 additions & 21 deletions ticketview/src/main/java/com/vipulasri/ticketview/TicketView.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import android.graphics.PorterDuffColorFilter;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;

import android.support.annotation.IntDef;
import android.util.AttributeSet;
import android.util.Log;
Expand All @@ -23,8 +20,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import static android.graphics.Bitmap.Config.ALPHA_8;
import static android.graphics.Color.BLACK;
import static android.graphics.Bitmap.Config.ARGB_8888;
import static android.graphics.Color.TRANSPARENT;
import static android.graphics.Paint.ANTI_ALIAS_FLAG;
import static android.graphics.PorterDuff.Mode.SRC_IN;
Expand Down Expand Up @@ -88,6 +84,7 @@ public class TicketView extends View {
private int mDividerPadding;
private Bitmap mShadow;
private final Paint mShadowPaint = new Paint(ANTI_ALIAS_FLAG);
private int mShadowColor;
private float mShadowBlurRadius = 0f;
private Drawable mBackgroundBeforeDivider;
private Drawable mBackgroundAfterDivider;
Expand Down Expand Up @@ -271,7 +268,7 @@ private void generateShadow() {
if (mShadowBlurRadius == 0f) return;

if (mShadow == null) {
mShadow = Bitmap.createBitmap(getWidth(), getHeight(), ALPHA_8);
mShadow = Bitmap.createBitmap(getWidth(), getHeight(), ARGB_8888);
} else {
mShadow.eraseColor(TRANSPARENT);
}
Expand All @@ -280,17 +277,8 @@ private void generateShadow() {
if (mShowBorder) {
c.drawPath(mPath, mShadowPaint);
}
RenderScript rs = RenderScript.create(getContext());
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8(rs));
Allocation input = Allocation.createFromBitmap(rs, mShadow);
Allocation output = Allocation.createTyped(rs, input.getType());
blur.setRadius(mShadowBlurRadius);
blur.setInput(input);
blur.forEach(output);
output.copyTo(mShadow);
input.destroy();
output.destroy();
blur.destroy();

mShadow = BlurBuilder.blur(getContext(),mShadow,mCornerRadius);
}
}

Expand Down Expand Up @@ -324,13 +312,11 @@ private void init(AttributeSet attrs) {
if (elevation > 0f) {
setShadowBlurRadius(elevation);
}
mShadowColor = typedArray.getColor(R.styleable.TicketView_shadowColor, getResources().getColor(android.R.color.black));

typedArray.recycle();
}

mShadowPaint.setColorFilter(new PorterDuffColorFilter(BLACK, SRC_IN));
mShadowPaint.setAlpha(51); // 20%

initElements();

setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Expand All @@ -346,6 +332,7 @@ private void initElements() {
mScallopPosition = 100 / mScallopPositionPercent;
mScallopHeight = mScallopRadius * 2;

setShadowPaint();
setBackgroundPaint();
setBorderPaint();
setDividerPaint();
Expand Down Expand Up @@ -382,6 +369,11 @@ private void setTicketBackgroundAfterDivider(Canvas canvas) {
mBackgroundAfterDivider.draw(canvas);
}

private void setShadowPaint() {
mShadowPaint.setColorFilter(new PorterDuffColorFilter(mShadowColor, SRC_IN));
mShadowPaint.setAlpha(51); // 20%
}

private void setBackgroundPaint() {
mBackgroundPaint.setAlpha(0);
mBackgroundPaint.setAntiAlias(true);
Expand Down Expand Up @@ -611,6 +603,15 @@ private void setShadowBlurRadius(float elevation) {
mShadowBlurRadius = Math.min(25f * (elevation / maxElevation), 25f);
}

public int getShadowColor() {
return mShadowColor;
}

public void setShadowColor(int dividerColor) {
this.mShadowColor = dividerColor;
initElements();
}

private boolean isJellyBeanAndAbove() {
return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
}
Expand Down
1 change: 1 addition & 0 deletions ticketview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<attr name="ticketElevation" format="dimension" />
<attr name="ticketBackgroundBeforeDivider" format="reference" />
<attr name="ticketBackgroundAfterDivider" format="reference" />
<attr name="shadowColor" format="color"/>
</declare-styleable>

</resources>