Java Code Examples for android.graphics.Bitmap.setHasAlpha()

The following are Jave code examples for showing how to use setHasAlpha() of the android.graphics.Bitmap class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: GitHub   File: TransformationUtilsTest.java   View Source Code Vote up 6 votes
@Test
public void testFitCenterSetsOutBitmapToHaveAlphaIfInBitmapHasAlphaAndOutBitmapIsReused() {
  Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

  Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
  reset(bitmapPool);
  when(bitmapPool.get(eq(toReuse.getWidth()), eq(toReuse.getHeight()), eq(toReuse.getConfig())))
      .thenReturn(toReuse);

  toReuse.setHasAlpha(false);
  toTransform.setHasAlpha(true);

  Bitmap result = TransformationUtils.fitCenter(bitmapPool, toTransform, toReuse.getWidth(),
      toReuse.getHeight());

  assertEquals(toReuse, result);
  assertTrue(result.hasAlpha());
}
 
Example 2
Project: GitHub   File: TransformationUtilsTest.java   View Source Code Vote up 6 votes
@Test
public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlphaAndOutBitmapIsReused() {
  Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

  Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
  reset(bitmapPool);
  when(bitmapPool.get(eq(50), eq(50), eq(Bitmap.Config.ARGB_8888)))
      .thenReturn(toReuse);

  toReuse.setHasAlpha(false);
  toTransform.setHasAlpha(true);

  Bitmap result = TransformationUtils.centerCrop(bitmapPool, toTransform, toReuse.getWidth(),
      toReuse.getHeight());

  assertEquals(toReuse, result);
  assertTrue(result.hasAlpha());
}
 
Example 3
Project: GitHub   File: TransformationUtilsTest.java   View Source Code Vote up 6 votes
@Test
public void
testCenterCropSetsOutBitmapToNotHaveAlphaIfInBitmapDoesNotHaveAlphaAndOutBitmapIsReused() {
  Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

  Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
  reset(bitmapPool);
  when(bitmapPool.get(eq(50), eq(50), eq(Bitmap.Config.ARGB_8888))).thenReturn(toReuse);

  toReuse.setHasAlpha(true);
  toTransform.setHasAlpha(false);

  Bitmap result = TransformationUtils.centerCrop(bitmapPool, toTransform, toReuse.getWidth(),
      toReuse.getHeight());

  assertEquals(toReuse, result);
  assertFalse(result.hasAlpha());
}
 
Example 4
Project: airgram   File: Bitmaps.java   View Source Code Vote up 6 votes
public static Bitmap createBitmap(int width, int height, Bitmap.Config config) {
    Bitmap bitmap;
    if (Build.VERSION.SDK_INT < 21) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDither = true;
        options.inPreferredConfig = config;
        options.inPurgeable = true;
        options.inSampleSize = 1;
        options.inMutable = true;
        byte[] array = jpegData.get();
        array[76] = (byte) (height >> 8);
        array[77] = (byte) (height & 0x00ff);
        array[78] = (byte) (width >> 8);
        array[79] = (byte) (width & 0x00ff);
        bitmap = BitmapFactory.decodeByteArray(array, 0, array.length, options);
        Utilities.pinBitmap(bitmap);
        bitmap.setHasAlpha(true);
        bitmap.eraseColor(0);
    } else {
        bitmap = Bitmap.createBitmap(width, height, config);
    }
    if (config == Bitmap.Config.ARGB_8888 || config == Bitmap.Config.ARGB_4444) {
        bitmap.eraseColor(Color.TRANSPARENT);
    }
    return bitmap;
}
 
