From 42f7061e8cf633e81a022a8eaf083f039cb8e5eb Mon Sep 17 00:00:00 2001 From: orangeboy Date: Mon, 31 Oct 2022 19:33:40 +0800 Subject: [PATCH] add: Adapt Jetpack Compose --- .idea/gradle.xml | 1 - app/build.gradle | 16 +++- .../net/center/blurview/demo/TestCompose.kt | 32 +++++++ blurview/build.gradle | 17 +++- .../net/center/blurview/ShapeBlurView.java | 95 ++++++++++++++----- .../blurview/compose/ShapeBlurViewCompose.kt | 23 +++++ 6 files changed, 154 insertions(+), 30 deletions(-) create mode 100644 app/src/main/java/net/center/blurview/demo/TestCompose.kt create mode 100644 blurview/src/main/java/net/center/blurview/compose/ShapeBlurViewCompose.kt diff --git a/.idea/gradle.xml b/.idea/gradle.xml index bff828e..fc85df8 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -15,7 +15,6 @@ - diff --git a/app/build.gradle b/app/build.gradle index 0d00462..ab47924 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,7 +7,7 @@ android { defaultConfig { applicationId "net.center.blurview.demo" - minSdk 19 + minSdk 21 targetSdk 31 versionCode 1 versionName "1.0" @@ -27,6 +27,14 @@ android { kotlinOptions { jvmTarget = '1.8' } + + composeOptions { + kotlinCompilerExtensionVersion '1.0.3' + } + + buildFeatures { + compose = true + } } dependencies { @@ -35,6 +43,12 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.4.1' implementation "androidx.constraintlayout:constraintlayout:2.1.3" + implementation("androidx.compose.ui:ui:1.1.0") + implementation("androidx.compose.material:material:1.1.0") + implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.0") + implementation("androidx.compose.ui:ui-tooling-preview:1.1.0") + debugImplementation("androidx.compose.ui:ui-tooling:1.1.0") + // implementation 'com.github.centerzx:ShapeBlurView:1.0.3' implementation(project(":blurview")) } \ No newline at end of file diff --git a/app/src/main/java/net/center/blurview/demo/TestCompose.kt b/app/src/main/java/net/center/blurview/demo/TestCompose.kt new file mode 100644 index 0000000..2741041 --- /dev/null +++ b/app/src/main/java/net/center/blurview/demo/TestCompose.kt @@ -0,0 +1,32 @@ +package net.center.blurview.demo + +import androidx.compose.foundation.Image +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.offset +import androidx.compose.foundation.layout.width +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import net.center.blurview.ShapeBlurView +import net.center.blurview.compose.ShapeBlurView + +/** + * @author: orangeboy + * @createTime: 2022/10/31 19:25 + * @description: + */ +@Preview(showBackground = true) +@Composable +fun BlurComposeExample() { + Box { + Image(painter = painterResource(id = R.mipmap.test), contentDescription = null) + ShapeBlurView( + modifier = Modifier.height(100.dp).width(100.dp).offset(x = 30.dp, y = 30.dp), + config = ShapeBlurView.Config().apply { + this.cornerRadiusOverride = 50f + }) + } +} \ No newline at end of file diff --git a/blurview/build.gradle b/blurview/build.gradle index 505cbd4..8058911 100644 --- a/blurview/build.gradle +++ b/blurview/build.gradle @@ -1,7 +1,6 @@ plugins { id 'com.android.library' id 'org.jetbrains.kotlin.android' - id 'maven-publish'//用来推送aar到jitpack id 'pl.allegro.tech.build.axion-release' version '1.13.6'//用来发行aar版本的插件 } @@ -51,7 +50,7 @@ android { compileSdk 31 defaultConfig { - minSdk 19 + minSdk 21 targetSdk 31 consumerProguardFiles "consumer-rules.pro" @@ -70,8 +69,22 @@ android { kotlinOptions { jvmTarget = '1.8' } + composeOptions { + kotlinCompilerExtensionVersion '1.0.3' + } + + buildFeatures { + compose = true + } } dependencies { + implementation 'androidx.core:core-ktx:1.7.0' compileOnly 'androidx.appcompat:appcompat:1.4.1' + + implementation("androidx.compose.ui:ui:1.1.0") + implementation("androidx.compose.material:material:1.1.0") + implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.0") + implementation("androidx.compose.ui:ui-tooling-preview:1.1.0") + debugImplementation("androidx.compose.ui:ui-tooling:1.1.0") } \ No newline at end of file diff --git a/blurview/src/main/java/net/center/blurview/ShapeBlurView.java b/blurview/src/main/java/net/center/blurview/ShapeBlurView.java index 2516f19..8682534 100644 --- a/blurview/src/main/java/net/center/blurview/ShapeBlurView.java +++ b/blurview/src/main/java/net/center/blurview/ShapeBlurView.java @@ -58,7 +58,7 @@ public class ShapeBlurView extends View { private float mBlurRadius; public static final int DEFAULT_BORDER_COLOR = Color.WHITE; - private final BlurImpl mBlurImpl; + private BlurImpl mBlurImpl; private boolean mDirty; private Bitmap mBitmapToBlur, mBlurredBitmap; private Canvas mBlurringCanvas; @@ -80,7 +80,7 @@ public class ShapeBlurView extends View { private static int BLUR_IMPL; private int blurMode = BlurMode.MODE_RECTANGLE; - private final Paint mBitmapPaint; + private Paint mBitmapPaint; //圆形 相关 private float cx = 0, cy = 0, cRadius = 0; @@ -94,62 +94,105 @@ public class ShapeBlurView extends View { private static final float DEFAULT_BORDER_WIDTH = 0f; private final RectF mBorderRect = new RectF(); - private final Paint mBorderPaint; + private Paint mBorderPaint; private float mBorderWidth = 0; private ColorStateList mBorderColor = ColorStateList.valueOf(DEFAULT_BORDER_COLOR); private Matrix matrix = new Matrix(); private BitmapShader shader; + public static class Config { + public float blurRadius = 25.0f; + public float downSampleFactor = 4f; + public int overlayColor = 0x000000; + + public float cornerRadiusOverride = -1; + public float cornerTopLeft = -1; + public float cornerTopRight = -1; + public float cornerBottomRight = -1; + public float cornerBottomLeft = -1; + + public int blurMode = BlurMode.MODE_RECTANGLE; + + public float borderWidth = DEFAULT_BORDER_WIDTH; + public ColorStateList borderColor = ColorStateList.valueOf(DEFAULT_BORDER_COLOR); + } + + public ShapeBlurView(Context context, Config config) { + super(context); + mContext = context; + mBlurImpl = getBlurImpl(); + + initConfig(config); + + + } + public ShapeBlurView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; - // provide your own by override getBlurImpl() mBlurImpl = getBlurImpl(); + + Config config = new Config(); try { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ShapeBlurView); - mBlurRadius = a.getDimension(R.styleable.ShapeBlurView_blur_radius, + config.blurRadius = a.getDimension(R.styleable.ShapeBlurView_blur_radius, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, context.getResources().getDisplayMetrics())); - mDownSampleFactor = a.getFloat(R.styleable.ShapeBlurView_blur_down_sample, 4); - mOverlayColor = a.getColor(R.styleable.ShapeBlurView_blur_overlay_color, 0x000000); + config.downSampleFactor = a.getFloat(R.styleable.ShapeBlurView_blur_down_sample, 4); + config.overlayColor = a.getColor(R.styleable.ShapeBlurView_blur_overlay_color, 0x000000); - float cornerRadiusOverride = + config.cornerRadiusOverride = a.getDimensionPixelSize(R.styleable.ShapeBlurView_blur_corner_radius, -1); - mCornerRadii[BlurCorner.TOP_LEFT] = + config.cornerTopLeft = a.getDimensionPixelSize(R.styleable.ShapeBlurView_blur_corner_radius_top_left, -1); - mCornerRadii[BlurCorner.TOP_RIGHT] = + config.cornerTopRight = a.getDimensionPixelSize(R.styleable.ShapeBlurView_blur_corner_radius_top_right, -1); - mCornerRadii[BlurCorner.BOTTOM_RIGHT] = + config.cornerBottomRight = a.getDimensionPixelSize(R.styleable.ShapeBlurView_blur_corner_radius_bottom_right, -1); - mCornerRadii[BlurCorner.BOTTOM_LEFT] = + config.cornerBottomLeft = a.getDimensionPixelSize(R.styleable.ShapeBlurView_blur_corner_radius_bottom_left, -1); - initCornerData(cornerRadiusOverride); - blurMode = a.getInt(R.styleable.ShapeBlurView_blur_mode, BlurMode.MODE_RECTANGLE); - - mBorderWidth = a.getDimensionPixelSize(R.styleable.ShapeBlurView_blur_border_width, -1); - if (mBorderWidth < 0) { - mBorderWidth = DEFAULT_BORDER_WIDTH; - } - mBorderColor = a.getColorStateList(R.styleable.ShapeBlurView_blur_border_color); - if (mBorderColor == null) { - mBorderColor = ColorStateList.valueOf(DEFAULT_BORDER_COLOR); - } + config.blurMode = a.getInt(R.styleable.ShapeBlurView_blur_mode, BlurMode.MODE_RECTANGLE); + config.borderWidth = a.getDimensionPixelSize(R.styleable.ShapeBlurView_blur_border_width, -1); + config.borderColor = a.getColorStateList(R.styleable.ShapeBlurView_blur_border_color); a.recycle(); } catch (Exception e) { e.printStackTrace(); } + initConfig(config); + } + + private void initConfig(Config config) { + mBlurRadius = config.blurRadius; + mDownSampleFactor = config.downSampleFactor; + mOverlayColor = config.overlayColor; + + mCornerRadii[BlurCorner.TOP_LEFT] = config.cornerTopLeft; + mCornerRadii[BlurCorner.TOP_RIGHT] = config.cornerTopRight; + mCornerRadii[BlurCorner.BOTTOM_RIGHT] = config.cornerBottomRight; + mCornerRadii[BlurCorner.BOTTOM_LEFT] = config.cornerBottomLeft; + initCornerData(config.cornerRadiusOverride); + + blurMode = config.blurMode; + mBorderWidth = config.borderWidth; + if (mBorderWidth == -1) { + mBorderWidth = DEFAULT_BORDER_WIDTH; + } + + mBorderColor = config.borderColor; + if (mBorderColor == null) { + mBorderColor = ColorStateList.valueOf(DEFAULT_BORDER_COLOR); + } + + mBitmapPaint = new Paint(); // mBitmapPaint.setStyle(Paint.Style.FILL); mBitmapPaint.setAntiAlias(true); - mBorderPaint = new Paint(); mBorderPaint.setStyle(Paint.Style.STROKE); mBorderPaint.setAntiAlias(true); mBorderPaint.setColor(mBorderColor.getColorForState(getState(), DEFAULT_BORDER_COLOR)); mBorderPaint.setStrokeWidth(mBorderWidth); - -// matrix = new Matrix(); } private void initCornerData(float cornerRadiusOverride) { diff --git a/blurview/src/main/java/net/center/blurview/compose/ShapeBlurViewCompose.kt b/blurview/src/main/java/net/center/blurview/compose/ShapeBlurViewCompose.kt new file mode 100644 index 0000000..f9be461 --- /dev/null +++ b/blurview/src/main/java/net/center/blurview/compose/ShapeBlurViewCompose.kt @@ -0,0 +1,23 @@ +package net.center.blurview.compose + +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.viewinterop.AndroidView +import net.center.blurview.ShapeBlurView + +/** + * @author: orangeboy + * @createTime: 2022/10/31 19:12 + * @description: + */ +@Composable +fun ShapeBlurView( + modifier: Modifier = Modifier, + config: ShapeBlurView.Config = ShapeBlurView.Config()) { + AndroidView( + factory = { context -> + ShapeBlurView(context, config) + }, + modifier = modifier + ) +} \ No newline at end of file