Skip to content

Commit

Permalink
add: Adapt Jetpack Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
orangeboyChen committed Oct 31, 2022
1 parent df8aff9 commit 42f7061
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 30 deletions.
1 change: 0 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {

defaultConfig {
applicationId "net.center.blurview.demo"
minSdk 19
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
Expand All @@ -27,6 +27,14 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}

composeOptions {
kotlinCompilerExtensionVersion '1.0.3'
}

buildFeatures {
compose = true
}
}

dependencies {
Expand All @@ -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"))
}
32 changes: 32 additions & 0 deletions app/src/main/java/net/center/blurview/demo/TestCompose.kt
Original file line number Diff line number Diff line change
@@ -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
})
}
}
17 changes: 15 additions & 2 deletions blurview/build.gradle
Original file line number Diff line number Diff line change
@@ -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版本的插件
}
Expand Down Expand Up @@ -51,7 +50,7 @@ android {
compileSdk 31

defaultConfig {
minSdk 19
minSdk 21
targetSdk 31

consumerProguardFiles "consumer-rules.pro"
Expand All @@ -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")
}
95 changes: 69 additions & 26 deletions blurview/src/main/java/net/center/blurview/ShapeBlurView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
)
}

0 comments on commit 42f7061

Please sign in to comment.