Skip to content

Commit

Permalink
优化代码,减少在onDraw里面新建对象
Browse files Browse the repository at this point in the history
  • Loading branch information
centerzx committed May 26, 2022
1 parent 7513dc7 commit bd5fbd6
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions blurview/src/main/java/net/center/blurview/ShapeBlurView.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public class ShapeBlurView extends View {
private final Paint mBorderPaint;
private float mBorderWidth = 0;
private ColorStateList mBorderColor = ColorStateList.valueOf(DEFAULT_BORDER_COLOR);

private Matrix matrix;
private BitmapShader shader;

public ShapeBlurView(Context context, AttributeSet attrs) {
super(context, attrs);
Expand Down Expand Up @@ -136,6 +137,7 @@ public ShapeBlurView(Context context, AttributeSet attrs) {

a.recycle();
} catch (Exception e) {
e.printStackTrace();
}
mBitmapPaint = new Paint();
// mBitmapPaint.setStyle(Paint.Style.FILL);
Expand All @@ -146,6 +148,8 @@ public ShapeBlurView(Context context, AttributeSet attrs) {
mBorderPaint.setAntiAlias(true);
mBorderPaint.setColor(mBorderColor.getColorForState(getState(), DEFAULT_BORDER_COLOR));
mBorderPaint.setStrokeWidth(mBorderWidth);

matrix = new Matrix();
}

private void initCornerData(float cornerRadiusOverride) {
Expand Down Expand Up @@ -401,6 +405,12 @@ private void releaseBitmap() {
mBlurredBitmap.recycle();
mBlurredBitmap = null;
}
if (matrix != null) {
matrix = null;
}
if (shader != null) {
shader = null;
}
}

protected void release() {
Expand Down Expand Up @@ -462,6 +472,7 @@ protected boolean prepare() {
}

protected void blur(Bitmap bitmapToBlur, Bitmap blurredBitmap) {
shader = new BitmapShader(blurredBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mBlurImpl.blur(bitmapToBlur, blurredBitmap);
}

Expand Down Expand Up @@ -627,8 +638,9 @@ private void drawOvalRectBitmap(Canvas canvas, Bitmap blurBitmap, int overlayCol
mRectFDst.bottom = getHeight();
mBitmapPaint.reset();
mBitmapPaint.setAntiAlias(true);
BitmapShader shader = new BitmapShader(blurBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Matrix matrix = new Matrix();
if (shader == null) {
shader = new BitmapShader(blurBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
}
matrix.postScale(mRectFDst.width() / blurBitmap.getWidth(), mRectFDst.height() / blurBitmap.getHeight());
shader.setLocalMatrix(matrix);
mBitmapPaint.setShader(shader);
Expand Down Expand Up @@ -662,8 +674,9 @@ private void drawCircleRectBitmap(Canvas canvas, Bitmap blurBitmap, int overlayC
mRectSrc.bottom = blurBitmap.getHeight();
mBitmapPaint.reset();
mBitmapPaint.setAntiAlias(true);
BitmapShader shader = new BitmapShader(blurBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Matrix matrix = new Matrix();
if (shader == null) {
shader = new BitmapShader(blurBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
}
matrix.postScale(mRectFDst.width() / mRectSrc.width(), mRectFDst.height() / mRectSrc.height());
shader.setLocalMatrix(matrix);
mBitmapPaint.setShader(shader);
Expand Down

0 comments on commit bd5fbd6

Please sign in to comment.