Skip to content

Commit

Permalink
优化高斯模糊
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfusheng committed Jun 26, 2018
1 parent 4a64356 commit 411dcb4
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,18 @@ protected void drawableStateChanged() {
}
}

public void setEnableState(boolean enableState) {
public GlideImageView enableState(boolean enableState) {
this.enableState = enableState;
return this;
}

public void setPressedAlpha(float pressedAlpha) {
public GlideImageView pressedAlpha(float pressedAlpha) {
this.pressedAlpha = pressedAlpha;
return this;
}

public void setUnableAlpha(float unableAlpha) {
public GlideImageView unableAlpha(float unableAlpha) {
this.unableAlpha = unableAlpha;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.sunfusheng.transformation;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
Expand All @@ -19,24 +22,27 @@ public class BlurTransformation extends BitmapTransformation {
private final String ID = getClass().getName();

private static int MAX_RADIUS = 25;
private static int DEFAULT_DOWN_SAMPLING = 1;
private static int DEFAULT_SAMPLING = 1;

private int radius;
private int sampling;
private Context context;
private int radius; //模糊半径0~25
private int sampling; //取样0~25

public BlurTransformation() {
this(MAX_RADIUS, DEFAULT_DOWN_SAMPLING);
public BlurTransformation(Context context) {
this(context, MAX_RADIUS, DEFAULT_SAMPLING);
}

public BlurTransformation(int radius) {
this(radius, DEFAULT_DOWN_SAMPLING);
public BlurTransformation(Context context, int radius) {
this(context, radius, DEFAULT_SAMPLING);
}

public BlurTransformation(int radius, int sampling) {
this.radius = radius;
this.sampling = sampling;
public BlurTransformation(Context context, int radius, int sampling) {
this.context = context;
this.radius = radius > MAX_RADIUS ? MAX_RADIUS : radius;
this.sampling = sampling > MAX_RADIUS ? MAX_RADIUS : sampling;
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
int width = toTransform.getWidth();
Expand All @@ -50,7 +56,11 @@ protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform
Paint paint = new Paint();
paint.setFlags(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(toTransform, 0, 0, paint);
bitmap = BlurUtils.blur(bitmap, radius, true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
bitmap = BlurUtils.rsBlur(context, bitmap, radius);
} else {
bitmap = BlurUtils.blur(bitmap, radius);
}
return bitmap;
}

Expand Down
38 changes: 26 additions & 12 deletions GlideImageView/src/main/java/com/sunfusheng/util/BlurUtils.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
package com.sunfusheng.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.support.annotation.RequiresApi;

/**
* @author sunfusheng on 2018/6/25.
*/
public class BlurUtils {

public static Bitmap blur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap) {
Bitmap bitmap;
if (canReuseInBitmap) {
bitmap = sentBitmap;
} else {
bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap rsBlur(Context context, Bitmap toTransform, int radius) {
RenderScript renderScript = RenderScript.create(context);
Allocation input = Allocation.createFromBitmap(renderScript, toTransform);
Allocation output = Allocation.createTyped(renderScript, input.getType());
ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
scriptIntrinsicBlur.setInput(input);
scriptIntrinsicBlur.setRadius(radius);
scriptIntrinsicBlur.forEach(output);
output.copyTo(toTransform);
renderScript.destroy();
return toTransform;
}

public static Bitmap blur(Bitmap toTransform, int radius) {
if (radius < 1) {
return (null);
}

int w = bitmap.getWidth();
int h = bitmap.getHeight();
int w = toTransform.getWidth();
int h = toTransform.getHeight();

int[] pix = new int[w * h];
bitmap.getPixels(pix, 0, w, 0, 0, w, h);
toTransform.getPixels(pix, 0, w, 0, 0, w, h);

int wm = w - 1;
int hm = h - 1;
Expand Down Expand Up @@ -207,7 +221,7 @@ public static Bitmap blur(Bitmap sentBitmap, int radius, boolean canReuseInBitma
}
}

bitmap.setPixels(pix, 0, w, 0, 0, w, h);
return bitmap;
toTransform.setPixels(pix, 0, w, 0, 0, w, h);
return toTransform;
}
}
11 changes: 0 additions & 11 deletions GlideImageView/src/main/res/values/style.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="ShapeImageViewStyle">
<attr name="riv_border_color" format="color"/>
<attr name="riv_border_width" format="dimension"/>
<attr name="riv_corner_radius" format="dimension"/>
<attr name="riv_is_circle" format="boolean"/>
<attr name="riv_pressed_border_color" format="color"/>
<attr name="riv_pressed_border_width" format="dimension"/>
<attr name="riv_pressed_mask_color" format="color"/>
<attr name="riv_pressed_mode_enabled" format="boolean"/>
</declare-styleable>

<declare-styleable name="CircleProgressView">
<attr name="cpv_progressNormalColor" format="color"/>
<attr name="cpv_progressReachColor" format="color"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

private void line1() {
image11.load(url1);
image11.enableState(true).load(url1);
image12.loadCircle(url1);
image13.load(url2, R.mipmap.image_loading);
image14.load(url2, R.mipmap.image_loading, 10);
Expand Down Expand Up @@ -140,7 +140,7 @@ private void line3() {
}

private void line4() {
image41.fitCenter().load(url3, R.mipmap.image_loading, new BlurTransformation(25, 1));
image41.fitCenter().load(url3, R.mipmap.image_loading, new BlurTransformation(this, 25, 1));

image41.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, SingleImageActivity.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType)
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
NewsModel entity = list.get(position);
viewHolder.tvTitle.setText(entity.getTitle());
viewHolder.imageView.load(entity.getImage_url(), R.color.placeholder);
viewHolder.imageView.centerCrop().load(entity.getImage_url(), R.color.placeholder);
viewHolder.llRootView.setOnClickListener(v -> Toast.makeText(mContext, entity.getTitle(), Toast.LENGTH_SHORT).show());
}

Expand Down
28 changes: 7 additions & 21 deletions Sample/src/main/res/layout/item_right_image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<LinearLayout
android:id="@+id/ll_root_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -21,32 +20,19 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/d2"
android:layout_toLeftOf="@+id/prl_image"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/giv"
android:ellipsize="end"
android:maxLines="2"
android:textColor="@color/c1"
android:textSize="@dimen/f6"
tools:text="标题"/>

<android.support.constraint.ConstraintLayout
android:id="@+id/prl_image"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true">

<com.sunfusheng.GlideImageView
android:id="@+id/giv"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="16:9"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:riv_pressed_mask_color="@color/transparent30"/>
</android.support.constraint.ConstraintLayout>
<com.sunfusheng.GlideImageView
android:id="@+id/giv"
android:layout_width="120dp"
android:layout_height="90dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>

<View
Expand Down
5 changes: 1 addition & 4 deletions Sample/src/main/res/layout/layout_roundimageview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
android:id="@+id/imageView"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
app:riv_is_circle="true"
app:riv_pressed_mask_color="@color/transparent10"
app:riv_pressed_mode_enabled="true"/>
android:layout_centerInParent="true"/>

</RelativeLayout>

0 comments on commit 411dcb4

Please sign in to comment.