Implement blur

This commit is contained in:
paphonb
2018-08-23 22:44:15 +07:00
parent f987d1a8e5
commit 139752d9c7
7 changed files with 163 additions and 15 deletions

View File

@@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<com.android.quickstep.views.ShelfScrimView
<ch.deletescape.lawnchair.views.BlurScrimView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"

View File

@@ -46,21 +46,21 @@ import com.android.launcher3.views.ScrimView;
public class ShelfScrimView extends ScrimView {
// In transposed layout, we simply draw a flat color.
private boolean mDrawingFlatColor;
protected boolean mDrawingFlatColor;
// For shelf mode
private final int mEndAlpha;
private final int mThresholdAlpha;
private final float mRadius;
protected final int mEndAlpha;
protected final int mThresholdAlpha;
protected final float mRadius;
private final float mMaxScrimAlpha;
private final Paint mPaint;
// Max vertical progress after which the scrim stops moving.
private float mMoveThreshold;
protected float mMoveThreshold;
// Minimum visible size of the scrim.
private int mMinSize;
private float mScrimMoveFactor = 0;
protected float mScrimMoveFactor = 0;
private int mShelfColor;
private int mRemainingScreenColor;
@@ -152,6 +152,7 @@ public class ShelfScrimView extends ScrimView {
private float drawBackground(Canvas canvas) {
if (mDrawingFlatColor) {
onDrawFlatColor(canvas);
if (mCurrentFlatColor != 0) {
canvas.drawColor(mCurrentFlatColor);
}
@@ -161,6 +162,7 @@ public class ShelfScrimView extends ScrimView {
if (mShelfColor == 0) {
return 0;
} else if (mScrimMoveFactor <= 0) {
onDrawFlatColor(canvas);
canvas.drawColor(mShelfColor);
return getHeight();
}
@@ -190,8 +192,14 @@ public class ShelfScrimView extends ScrimView {
}
mPaint.setColor(mShelfColor);
canvas.drawRoundRect(0, top, getWidth(), getHeight() + mRadius,
onDrawRoundRect(canvas, 0, top, getWidth(), getHeight() + mRadius,
mRadius, mRadius, mPaint);
return minTop - mDragHandleSize - top;
}
@Override
protected void onDrawRoundRect(Canvas canvas, float left, float top, float right, float bottom, float rx, float ry, Paint paint) {
canvas.drawRoundRect(0, top, getWidth(), getHeight() + mRadius,
mRadius, mRadius, mPaint);
}
}

View File

@@ -145,6 +145,11 @@ open class LawnchairLauncher : NexusLauncherActivity() {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onRotationChanged() {
super.onRotationChanged()
blurWallpaperProvider.updateAsync()
}
fun shouldRecreate() = !sRestart
class Screenshot : LawnchairLauncher() {

View File

@@ -79,6 +79,8 @@ class BlurDrawable internal constructor(
private var mBottomRoundBitmap: Bitmap? = null
private var mBottomCanvas = Canvas()
private var mAlpha = 255
init {
initializeRenderScript(mProvider.context)
}
@@ -297,9 +299,11 @@ class BlurDrawable internal constructor(
}
override fun setAlpha(alpha: Int) {
mShouldDraw = alpha > 0
mPaint.alpha = alpha
mCornerPaint.alpha = alpha
if (mAlpha != alpha) {
mAlpha = alpha
mShouldDraw = alpha > 0
mPaint.alpha = alpha
}
}
override fun setColorFilter(colorFilter: ColorFilter?) {

View File

@@ -0,0 +1,120 @@
package ch.deletescape.lawnchair.views
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.Log
import ch.deletescape.lawnchair.blur.BlurDrawable
import ch.deletescape.lawnchair.blur.BlurWallpaperProvider
import ch.deletescape.lawnchair.blurWallpaperProvider
import com.android.launcher3.anim.Interpolators.ACCEL_2
import com.android.quickstep.views.ShelfScrimView
/*
* Copyright (C) 2018 paphonb@xda
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class BlurScrimView(context: Context, attrs: AttributeSet) : ShelfScrimView(context, attrs) {
private val blurDrawableCallback by lazy {
object : Drawable.Callback {
override fun unscheduleDrawable(who: Drawable?, what: Runnable?) {
}
override fun invalidateDrawable(who: Drawable?) {
invalidate()
}
override fun scheduleDrawable(who: Drawable?, what: Runnable?, `when`: Long) {
}
}
}
private val provider by lazy { context.blurWallpaperProvider }
private val useFlatColor get() = mLauncher.deviceProfile.isVerticalBarLayout
private val blurRadius get() = if (useFlatColor) 0f else mRadius
private var blurDrawable: BlurDrawable? = null
private fun createBlurDrawable(): BlurDrawable? {
blurDrawable?.apply { if (isAttachedToWindow) stopListening() }
return if (BlurWallpaperProvider.isEnabled) {
provider.createDrawable(blurRadius, false).apply { callback = blurDrawableCallback }
} else {
null
}?.apply {
setBounds(left, top, right, bottom)
if (isAttachedToWindow) startListening()
}
}
override fun reInitUi() {
super.reInitUi()
blurDrawable = createBlurDrawable()
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
blurDrawable?.startListening()
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
blurDrawable?.stopListening()
}
override fun setProgress(progress: Float) {
blurDrawable?.alpha = if (useFlatColor) ((1 - progress) * 255).toInt() else 255
super.setProgress(progress)
}
override fun onDrawFlatColor(canvas: Canvas) {
blurDrawable?.draw(canvas)
}
override fun onDrawRoundRect(canvas: Canvas, left: Float, top: Float, right: Float, bottom: Float, rx: Float, ry: Float, paint: Paint) {
blurDrawable?.run {
setBounds(left.toInt(), top.toInt(), right.toInt(), bottom.toInt())
draw(canvas)
}
super.onDrawRoundRect(canvas, left, top, right, bottom, rx, ry, paint)
}
override fun updateColors() {
super.updateColors()
if (useFlatColor) {
blurDrawable?.alpha = ((1 - mProgress) * 255).toInt()
} else {
if (mProgress >= mMoveThreshold) {
blurDrawable?.alpha = Math.round(255 * ACCEL_2.getInterpolation(
(1 - mProgress) / (1 - mMoveThreshold)))
} else {
blurDrawable?.alpha = 255
}
}
}
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
super.onLayout(changed, left, top, right, bottom)
if (useFlatColor) {
blurDrawable?.setBounds(left, top, right, bottom)
}
}
}

View File

@@ -376,6 +376,8 @@ public class Launcher extends BaseDraggingActivity implements LauncherExterns,
// TODO: We can probably avoid rebind when only screen size changed.
rebindModel();
onRotationChanged();
}
mOldConfig.setTo(newConfig);
@@ -2453,6 +2455,10 @@ public class Launcher extends BaseDraggingActivity implements LauncherExterns,
return mDeviceProfile.isVerticalBarLayout();
}
protected void onRotationChanged() {
}
public static Launcher getLauncher(Context context) {
if (context instanceof Launcher) {
return (Launcher) context;

View File

@@ -32,10 +32,7 @@ import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.RectEvaluator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
@@ -428,4 +425,12 @@ public class ScrimView extends View implements Insettable, OnChangeListener,
public int getDragHandleSize() {
return mDragHandleSize;
}
protected void onDrawFlatColor(Canvas canvas) {
}
protected void onDrawRoundRect(Canvas canvas, float left, float top, float right, float bottom, float rx, float ry, Paint paint) {
}
}