Example 5
Project: react-native-webgl-view-shot   File: RNWebGLTextureView.java   View Source Code Vote up 6 votes
public void run() {
    Bitmap bitmap;
    try {
        bitmap = captureView(view);
    }
    catch (Exception e) {
        return;
    }
    if (yflip) {
        Matrix matrix = new Matrix();
        matrix.postScale(1, -1);
        boolean hasAlpha = bitmap.hasAlpha();
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        bitmap.setHasAlpha(hasAlpha);
    }
    int[] textures = new int[1];
    glGenTextures(1, textures, 0);
    glBindTexture(GL_TEXTURE_2D, textures[0]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    GLUtils.texImage2D(GL_TEXTURE_2D, 0, bitmap, 0);
    this.attachTexture(textures[0]);
}
 
Example 6
Project: GitHub   File: TransformationUtilsTest.java   View Source Code Vote up 5 votes
@Test
public void testFitCenterSetsOutBitmapToHaveAlphaIfInBitmapHasAlpha() {
  Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

  toTransform.setHasAlpha(true);

  Bitmap result = TransformationUtils.fitCenter(bitmapPool, toTransform,
      toTransform.getWidth() / 2, toTransform.getHeight() / 2);

  assertTrue(result.hasAlpha());
}
 
Example 7
Project: GitHub   File: TransformationUtilsTest.java   View Source Code Vote up 5 votes
@Test
public void testCenterCropSetsOutBitmapToNotHaveAlphaIfInBitmapDoesNotHaveAlpha() {
  Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

  toTransform.setHasAlpha(false);

  Bitmap result = TransformationUtils.centerCrop(bitmapPool, toTransform,
      toTransform.getWidth() / 2, toTransform.getHeight() / 2);

  assertFalse(result.hasAlpha());
}
 
Example 8
Project: GitHub   File: TransformationUtilsTest.java   View Source Code Vote up 5 votes
@Test
public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlpha() {
  Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

  toTransform.setHasAlpha(true);

  Bitmap result = TransformationUtils.centerCrop(bitmapPool, toTransform,
      toTransform.getWidth() / 2, toTransform.getHeight() / 2);

  assertTrue(result.hasAlpha());
}
 
Example 9
Project: GitHub   File: TransformationUtilsTest.java   View Source Code Vote up 5 votes
@Test
public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlpha() {
  Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

  toTransform.setHasAlpha(true);

  Bitmap result = TransformationUtils.centerCrop(bitmapPool, toTransform,
      toTransform.getWidth() / 2, toTransform.getHeight() / 2);

  assertTrue(result.hasAlpha());
}
 
Example 10
Project: GitHub   File: PlatformBitmapFactory.java   View Source Code Vote up 5 votes
/**
 * Creates a bitmap with the specified width and height.  Its initial density is
 * determined from the given DisplayMetrics.
 *
 * @param display Display metrics for the display this bitmap will be drawn on
 * @param width The width of the bitmap
 * @param height The height of the bitmap
 * @param config The bitmap config to create
 * @param hasAlpha If the bitmap is ARGB_8888 this flag can be used to mark the bitmap as opaque
 * Doing so will clear the bitmap in black instead of transparent
 * @param callerContext the Tag to track who create the Bitmap
 * @return a reference to the bitmap
 * @throws IllegalArgumentException if the width or height are <= 0
 * @throws TooManyBitmapsException if the pool is full
 * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
 */
private CloseableReference<Bitmap> createBitmap(
    DisplayMetrics display,
    int width,
    int height,
    Bitmap.Config config,
    boolean hasAlpha,
    @Nullable Object callerContext) {
  checkWidthHeight(width, height);
  CloseableReference<Bitmap> bitmapRef = createBitmapInternal(width, height, config);

  Bitmap bitmap = bitmapRef.get();
  if (display != null) {
    bitmap.setDensity(display.densityDpi);
  }

  if (Build.VERSION.SDK_INT >= 12) {
    bitmap.setHasAlpha(hasAlpha);
  }

  if (config == Bitmap.Config.ARGB_8888 && !hasAlpha) {
    bitmap.eraseColor(0xff000000);
  }

  addBitmapReference(bitmapRef.get(), callerContext);
  return bitmapRef;
}
 
Example 11
Project: GitHub   File: TransformationUtilsTest.java   View Source Code Vote up 5 votes
@Test
public void testFitCenterSetsOutBitmapToHaveAlphaIfInBitmapHasAlpha() {
  Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

  toTransform.setHasAlpha(true);

  Bitmap result = TransformationUtils.fitCenter(bitmapPool, toTransform,
      toTransform.getWidth() / 2, toTransform.getHeight() / 2);

  assertTrue(result.hasAlpha());
}
 
Example 12
Project: RNLearn_Project1   File: ReactImageView.java   View Source Code Vote up 5 votes
@Override
public void process(Bitmap output, Bitmap source) {
  cornerRadii(sComputedCornerRadii);

  output.setHasAlpha(true);
  if (FloatUtil.floatsEqual(sComputedCornerRadii[0], 0f) &&
      FloatUtil.floatsEqual(sComputedCornerRadii[1], 0f) &&
      FloatUtil.floatsEqual(sComputedCornerRadii[2], 0f) &&
      FloatUtil.floatsEqual(sComputedCornerRadii[3], 0f)) {
    super.process(output, source);
    return;
  }
  Paint paint = new Paint();
  paint.setAntiAlias(true);
  paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
  Canvas canvas = new Canvas(output);

  float[] radii = new float[8];

  getRadii(source, sComputedCornerRadii, radii);

  Path pathForBorderRadius = new Path();

  pathForBorderRadius.addRoundRect(
      new RectF(0, 0, source.getWidth(), source.getHeight()),
      radii,
      Path.Direction.CW);

  canvas.drawPath(pathForBorderRadius, paint);
}
 
Example 13
Project: GitHub   File: TransformationUtils.java   View Source Code Vote up 5 votes
/**
 * Creates a bitmap from a source bitmap and rounds the corners.
 *
 * <p>This method does <em>NOT</em> resize the given {@link Bitmap}, it only rounds it's corners.
 * To both resize and round the corners of an image, consider
 * {@link com.bumptech.glide.request.RequestOptions#transforms(Transformation[])} and/or
 * {@link com.bumptech.glide.load.MultiTransformation}.
 *
 * @param inBitmap the source bitmap to use as a basis for the created bitmap.
 * @param roundingRadius the corner radius to be applied (in device-specific pixels).
 * @return a {@link Bitmap} similar to inBitmap but with rounded corners.
 * @throws IllegalArgumentException if roundingRadius, width or height is 0 or less.
 */
public static Bitmap roundedCorners(
    @NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int roundingRadius) {
  Preconditions.checkArgument(roundingRadius > 0, "roundingRadius must be greater than 0.");

  // Alpha is required for this transformation.
  Bitmap toTransform = getAlphaSafeBitmap(pool, inBitmap);
  Bitmap result =
      pool.get(toTransform.getWidth(), toTransform.getHeight(), Bitmap.Config.ARGB_8888);

  result.setHasAlpha(true);

  BitmapShader shader = new BitmapShader(toTransform, Shader.TileMode.CLAMP,
      Shader.TileMode.CLAMP);
  Paint paint = new Paint();
  paint.setAntiAlias(true);
  paint.setShader(shader);
  RectF rect = new RectF(0, 0, result.getWidth(), result.getHeight());
  BITMAP_DRAWABLE_LOCK.lock();
  try {
    Canvas canvas = new Canvas(result);
    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    canvas.drawRoundRect(rect, roundingRadius, roundingRadius, paint);
    clear(canvas);
  } finally {
    BITMAP_DRAWABLE_LOCK.unlock();
  }

  if (!toTransform.equals(inBitmap)) {
    pool.put(toTransform);
  }

  return result;
}
 
Example 14
Project: GitHub   File: CircleCropTest.java   View Source Code Vote up 5 votes
private Bitmap createBitmapWithRedCircle(int width, int height) {
  int minEdge = Math.min(width, height);
  float radius = minEdge / 2f;

  Bitmap result = Bitmap.createBitmap(minEdge, minEdge, Bitmap.Config.ARGB_8888);
  result.setHasAlpha(true);
  Canvas canvas = new Canvas(result);
  Paint paint = new Paint();
  paint.setAntiAlias(true);
  paint.setColor(Color.RED);

  canvas.drawCircle(radius, radius, radius, paint);
  return result;
}
 
Example 15
Project: TextEmoji   File: TextEmoji.java   View Source Code Vote up 5 votes
public Bitmap getBitmap(boolean isAlpha) {
    Bitmap bitmap = Bitmap.createBitmap(DisplayUtils.dp2px(getContext(), WIDTH),
            DisplayUtils.dp2px(getContext(), HEIGHT),
            Bitmap.Config.ARGB_8888);
    bitmap.setHasAlpha(isAlpha);
    Canvas canvas = new Canvas(bitmap);
    if (!isAlpha) {
        canvas.drawColor(Color.WHITE);
    }
    draw(canvas);
    return bitmap;
}
 
Example 16
Project: pc-android-controller-android   File: ScreentShotUtil.java   View Source Code Vote up 4 votes
/**
 * Takes Reboot screenshot of the current display and shows an animation.
 */
@SuppressLint("NewApi")
public void takeScreenshot(Context context, String fileFullPath)
{
    if(fileFullPath == ""){
        format = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = format.format(new Date(System.currentTimeMillis())) + ".png";
        fileFullPath = "/data/local/tmp/" + fileName;
    }

    if(ShellUtils.checkRootPermission()){
        if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
            ShellUtils.execCommand("/system/bin/screencap -p "+ fileFullPath,true);
        }
    }
    else {
        if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2 && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
            wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            mDisplay = wm.getDefaultDisplay();
            mDisplayMatrix = new Matrix();
            mDisplayMetrics = new DisplayMetrics();
            // We need to orient the screenshot correctly (and the Surface api seems to take screenshots
            // only in the natural orientation of the device :!)
            mDisplay.getRealMetrics(mDisplayMetrics);
            float[] dims =
                    {
                            mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels
                    };
            float degrees = getDegreesForRotation(mDisplay.getRotation());
            boolean requiresRotation = (degrees > 0);
            if (requiresRotation)
            {
                // Get the dimensions of the device in its native orientation
                mDisplayMatrix.reset();
                mDisplayMatrix.preRotate(-degrees);
                mDisplayMatrix.mapPoints(dims);
                dims[0] = Math.abs(dims[0]);
                dims[1] = Math.abs(dims[1]);
            }

            Bitmap mScreenBitmap = screenShot((int) dims[0], (int) dims[1]);
            if (requiresRotation)
            {
                // Rotate the screenshot to the current orientation
                Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
                        Bitmap.Config.ARGB_8888);
                Canvas c = new Canvas(ss);
                c.translate(ss.getWidth() / 2, ss.getHeight() / 2);
                c.rotate(degrees);
                c.translate(-dims[0] / 2, -dims[1] / 2);
                c.drawBitmap(mScreenBitmap, 0, 0, null);
                c.setBitmap(null);
                mScreenBitmap = ss;
                if (ss != null && !ss.isRecycled())
                {
                    ss.recycle();
                }
            }

            // If we couldn't take the screenshot, notify the user
            if (mScreenBitmap == null)
            {
                Toast.makeText(context, "screen shot fail", Toast.LENGTH_SHORT).show();
            }

            // Optimizations
            mScreenBitmap.setHasAlpha(false);
            mScreenBitmap.prepareToDraw();

            saveBitmap2file(context, mScreenBitmap, fileFullPath);
        }
    }

}
 
