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

feat: highlight an area when using "Overlay" function #121

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion blurry/src/main/java/jp/wasabeef/blurry/Blur.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.PorterDuffXfermode;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RSRuntimeException;
Expand Down Expand Up @@ -49,7 +50,7 @@ public static Bitmap of(Context context, Bitmap source, BlurFactor factor) {
return null;
}

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Bitmap bitmap = Bitmap.createBitmap(context.getResources().getDisplayMetrics(), width, height, Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);
canvas.scale(1 / (float) factor.sampling, 1 / (float) factor.sampling);
Expand All @@ -66,6 +67,11 @@ public static Bitmap of(Context context, Bitmap source, BlurFactor factor) {
bitmap = Blur.stack(bitmap, factor.radius, true);
}

if (factor.hotRect != null) {
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawBitmap(source, factor.hotRect, factor.hotRect, paint);
}

if (factor.sampling == BlurFactor.DEFAULT_SAMPLING) {
return bitmap;
} else {
Expand Down
2 changes: 2 additions & 0 deletions blurry/src/main/java/jp/wasabeef/blurry/BlurFactor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jp.wasabeef.blurry;

import android.graphics.Color;
import android.graphics.Rect;

/**
* Copyright (C) 2020 Wasabeef
Expand Down Expand Up @@ -28,4 +29,5 @@ class BlurFactor {
public int radius = DEFAULT_RADIUS;
public int sampling = DEFAULT_SAMPLING;
public int color = Color.TRANSPARENT;
public Rect hotRect;
}
2 changes: 1 addition & 1 deletion blurry/src/main/java/jp/wasabeef/blurry/BlurTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void execute() {
THREAD_POOL.execute(new Runnable() {
@Override
public void run() {
Context context = contextWeakRef.get();
final Context context = contextWeakRef.get();
if (callback != null) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
Expand Down
15 changes: 15 additions & 0 deletions blurry/src/main/java/jp/wasabeef/blurry/Blurry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
Expand Down Expand Up @@ -95,9 +96,23 @@ public BitmapComposer from(Bitmap bitmap) {
}

public void onto(final ViewGroup target) {
onto(target, null);
}

public void onto(final ViewGroup target, final View hotView) {
factor.width = target.getMeasuredWidth();
factor.height = target.getMeasuredHeight();

if (hotView != null) {
final int[] hotPos = new int[2];
hotView.getLocationOnScreen(hotPos);
final int[] targetPos = new int[2];
target.getLocationOnScreen(targetPos);
final int relativeX = hotPos[0] - targetPos[0];
final int relativeY = hotPos[1] - targetPos[1];
factor.hotRect = new Rect(relativeX, relativeY, hotView.getWidth() + relativeX, hotView.getHeight() + relativeY);
}

if (async) {
BlurTask task = new BlurTask(target, factor, new BlurTask.Callback() {
@Override
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.0-beta04'
classpath 'com.android.tools.build:gradle:4.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// TODO: Close JCenter on May 1st https://jfrog.com/blog/into-the-sunset-bintray-jcenter-gocenter-and-chartcenter/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MainActivity : AppCompatActivity() {
.sampling(2)
.async()
.animate(500)
.onto(findViewById<View>(R.id.content) as ViewGroup)
.onto(findViewById<View>(R.id.content) as ViewGroup, findViewById(R.id.hotView))
Log.d(getString(R.string.app_name),
"TIME " + (System.currentTimeMillis() - startMs).toString() + "ms")
}
Expand Down
7 changes: 7 additions & 0 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,12 @@
android:textSize="88sp"
android:textStyle="bold" />

<View
android:id="@+id/hotView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignLeft="@id/spacer"
android:layout_marginTop="100dp" />

</RelativeLayout>