package com.jp.wasabeef.glide.transformations.internal; import android.annotation.TargetApi; import android.graphics.Bitmap; import android.os.Build; import android.renderscript.Allocation; import android.renderscript.Element; import android.renderscript.RSRuntimeException; import android.renderscript.RenderScript; import android.renderscript.ScriptIntrinsicBlur; import com.simplecity.amp_library.ShuttleApplication; /** * Copyright (C) 2015 Wasabeef * <p> * 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 * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * 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. */ public class RSBlur { private static RSBlur sInstance; private RenderScript renderScript; @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public static RSBlur getInstance() { if (sInstance == null) { sInstance = new RSBlur(); } return sInstance; } private RSBlur() { renderScript = RenderScript.create(ShuttleApplication.getInstance()); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public Bitmap blur(Bitmap bitmap, int radius) throws RSRuntimeException { Allocation input = null; Allocation output = null; ScriptIntrinsicBlur blur = null; try { input = Allocation.createFromBitmap(renderScript, bitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); output = Allocation.createTyped(renderScript, input.getType()); blur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript)); blur.setInput(input); blur.setRadius(radius); blur.forEach(output); output.copyTo(bitmap); } finally { if (input != null) { input.destroy(); } if (output != null) { output.destroy(); } if (blur != null) { blur.destroy(); } } return bitmap; } }