Example 17
Project: RLibrary   File: RImageView.java   View Source Code Vote up 4 votes
/**
     * 居中剪切图片
     */
    public static Bitmap centerCrop(Resources res, Bitmap bitmap, int width, int height) {
        if (bitmap == null) {
            return null;
        }

        if (width == 0 || height == 0) {
            return bitmap;
        }

        Bitmap result = Bitmap.createBitmap(width, height, bitmap.getConfig() != null
                ? bitmap.getConfig() : Bitmap.Config.ARGB_8888);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1 && result != null) {
            result.setHasAlpha(bitmap.hasAlpha());
        }

        Matrix matrix = new Matrix();

        float scale;
        float dx = 0, dy = 0;

        final int dwidth = bitmap.getWidth();
        final int dheight = bitmap.getHeight();

        final int vwidth = width;
        final int vheight = height;

        if (dwidth * vheight > vwidth * dheight) {
            scale = (float) vheight / (float) dheight;
            dx = (vwidth - dwidth * scale) * 0.5f;
        } else {
            scale = (float) vwidth / (float) dwidth;
            dy = (vheight - dheight * scale) * 0.5f;
        }

        matrix.setScale(scale, scale);
        matrix.postTranslate(Math.round(dx), Math.round(dy));

//        Canvas canvas = new Canvas(result);
//        canvas.concat(matrix);
//        BitmapDrawable bitmapDrawable = new BitmapDrawable(res, bitmap);
//        bitmapDrawable.setBounds(0, 0, dwidth, dheight);
//        bitmapDrawable.draw(canvas);

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint(Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(bitmap, matrix, paint);

        return result;
    }
 
