Skip to content

Commit

Permalink
Merge pull request #45 from CameraKit/v1.1.0
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
emersoncloud committed Nov 1, 2018
2 parents a7adafc + 8daeebf commit 5756fb3
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 85 deletions.
2 changes: 0 additions & 2 deletions blurkit/build.gradle
@@ -1,6 +1,5 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'com.github.dcendents.android-maven' version '1.5'
}

Expand Down Expand Up @@ -35,7 +34,6 @@ android {
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:1.10.19'
androidTestImplementation 'com.android.support:support-annotations:28.0.0'
Expand Down
17 changes: 15 additions & 2 deletions blurkit/src/main/java/io/alterac/blurkit/BlurKit.java
Expand Up @@ -24,7 +24,7 @@ public static void init(Context context) {
}

instance = new BlurKit();
rs = RenderScript.create(context);
rs = RenderScript.create(context.getApplicationContext());
}

public Bitmap blur(Bitmap src, int radius) {
Expand All @@ -39,7 +39,7 @@ public Bitmap blur(Bitmap src, int radius) {
}

public Bitmap blur(View src, int radius) {
Bitmap bitmap = getBitmapForView(src, FULL_SCALE);
Bitmap bitmap = getBitmapForView(src);
return blur(bitmap, radius);
}

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

private Bitmap getBitmapForView(View src) {
Bitmap bitmap = Bitmap.createBitmap(
src.getWidth(),
src.getHeight(),
Bitmap.Config.ARGB_8888
);

Canvas canvas = new Canvas(bitmap);
src.draw(canvas);

return bitmap;
}

public static BlurKit getInstance() {
if (instance == null) {
throw new RuntimeException("BlurKit not initialized!");
Expand Down
47 changes: 41 additions & 6 deletions blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java
Expand Up @@ -29,6 +29,7 @@ public class BlurLayout extends FrameLayout {
public static final int DEFAULT_BLUR_RADIUS = 12;
public static final int DEFAULT_FPS = 60;
public static final float DEFAULT_CORNER_RADIUS = 0.f;
public static final float DEFAULT_ALPHA = Float.NaN;

// Customizable attributes

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

/** Alpha value to set transparency */
private float mAlpha;

/** Is blur running? */
private boolean mRunning;

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

// Set alpha to 0 before creating the parent view bitmap.
// The blur view shouldn't be visible in the created bitmap.
setAlpha(0);
super.setAlpha(0);

// Screen sizes for bound checks
int screenWidth = mActivityView.get().getWidth();
Expand All @@ -219,13 +224,13 @@ private Bitmap blur() {
leftOffset = x + leftOffset >= 0 ? leftOffset : 0;

int rightOffset = xPadding;
rightOffset = x + getWidth() + rightOffset <= screenWidth ? rightOffset : screenWidth - getWidth() - x;
rightOffset = x + screenWidth - rightOffset <= screenWidth ? rightOffset : screenWidth + screenWidth - x;

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

int bottomOffset = yPadding;
bottomOffset = y + height + bottomOffset <= screenHeight ? bottomOffset : 0;
bottomOffset = y + getHeight() + bottomOffset <= screenHeight ? bottomOffset : 0;

// Parent view bitmap, downscaled with mDownscaleFactor
Bitmap bitmap;
Expand Down Expand Up @@ -279,7 +284,11 @@ private Bitmap blur() {
}

// Make self visible again.
setAlpha(1);
if (Float.isNaN(mAlpha)) {
super.setAlpha(1);
} else {
super.setAlpha(mAlpha);
}

// Set background as blurred bitmap.
return bitmap;
Expand Down Expand Up @@ -446,6 +455,25 @@ public float getCornerRadius() {
return mCornerRadius;
}

/**
* Set the alpha value
* See {@link #mAlpha}
*/
public void setAlpha(float alpha) {
mAlpha = alpha;
if (!mViewLocked) {
super.setAlpha(mAlpha);
}
}

/**
* Get alpha value.
* See {@link #mAlpha}
*/
public float getAlpha() {
return mAlpha;
}

/**
* Save the view bitmap to be re-used each frame instead of regenerating.
* See {@link #mViewLocked}.
Expand All @@ -456,9 +484,16 @@ public void lockView() {
if (mActivityView != null && mActivityView.get() != null) {
View view = mActivityView.get().getRootView();
try {
setAlpha(0f);
super.setAlpha(0f);

mLockedBitmap = getDownscaledBitmapForView(view, new Rect(0, 0, view.getWidth(), view.getHeight()), mDownscaleFactor);
setAlpha(1f);

if (Float.isNaN(mAlpha)) {
super.setAlpha(1);
} else {
super.setAlpha(mAlpha);
}

mLockedBitmap = BlurKit.getInstance().blur(mLockedBitmap, mBlurRadius);
} catch (Exception e) {
// ignore
Expand Down
69 changes: 69 additions & 0 deletions blurkit/src/main/java/io/alterac/blurkit/RoundedImageView.java
@@ -0,0 +1,69 @@
package io.alterac.blurkit;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RoundedImageView extends ImageView {

private float mCornerRadius = 0;
public static final int DEFAULT_COLOR = 0xff000000;
public static final int DEFAULT_RGB = 0;

private RectF rectF;
private PorterDuffXfermode porterDuffXfermode;

public RoundedImageView(Context context) {
super(context, null);
rectF = new RectF();
porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
}

public RoundedImageView(Context context, AttributeSet attributes) {
super(context, attributes);
rectF = new RectF();
porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
}

@Override
protected void onDraw(Canvas canvas) {
Drawable myDrawable = getDrawable();
if (myDrawable!=null && myDrawable instanceof BitmapDrawable && mCornerRadius > 0) {
rectF.set(myDrawable.getBounds());
int prevCount = canvas.saveLayer(rectF, null, Canvas.ALL_SAVE_FLAG);
getImageMatrix().mapRect(rectF);

Paint paint = ((BitmapDrawable) myDrawable).getPaint();
paint.setAntiAlias(true);
paint.setColor(DEFAULT_COLOR);
Xfermode prevMode = paint.getXfermode();

canvas.drawARGB(DEFAULT_RGB, DEFAULT_RGB, DEFAULT_RGB, DEFAULT_RGB);
canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, paint);

paint.setXfermode(porterDuffXfermode);
super.onDraw(canvas);

paint.setXfermode(prevMode);
canvas.restoreToCount(prevCount);
} else {
super.onDraw(canvas);
}
}

public void setCornerRadius(float cornerRadius) {
this.mCornerRadius = cornerRadius;
}

public float getCornerRadius() {
return this.mCornerRadius;
}
}
73 changes: 0 additions & 73 deletions blurkit/src/main/java/io/alterac/blurkit/RoundedImageView.kt

This file was deleted.

1 change: 1 addition & 0 deletions blurkit/src/main/res/values/attrs.xml
Expand Up @@ -6,6 +6,7 @@
<attr name="blk_blurRadius" format="integer" />
<attr name="blk_fps" format="integer" />
<attr name="blk_cornerRadius" format="dimension" />
<attr name="blk_alpha" format="float" />
</declare-styleable>

</resources>
2 changes: 0 additions & 2 deletions build.gradle
@@ -1,14 +1,12 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.2.71'
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.1'
}
}
Expand Down

0 comments on commit 5756fb3

Please sign in to comment.