Ejemplo n.º 1
0
  public static Drawable createBlurredImageFromBitmap(
      Bitmap bitmap, Context context, int inSampleSize) {

    RenderScript rs = RenderScript.create(context);
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = inSampleSize;

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] imageInByte = stream.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
    Bitmap blurTemplate = BitmapFactory.decodeStream(bis, null, options);

    final android.support.v8.renderscript.Allocation input =
        android.support.v8.renderscript.Allocation.createFromBitmap(rs, blurTemplate);
    final android.support.v8.renderscript.Allocation output =
        android.support.v8.renderscript.Allocation.createTyped(rs, input.getType());
    final android.support.v8.renderscript.ScriptIntrinsicBlur script =
        android.support.v8.renderscript.ScriptIntrinsicBlur.create(
            rs, android.support.v8.renderscript.Element.U8_4(rs));
    script.setRadius(8f);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(blurTemplate);
    Drawable dr = new BitmapDrawable(blurTemplate);

    return dr;
  }
Ejemplo n.º 2
0
  private static void blurRenderScript(Bitmap bitmap, float radius) {
    Allocation inAllocation =
        Allocation.createFromBitmap(
            renderScript, bitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    Allocation outAllocation = Allocation.createTyped(renderScript, inAllocation.getType());

    blurShader.setRadius(radius);
    blurShader.setInput(inAllocation);
    blurShader.forEach(outAllocation);

    outAllocation.copyTo(bitmap);
  }
Ejemplo n.º 3
0
  public static Bitmap blurBitmap(Bitmap bitmapToBlur, Context context) {
    RenderScript rs = RenderScript.create(context);

    // use this constructor for best performance, because it uses USAGE_SHARED mode which reuses
    // memory
    final Allocation input = Allocation.createFromBitmap(rs, bitmapToBlur);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(16f);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bitmapToBlur);
    return bitmapToBlur;
  }
Ejemplo n.º 4
0
  public static Shadow generateShadow(View view, float elevation) {
    if (!software && renderScript == null) {
      try {
        renderScript = RenderScript.create(view.getContext());
        blurShader = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
      } catch (RSRuntimeException ignore) {
        software = true;
      }
    }

    CornerView cornerView = (CornerView) view;

    int e = (int) Math.ceil(elevation);
    int c = Math.max(e, cornerView.getCornerRadius());

    Bitmap bitmap;
    bitmap = Bitmap.createBitmap(e * 2 + 2 * c + 1, e * 2 + 2 * c + 1, Bitmap.Config.ARGB_8888);

    Canvas shadowCanvas = new Canvas(bitmap);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(0xff000000);

    roundRect.set(e, e, bitmap.getWidth() - e, bitmap.getHeight() - e);
    shadowCanvas.drawRoundRect(roundRect, c, c, paint);

    blur(bitmap, elevation / 2);

    return new NinePatchShadow(bitmap, elevation, c);
  }
  private Bitmap BlurImage(Bitmap input, int radius) {
    RenderScript rsScript = RenderScript.create(getActivity());
    Allocation alloc = Allocation.createFromBitmap(rsScript, input);

    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rsScript, Element.U8_4(rsScript));
    blur.setRadius(radius);
    blur.setInput(alloc);

    Bitmap result = Bitmap.createBitmap(input.getWidth(), input.getHeight(), input.getConfig());
    Allocation outAlloc = Allocation.createFromBitmap(rsScript, result);
    blur.forEach(outAlloc);
    outAlloc.copyTo(result);

    rsScript.destroy();
    return result;
  }
Ejemplo n.º 6
0
  @SuppressLint("NewApi")
  public static Bitmap blur(Context context, Bitmap sentBitmap, int radius) {
    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);
    return bitmap;
  }
Ejemplo n.º 7
0
 protected void blur() {
   mBlurInput.copyFrom(mBitmapToBlur);
   mBlurScript.setInput(mBlurInput);
   mBlurScript.forEach(mBlurOutput);
   mBlurOutput.copyTo(mBlurredBitmap);
 }
Ejemplo n.º 8
0
 private void initRenderScript(Context context) {
   mRenderScript = RenderScript.create(context);
   mBlurScript = ScriptIntrinsicBlur.create(mRenderScript, Element.U8_4(mRenderScript));
   mBlurScript.setRadius(mBlurRadius);
 }
Ejemplo n.º 9
0
 void setBlurRadius(int radius) {
   mBlurScript.setRadius(radius);
 }