Example 18
Project: style-transfer   File: Healing.java   View Source Code Vote up 4 votes
/**
 * This function only assumes mPointsXY, mPasteOffX, mPasteOffY
 *
 * @param healing
 * @param rs
 * @param image
 */
public void heal_orig(ScriptC_healing healing, RenderScript rs, Bitmap image, Bitmap output) {
    long time = System.nanoTime();
    Type.Builder floatImage = new Type.Builder(rs, Element.F32_3(rs));
    floatImage.setX(mRoiBounds.width());
    floatImage.setY(mRoiBounds.height());

    Bitmap maskBitmap = buildMask(mRoiBounds, mPointsXY);

    Allocation dest1 = Allocation.createTyped(rs, floatImage.create());
    Allocation dest2 = Allocation.createTyped(rs, floatImage.create());
    healing.set_dest1(dest1);
    healing.set_dest2(dest2);

    Bitmap destBitmap = createMutableBitmap(image, mRoiBounds.left, mRoiBounds.top,
            mRoiBounds.width(), mRoiBounds.height());
    Allocation dest_uc4 = Allocation.createFromBitmap(rs, destBitmap);
    healing.forEach_convert_to_f(dest_uc4, dest1);

    Bitmap src = createMutableBitmap(image, mCutOffsetX, mCutOffsetY,
            mRoiBounds.width(), mRoiBounds.height());
    Allocation src_f3 = Allocation.createTyped(rs, floatImage.create());
    Allocation src_uc4 = Allocation.createFromBitmap(rs, src);
    healing.forEach_convert_to_f(src_uc4, src_f3);
    healing.set_src(src_f3);

    Allocation mask = Allocation.createFromBitmap(rs, maskBitmap);
    healing.set_mask(mask);

    Allocation laplace_f3 = Allocation.createTyped(rs, floatImage.create());
    healing.set_laplace(laplace_f3);

    Script.LaunchOptions options = new Script.LaunchOptions();
    options.setX(1, mRoiBounds.width() - 1);
    options.setY(1, mRoiBounds.height() - 1);
    healing.forEach_laplacian(laplace_f3, options);
    healing.forEach_copyMasked(mask, dest1);

    int steps = (int) Math.hypot(mRoiBounds.width(), mRoiBounds.height()); // match RS Single source
    Log.v(TAG, "Healing_orig  :steps = " + steps);
    for (int i = 0; i < steps; i++) {
        healing.forEach_solve1(mask, dest2);
        healing.forEach_solve2(mask, dest1);
    }

    healing.forEach_convert_to_uc(dest1, dest_uc4);
    rs.finish();

    healing.forEach_alphaMask(dest_uc4, dest_uc4);
    rs.finish();

    dest_uc4.copyTo(destBitmap);
    rs.finish();
    destBitmap.setHasAlpha(true);
    rs.finish();
    // build the undo
    mUndoBitmap = Bitmap.createBitmap(mRoiBounds.width(), mRoiBounds.height(),
            Bitmap.Config.ARGB_8888);
    Canvas undoCanvas = new Canvas(mUndoBitmap);
    Rect undoRect = new Rect(0, 0, mRoiBounds.width(), mRoiBounds.height());
    undoCanvas.drawBitmap(output, mRoiBounds, undoRect, null);

    Canvas c = new Canvas(output);
    c.drawBitmap(image, 0, 0, null);
    c.drawBitmap(destBitmap, mRoiBounds.left, mRoiBounds.top, null);
    Log.v(TAG, " time to smart paste = " + (System.nanoTime() - time) / 1E6f + "ms");
}
 
