diff --git a/blurkit/build.gradle b/blurkit/build.gradle index 16196b3..cb23d1e 100644 --- a/blurkit/build.gradle +++ b/blurkit/build.gradle @@ -1,6 +1,5 @@ plugins { id 'com.android.library' - id 'kotlin-android' id 'com.github.dcendents.android-maven' version '1.5' } @@ -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' diff --git a/blurkit/src/main/java/io/alterac/blurkit/BlurKit.java b/blurkit/src/main/java/io/alterac/blurkit/BlurKit.java index 9a75faf..f969c3c 100644 --- a/blurkit/src/main/java/io/alterac/blurkit/BlurKit.java +++ b/blurkit/src/main/java/io/alterac/blurkit/BlurKit.java @@ -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) { @@ -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); } @@ -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!"); diff --git a/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java b/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java index 6a9e5ec..7d03c9e 100644 --- a/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java +++ b/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java @@ -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 @@ -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; @@ -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(); } @@ -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(); @@ -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; @@ -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; @@ -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}. @@ -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 diff --git a/blurkit/src/main/java/io/alterac/blurkit/RoundedImageView.java b/blurkit/src/main/java/io/alterac/blurkit/RoundedImageView.java new file mode 100644 index 0000000..886efdd --- /dev/null +++ b/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; + } +} \ No newline at end of file diff --git a/blurkit/src/main/java/io/alterac/blurkit/RoundedImageView.kt b/blurkit/src/main/java/io/alterac/blurkit/RoundedImageView.kt deleted file mode 100644 index 5b529fd..0000000 --- a/blurkit/src/main/java/io/alterac/blurkit/RoundedImageView.kt +++ /dev/null @@ -1,73 +0,0 @@ -package io.alterac.blurkit - -import android.content.Context -import android.graphics.Canvas -import android.graphics.PorterDuff -import android.graphics.PorterDuffXfermode -import android.graphics.RectF -import android.graphics.drawable.BitmapDrawable -import android.util.AttributeSet -import android.widget.ImageView - -class RoundedImageView : ImageView { - - private var mCornerRadius = 0f - - private var rectF: RectF? = null - private var porterDuffXfermode: PorterDuffXfermode? = null - - companion object { - const val DEFAULT_COLOR = -0x1000000 - } - - constructor(context: Context) : super(context, null) - constructor(context: Context, attributes: AttributeSet) : super(context, attributes) - - init { - rectF = RectF() - porterDuffXfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN) - } - - /** - * Draw the RoundedImageView. - */ - override fun onDraw(canvas: Canvas) { - val myDrawable = drawable - if (myDrawable != null && myDrawable is BitmapDrawable && mCornerRadius > 0) { - val paint = myDrawable.paint - - rectF!!.set(myDrawable.bounds) - - val prevCount = canvas.saveLayer(rectF, null) - - paint.isAntiAlias = true - paint.color = DEFAULT_COLOR - - canvas.drawARGB(0, 0, 0, 0) - canvas.drawRoundRect(rectF!!, mCornerRadius, mCornerRadius, paint) - - val prevMode = paint.xfermode - paint.xfermode = porterDuffXfermode - super.onDraw(canvas) - - paint.xfermode = prevMode - canvas.restoreToCount(prevCount) - } else { - super.onDraw(canvas) - } - } - - /** - * Set corner radius for RoundedImageView. - */ - fun setCornerRadius(cornerRadius: Float) { - this.mCornerRadius = cornerRadius - } - - /** - * Get corner radius value. - */ - fun getCornerRadius(): Float { - return this.mCornerRadius - } -} \ No newline at end of file diff --git a/blurkit/src/main/res/values/attrs.xml b/blurkit/src/main/res/values/attrs.xml index 3859654..d605584 100644 --- a/blurkit/src/main/res/values/attrs.xml +++ b/blurkit/src/main/res/values/attrs.xml @@ -6,6 +6,7 @@ + \ No newline at end of file diff --git a/build.gradle b/build.gradle index f385f1b..d919f76 100644 --- a/build.gradle +++ b/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' } }