Search in sources :

Example 1 with RenderScript

use of android.support.v8.renderscript.RenderScript in project AndroidViewHover by daimajia.

the class Blur method apply.

public static Bitmap apply(Context context, Bitmap sentBitmap, int radius) {
    final Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    final RenderScript rs = RenderScript.create(context);
    final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(radius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bitmap);
    sentBitmap.recycle();
    rs.destroy();
    input.destroy();
    output.destroy();
    script.destroy();
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) RenderScript(android.support.v8.renderscript.RenderScript) Allocation(android.support.v8.renderscript.Allocation) ScriptIntrinsicBlur(android.support.v8.renderscript.ScriptIntrinsicBlur)

Example 2 with RenderScript

use of android.support.v8.renderscript.RenderScript in project Carbon by ZieIony.

the class ShadowGenerator method generateShadow.

public static Shadow generateShadow(View view, float elevation) {
    if (!software && renderScript == null) {
        try {
            renderScript = RenderScript.create(view.getContext());
            blurShader = ScriptIntrinsicBlur.create((RenderScript) renderScript, Element.U8_4((RenderScript) renderScript));
        } catch (Error ignore) {
            software = true;
        }
    }
    CornerView cornerView = (CornerView) view;
    int e = (int) Math.ceil(elevation);
    int c = (int) Math.max(e, cornerView.getCornerRadius());
    for (Object o : shadowSet) {
        Shadow s = (Shadow) o;
        if (s != null && s.elevation == elevation && s.c == c)
            return s;
    }
    Bitmap bitmap;
    int bitmapSize = e * 2 + 2 * c + 1;
    bitmap = Bitmap.createBitmap(bitmapSize, bitmapSize, Bitmap.Config.ARGB_8888);
    Canvas shadowCanvas = new Canvas(bitmap);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(0xffffffff);
    roundRect.set(e, e, bitmapSize - e, bitmapSize - e);
    shadowCanvas.drawRoundRect(roundRect, c, c, paint);
    blur(bitmap, elevation);
    Shadow shadow = new Shadow(bitmap, elevation, c);
    shadowSet.add(shadow);
    return shadow;
}
Also used : RenderScript(android.support.v8.renderscript.RenderScript) Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) CornerView(carbon.widget.CornerView) Paint(android.graphics.Paint)

Example 3 with RenderScript

use of android.support.v8.renderscript.RenderScript in project Carbon by ZieIony.

the class ShadowGenerator method blurRenderScript.

private static void blurRenderScript(Bitmap bitmap, float radius) {
    Allocation inAllocation = Allocation.createFromBitmap((RenderScript) renderScript, bitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    Allocation outAllocation = Allocation.createTyped((RenderScript) renderScript, inAllocation.getType());
    ((ScriptIntrinsicBlur) blurShader).setRadius(radius);
    ((ScriptIntrinsicBlur) blurShader).setInput(inAllocation);
    ((ScriptIntrinsicBlur) blurShader).forEach(outAllocation);
    outAllocation.copyTo(bitmap);
}
Also used : Allocation(android.support.v8.renderscript.Allocation) ScriptIntrinsicBlur(android.support.v8.renderscript.ScriptIntrinsicBlur)

Example 4 with RenderScript

use of android.support.v8.renderscript.RenderScript in project RecyclerViewCardGallery by huazhiyuan2008.

the class BlurBitmapUtils method getBlurBitmap.

/**
     * 得到模糊后的bitmap
     * thanks http://wl9739.github.io/2016/07/14/教你一分钟实现模糊效果/
     *
     * @param context
     * @param bitmap
     * @param radius
     * @return
     */
public static Bitmap getBlurBitmap(Context context, Bitmap bitmap, int radius) {
    // 将缩小后的图片做为预渲染的图片。
    Bitmap inputBitmap = Bitmap.createScaledBitmap(bitmap, SCALED_WIDTH, SCALED_HEIGHT, false);
    // 创建一张渲染后的输出图片。
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
    // 创建RenderScript内核对象
    RenderScript rs = RenderScript.create(context);
    // 创建一个模糊效果的RenderScript的工具对象
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间。
    // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去。
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    // 设置渲染的模糊程度, 25f是最大模糊度
    blurScript.setRadius(radius);
    // 设置blurScript对象的输入内存
    blurScript.setInput(tmpIn);
    // 将输出数据保存到输出内存中
    blurScript.forEach(tmpOut);
    // 将数据填充到Allocation中
    tmpOut.copyTo(outputBitmap);
    return outputBitmap;
}
Also used : Bitmap(android.graphics.Bitmap) RenderScript(android.support.v8.renderscript.RenderScript) ScriptIntrinsicBlur(android.support.v8.renderscript.ScriptIntrinsicBlur) Allocation(android.support.v8.renderscript.Allocation)

Example 5 with RenderScript

use of android.support.v8.renderscript.RenderScript in project JamsMusicPlayer by psaravan.

the class PicassoBlurTransformer method transform.

@Override
public Bitmap transform(Bitmap bitmap) {
    // Create another bitmap that will hold the results of the filter.
    Bitmap blurredBitmap = Bitmap.createBitmap(bitmap);
    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
    Allocation output = Allocation.createTyped(rs, input.getType());
    // Load up an instance of the specific script that we want to use.
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);
    // Set the blur radius
    script.setRadius(10);
    // Start the ScriptIntrinisicBlur
    script.forEach(output);
    // Copy the output to the blurred bitmap
    output.copyTo(blurredBitmap);
    return blurredBitmap;
}
Also used : Bitmap(android.graphics.Bitmap) Allocation(android.support.v8.renderscript.Allocation) ScriptIntrinsicBlur(android.support.v8.renderscript.ScriptIntrinsicBlur)

Aggregations

Bitmap (android.graphics.Bitmap)11 RenderScript (android.support.v8.renderscript.RenderScript)9 Allocation (android.support.v8.renderscript.Allocation)8 ScriptIntrinsicBlur (android.support.v8.renderscript.ScriptIntrinsicBlur)7 BitmapFactory (android.graphics.BitmapFactory)3 BitmapDrawable (android.graphics.drawable.BitmapDrawable)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Canvas (android.graphics.Canvas)2 Paint (android.graphics.Paint)2 PorterDuffColorFilter (android.graphics.PorterDuffColorFilter)1 RSRuntimeException (android.support.v8.renderscript.RSRuntimeException)1 CornerView (carbon.widget.CornerView)1