Example 19
Project: GitHub   File: TransformationUtils.java   View Source Code Vote up 4 votes
/**
 * Crop the image to a circle and resize to the specified width/height.  The circle crop will
 * have the same width and height equal to the min-edge of the result image.
 *
 * @param pool   The BitmapPool obtain a bitmap from.
 * @param inBitmap   The Bitmap to resize.
 * @param destWidth    The width in pixels of the final Bitmap.
 * @param destHeight   The height in pixels of the final Bitmap.
 * @return The resized Bitmap (will be recycled if recycled is not null).
 */
public static Bitmap circleCrop(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap,
    int destWidth, int destHeight) {
  int destMinEdge = Math.min(destWidth, destHeight);
  float radius = destMinEdge / 2f;

  int srcWidth = inBitmap.getWidth();
  int srcHeight = inBitmap.getHeight();

  float scaleX = destMinEdge / (float) srcWidth;
  float scaleY = destMinEdge / (float) srcHeight;
  float maxScale = Math.max(scaleX, scaleY);

  float scaledWidth = maxScale * srcWidth;
  float scaledHeight = maxScale * srcHeight;
  float left = (destMinEdge - scaledWidth) / 2f;
  float top = (destMinEdge - scaledHeight) / 2f;

  RectF destRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

  // Alpha is required for this transformation.
  Bitmap toTransform = getAlphaSafeBitmap(pool, inBitmap);

  Bitmap result = pool.get(destMinEdge, destMinEdge, Bitmap.Config.ARGB_8888);
  result.setHasAlpha(true);

  BITMAP_DRAWABLE_LOCK.lock();
  try {
    Canvas canvas = new Canvas(result);
    // Draw a circle
    canvas.drawCircle(radius, radius, radius, CIRCLE_CROP_SHAPE_PAINT);
    // Draw the bitmap in the circle
    canvas.drawBitmap(toTransform, null, destRect, CIRCLE_CROP_BITMAP_PAINT);
    clear(canvas);
  } finally {
    BITMAP_DRAWABLE_LOCK.unlock();
  }

  if (!toTransform.equals(inBitmap)) {
    pool.put(toTransform);
  }

  return result;
}
 
Example 20
Project: GitHub   File: TransformationUtils.java   View Source Code Vote up 2 votes
/**
 * Sets the alpha of the Bitmap we're going to re-use to the alpha of the Bitmap we're going to
 * transform. This keeps {@link android.graphics.Bitmap#hasAlpha()}} consistent before and after
 * the transformation for transformations that don't add or remove transparent pixels.
 *
 * @param inBitmap The {@link android.graphics.Bitmap} that will be transformed.
 * @param outBitmap   The {@link android.graphics.Bitmap} that will be returned from the
 *                    transformation.
 */
public static void setAlpha(Bitmap inBitmap, Bitmap outBitmap) {
  outBitmap.setHasAlpha(inBitmap.hasAlpha());
}