Skip to content

Commit 5756fb3

Browse files
authored
Merge pull request #45 from CameraKit/v1.1.0
v1.1.0
2 parents a7adafc + 8daeebf commit 5756fb3

File tree

7 files changed

+126
-85
lines changed

7 files changed

+126
-85
lines changed

blurkit/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
plugins {
22
id 'com.android.library'
3-
id 'kotlin-android'
43
id 'com.github.dcendents.android-maven' version '1.5'
54
}
65

@@ -35,7 +34,6 @@ android {
3534
}
3635

3736
dependencies {
38-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
3937
testImplementation 'junit:junit:4.12'
4038
testImplementation 'org.mockito:mockito-core:1.10.19'
4139
androidTestImplementation 'com.android.support:support-annotations:28.0.0'

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static void init(Context context) {
2424
}
2525

2626
instance = new BlurKit();
27-
rs = RenderScript.create(context);
27+
rs = RenderScript.create(context.getApplicationContext());
2828
}
2929

3030
public Bitmap blur(Bitmap src, int radius) {
@@ -39,7 +39,7 @@ public Bitmap blur(Bitmap src, int radius) {
3939
}
4040

4141
public Bitmap blur(View src, int radius) {
42-
Bitmap bitmap = getBitmapForView(src, FULL_SCALE);
42+
Bitmap bitmap = getBitmapForView(src);
4343
return blur(bitmap, radius);
4444
}
4545

@@ -64,6 +64,19 @@ private Bitmap getBitmapForView(View src, float downscaleFactor) {
6464
return bitmap;
6565
}
6666

67+
private Bitmap getBitmapForView(View src) {
68+
Bitmap bitmap = Bitmap.createBitmap(
69+
src.getWidth(),
70+
src.getHeight(),
71+
Bitmap.Config.ARGB_8888
72+
);
73+
74+
Canvas canvas = new Canvas(bitmap);
75+
src.draw(canvas);
76+
77+
return bitmap;
78+
}
79+
6780
public static BlurKit getInstance() {
6881
if (instance == null) {
6982
throw new RuntimeException("BlurKit not initialized!");

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

Lines changed: 41 additions & 6 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();
@@ -219,13 +224,13 @@ private Bitmap blur() {
219224
leftOffset = x + leftOffset >= 0 ? leftOffset : 0;
220225

221226
int rightOffset = xPadding;
222-
rightOffset = x + getWidth() + rightOffset <= screenWidth ? rightOffset : screenWidth - getWidth() - x;
227+
rightOffset = x + screenWidth - rightOffset <= screenWidth ? rightOffset : screenWidth + screenWidth - x;
223228

224229
int topOffset = -yPadding;
225230
topOffset = y + topOffset >= 0 ? topOffset : 0;
226231

227232
int bottomOffset = yPadding;
228-
bottomOffset = y + height + bottomOffset <= screenHeight ? bottomOffset : 0;
233+
bottomOffset = y + getHeight() + bottomOffset <= screenHeight ? bottomOffset : 0;
229234

230235
// Parent view bitmap, downscaled with mDownscaleFactor
231236
Bitmap bitmap;
@@ -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
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package io.alterac.blurkit;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Paint;
6+
import android.graphics.PorterDuff;
7+
import android.graphics.PorterDuffXfermode;
8+
import android.graphics.RectF;
9+
import android.graphics.Xfermode;
10+
import android.graphics.drawable.BitmapDrawable;
11+
import android.graphics.drawable.Drawable;
12+
import android.util.AttributeSet;
13+
import android.widget.ImageView;
14+
15+
public class RoundedImageView extends ImageView {
16+
17+
private float mCornerRadius = 0;
18+
public static final int DEFAULT_COLOR = 0xff000000;
19+
public static final int DEFAULT_RGB = 0;
20+
21+
private RectF rectF;
22+
private PorterDuffXfermode porterDuffXfermode;
23+
24+
public RoundedImageView(Context context) {
25+
super(context, null);
26+
rectF = new RectF();
27+
porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
28+
}
29+
30+
public RoundedImageView(Context context, AttributeSet attributes) {
31+
super(context, attributes);
32+
rectF = new RectF();
33+
porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
34+
}
35+
36+
@Override
37+
protected void onDraw(Canvas canvas) {
38+
Drawable myDrawable = getDrawable();
39+
if (myDrawable!=null && myDrawable instanceof BitmapDrawable && mCornerRadius > 0) {
40+
rectF.set(myDrawable.getBounds());
41+
int prevCount = canvas.saveLayer(rectF, null, Canvas.ALL_SAVE_FLAG);
42+
getImageMatrix().mapRect(rectF);
43+
44+
Paint paint = ((BitmapDrawable) myDrawable).getPaint();
45+
paint.setAntiAlias(true);
46+
paint.setColor(DEFAULT_COLOR);
47+
Xfermode prevMode = paint.getXfermode();
48+
49+
canvas.drawARGB(DEFAULT_RGB, DEFAULT_RGB, DEFAULT_RGB, DEFAULT_RGB);
50+
canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, paint);
51+
52+
paint.setXfermode(porterDuffXfermode);
53+
super.onDraw(canvas);
54+
55+
paint.setXfermode(prevMode);
56+
canvas.restoreToCount(prevCount);
57+
} else {
58+
super.onDraw(canvas);
59+
}
60+
}
61+
62+
public void setCornerRadius(float cornerRadius) {
63+
this.mCornerRadius = cornerRadius;
64+
}
65+
66+
public float getCornerRadius() {
67+
return this.mCornerRadius;
68+
}
69+
}

blurkit/src/main/java/io/alterac/blurkit/RoundedImageView.kt

Lines changed: 0 additions & 73 deletions
This file was deleted.

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>

build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
ext.kotlin_version = '1.2.71'
54
repositories {
65
jcenter()
76
google()
87
}
98
dependencies {
109
classpath 'com.android.tools.build:gradle:3.2.0'
11-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1210
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.1'
1311
}
1412
}

0 commit comments

Comments
 (0)