Skip to content

Commit 8daeebf

Browse files
committed
Add blk_alpha attribute and setAlpha method. Update alpha behavior
1 parent 0d2b94e commit 8daeebf

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class BlurLayout extends FrameLayout {
2929
public static final int DEFAULT_BLUR_RADIUS = 12;
3030
public static final int DEFAULT_FPS = 60;
3131
public static final float DEFAULT_CORNER_RADIUS = 0.f;
32+
public static final float DEFAULT_ALPHA = Float.NaN;
3233

3334
// Customizable attributes
3435

@@ -44,6 +45,9 @@ public class BlurLayout extends FrameLayout {
4445
/** Corner radius for the layouts blur. To make rounded rects and circles. */
4546
private float mCornerRadius;
4647

48+
/** Alpha value to set transparency */
49+
private float mAlpha;
50+
4751
/** Is blur running? */
4852
private boolean mRunning;
4953

@@ -91,6 +95,7 @@ public BlurLayout(Context context, AttributeSet attrs) {
9195
mBlurRadius = a.getInteger(R.styleable.BlurLayout_blk_blurRadius, DEFAULT_BLUR_RADIUS);
9296
mFPS = a.getInteger(R.styleable.BlurLayout_blk_fps, DEFAULT_FPS);
9397
mCornerRadius = a.getDimension(R.styleable.BlurLayout_blk_cornerRadius, DEFAULT_CORNER_RADIUS);
98+
mAlpha = a.getDimension(R.styleable.BlurLayout_blk_alpha, DEFAULT_ALPHA);
9499
} finally {
95100
a.recycle();
96101
}
@@ -195,7 +200,7 @@ private Bitmap blur() {
195200

196201
// Set alpha to 0 before creating the parent view bitmap.
197202
// The blur view shouldn't be visible in the created bitmap.
198-
setAlpha(0);
203+
super.setAlpha(0);
199204

200205
// Screen sizes for bound checks
201206
int screenWidth = mActivityView.get().getWidth();
@@ -279,7 +284,11 @@ private Bitmap blur() {
279284
}
280285

281286
// Make self visible again.
282-
setAlpha(1);
287+
if (Float.isNaN(mAlpha)) {
288+
super.setAlpha(1);
289+
} else {
290+
super.setAlpha(mAlpha);
291+
}
283292

284293
// Set background as blurred bitmap.
285294
return bitmap;
@@ -446,6 +455,25 @@ public float getCornerRadius() {
446455
return mCornerRadius;
447456
}
448457

458+
/**
459+
* Set the alpha value
460+
* See {@link #mAlpha}
461+
*/
462+
public void setAlpha(float alpha) {
463+
mAlpha = alpha;
464+
if (!mViewLocked) {
465+
super.setAlpha(mAlpha);
466+
}
467+
}
468+
469+
/**
470+
* Get alpha value.
471+
* See {@link #mAlpha}
472+
*/
473+
public float getAlpha() {
474+
return mAlpha;
475+
}
476+
449477
/**
450478
* Save the view bitmap to be re-used each frame instead of regenerating.
451479
* See {@link #mViewLocked}.
@@ -456,9 +484,16 @@ public void lockView() {
456484
if (mActivityView != null && mActivityView.get() != null) {
457485
View view = mActivityView.get().getRootView();
458486
try {
459-
setAlpha(0f);
487+
super.setAlpha(0f);
488+
460489
mLockedBitmap = getDownscaledBitmapForView(view, new Rect(0, 0, view.getWidth(), view.getHeight()), mDownscaleFactor);
461-
setAlpha(1f);
490+
491+
if (Float.isNaN(mAlpha)) {
492+
super.setAlpha(1);
493+
} else {
494+
super.setAlpha(mAlpha);
495+
}
496+
462497
mLockedBitmap = BlurKit.getInstance().blur(mLockedBitmap, mBlurRadius);
463498
} catch (Exception e) {
464499
// ignore

blurkit/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<attr name="blk_blurRadius" format="integer" />
77
<attr name="blk_fps" format="integer" />
88
<attr name="blk_cornerRadius" format="dimension" />
9+
<attr name="blk_alpha" format="float" />
910
</declare-styleable>
1011

1112
</resources>

0 commit comments

Comments
 (0)