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

The following are Jave code examples for showing how to use prepareToDraw() 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: BitmapPrepareProducer.java   View Source Code Vote up 5 votes
private void internalPrepareBitmap(CloseableReference<CloseableImage> newResult) {
  if (newResult == null || !newResult.isValid()) {
    return;
  }

  final CloseableImage closeableImage = newResult.get();
  if (closeableImage == null || closeableImage.isClosed()) {
    return;
  }

  if (closeableImage instanceof CloseableStaticBitmap) {
    final CloseableStaticBitmap staticBitmap = (CloseableStaticBitmap) closeableImage;
    final Bitmap bitmap = staticBitmap.getUnderlyingBitmap();
    if (bitmap == null) {
      return;
    }

    final int bitmapByteCount = bitmap.getRowBytes() * bitmap.getHeight();
    if (bitmapByteCount < mMinBitmapSizeBytes) {
      return;
    }
    if (bitmapByteCount > mMaxBitmapSizeBytes) {
      return;
    }

    bitmap.prepareToDraw();
  }
}
 
Example 2
Project: silly-android   File: Coloring.java   View Source Code Vote up 5 votes
/**
 * Colors the given bitmap to the specified color. Uses {@link PorterDuff.Mode#SRC_ATOP}.
 *
 * @param bitmap The original bitmap, must not be {@code null}
 * @param color  Which color to use for coloring
 * @return A new, colored Bitmap, never {@code null}
 */
@NonNull
public static Bitmap colorBitmap(@NonNull final Bitmap bitmap, @ColorInt final int color) {
    // use the original bitmap config
    final Bitmap result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());

    // paint over the new canvas
    final Paint paint = new Paint();
    final Canvas c = new Canvas(result);
    paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
    c.drawBitmap(bitmap, 0, 0, paint);

    result.prepareToDraw();
    return result;
}
 
Example 3
Project: silly-android   File: ColoringTest.java   View Source Code Vote up 5 votes
/**
 * Tests the {@link Coloring#colorBitmap(Bitmap, int)} method.
 * <p>
 * Due to {@link org.robolectric.shadows.ShadowBitmap}'s empty implementation, this won't really work, so we can only test the transparency.
 */
@Test
public final void testColorBitmap() {
    final Bitmap.Config config = Bitmap.Config.ARGB_8888;
    final int width = 10, height = 10;
    final int[] allReds = new int[width * height];
    for (int i = 0; i < width * height; i++) {
        allReds[i] = Color.RED;
    }

    final Bitmap redSquare = Bitmap.createBitmap(allReds, width, height, config);
    assertNotNull("Created Bitmap is null", redSquare);

    // initialize red Bitmap's internal structures, otherwise it won't draw properly
    redSquare.prepareToDraw();
    final byte[] redPixels = new byte[redSquare.getWidth() * redSquare.getHeight() * 8];
    final ByteBuffer redBuffer = ByteBuffer.wrap(redPixels);
    redBuffer.order(ByteOrder.nativeOrder());
    redSquare.copyPixelsToBuffer(redBuffer);
    redSquare.copyPixelsFromBuffer(redBuffer);
    redSquare.prepareToDraw();

    final String redPixel = hex(redSquare.getPixel(width / 2, height / 2));
    final String errorRed = String.format("Error while creating red bitmap, middle pixel is %s", redPixel);
    assertEquals(errorRed, hex(Color.TRANSPARENT), redPixel);

    final Bitmap greenSquare = Coloring.colorBitmap(redSquare, Color.GREEN);
    assertNotNull("Created colored Bitmap is null", greenSquare);
    final String greenPixel = hex(greenSquare.getPixel(width / 2, height / 2));
    final String errorGreen = String.format("Error while coloring bitmap, middle pixel is %s", greenPixel);
    assertEquals(errorGreen, hex(Color.TRANSPARENT), greenPixel);
}
 
Example 4
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);
        }
    }

}