Example usage for android.graphics Paint setDither

List of usage examples for android.graphics Paint setDither

Introduction

In this page you can find the example usage for android.graphics Paint setDither.

Prototype

public void setDither(boolean dither) 

Source Link

Document

Helper for setFlags(), setting or clearing the DITHER_FLAG bit Dithering affects how colors that are higher precision than the device are down-sampled.

Usage

From source file:Main.java

public static Bitmap int2Icon(Context context, int i) {
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), 0x7f020047);
    Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
            android.graphics.Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap1);
    Paint paint = new Paint();
    paint.setDither(true);
    paint.setFilterBitmap(true);/*from   ww w  . ja  v  a  2s .  c  o  m*/
    canvas.drawBitmap(bitmap, 0.0F, 0.0F, paint);
    bitmap.recycle();
    Paint paint1 = new Paint(257);
    paint1.setColor(-1);
    paint1.setTypeface(Typeface.DEFAULT_BOLD);
    paint1.setTextAlign(android.graphics.Paint.Align.CENTER);
    canvas.drawText(String.valueOf(i), bitmap.getWidth() / 2, 3 + bitmap.getHeight() / 2, paint1);
    return bitmap1;
}

From source file:Main.java

public static void erase(Canvas canvas, RectF rectf) {
    Paint paint = new Paint();
    paint.setColor(0);/*from w  ww .j  av a2  s . c  om*/
    paint.setDither(true);
    paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR));
    canvas.drawRect(rectf, paint);
}

From source file:Main.java

static Bitmap generatorContactCountIcon(Context context, Bitmap icon) {
    int iconSize = (int) context.getResources().getDimension(android.R.dimen.app_icon_size);
    Bitmap contactIcon = Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888);
    Canvas canvas = new Canvas(contactIcon);

    Paint iconPaint = new Paint();
    iconPaint.setDither(true);
    iconPaint.setFilterBitmap(true);//from  www  . j a va 2 s. c  om
    Rect src = new Rect(0, 0, icon.getWidth(), icon.getHeight());
    Rect dst = new Rect(0, 0, iconSize, iconSize);
    canvas.drawBitmap(icon, src, dst, iconPaint);
    int contacyCount = 11;
    Paint countPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
    countPaint.setColor(Color.RED);
    countPaint.setTextSize(20f);
    countPaint.setTypeface(Typeface.DEFAULT_BOLD);
    canvas.drawText(String.valueOf(contacyCount), iconSize - 18, 25, countPaint);
    return contactIcon;
}

From source file:Main.java

public static void drawCircleBorder(Canvas canvas, int radius, int w, int y) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*ww  w.ja va  2  s.co m*/
    paint.setFilterBitmap(true);
    paint.setDither(true);
    paint.setColor(Color.WHITE);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(mBorderThickness);
    canvas.drawCircle(w / 2, y / 2, radius, paint);
}

From source file:Main.java

public static Bitmap getBitmap(Bitmap background, Bitmap contour, float alpha) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from www.  j  a v  a2  s . c o  m*/
    paint.setColor(Color.WHITE);
    paint.setDither(false);
    Bitmap bitmap = Bitmap.createBitmap(contour.getWidth(), contour.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Matrix m = new Matrix();
    m.setScale(contour.getWidth() * 1.0f / background.getWidth(),
            contour.getHeight() * 1.0f / background.getHeight());
    paint.setAlpha((int) (alpha * 0xff));
    canvas.drawBitmap(background, m, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    paint.setAlpha(0xff);
    canvas.drawBitmap(contour, 0, 0, paint);
    return bitmap;
}

From source file:Main.java

/**
 * Create a Bitmap that will be used as a mask to create the preview window in the background.
 * The Bitmap will be the given width and height. The Bitmap will be transparent except for a
 * gradient on the left and bottom sides.
 *
 * @param width Width of the mask./*from   www. j  a  v a2s  .c  o  m*/
 * @param height Height of the mask.
 * @param gradientSize Size of the gradient.
 * @return Bitmap mask that will be used to create a preview window.
 */
private static Bitmap createPreviewWindowMask(int width, int height, int gradientSize) {

    // Do nothing if the size is invalid
    if (width <= 0 || height <= 0) {
        return null;
    }

    // Initialize the mask
    Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(mask);
    canvas.drawColor(Color.TRANSPARENT);

    // If the gradient size is zero, don't draw the gradients
    if (gradientSize <= 0) {
        return mask;
    }

    // Calculate gradient rects
    Rect leftGradientRect = new Rect(0, 0, gradientSize, height - gradientSize);
    Rect bottomGradientRect = new Rect(leftGradientRect.right, height - gradientSize, width, height);
    Rect cornerGradientRect = new Rect(leftGradientRect.left, leftGradientRect.bottom, bottomGradientRect.left,
            bottomGradientRect.bottom);

    // Create left gradient
    Paint leftGradientPaint = new Paint();
    leftGradientPaint.setDither(true);
    leftGradientPaint.setShader(new LinearGradient(leftGradientRect.left, 0, leftGradientRect.right, 0,
            Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP));

    // Create right gradient
    Paint bottomGradientPaint = new Paint();
    bottomGradientPaint.setDither(true);
    bottomGradientPaint.setShader(
            new LinearGradient(leftGradientRect.right, bottomGradientRect.bottom, leftGradientRect.right,
                    bottomGradientRect.top, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP));

    // Create corner gradient
    Paint cornerGradientPaint = new Paint();
    cornerGradientPaint.setDither(true);
    cornerGradientPaint.setShader(new RadialGradient(cornerGradientRect.right, cornerGradientRect.top,
            gradientSize, Color.TRANSPARENT, Color.BLACK, Shader.TileMode.CLAMP));

    // Draw the gradients
    canvas.drawRect(leftGradientRect, leftGradientPaint);
    canvas.drawRect(bottomGradientRect, bottomGradientPaint);
    canvas.drawRect(cornerGradientRect, cornerGradientPaint);

    return mask;
}

From source file:Main.java

public static Bitmap createCircleBitmap(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }// ww w.j a  v a 2 s.  co  m

    int bmWidth = bitmap.getWidth();
    int bmHeight = bitmap.getHeight();
    int side = bmWidth < bmHeight ? bmWidth : bmHeight;
    int x = (bmWidth - side) / 2;
    int y = (bmHeight - side) / 2;
    Bitmap newBm = Bitmap.createBitmap(side, side, Bitmap.Config.ARGB_8888);
    if (newBm != null) {
        Canvas canvas = new Canvas(newBm);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);

        Rect rect = new Rect(0, 0, newBm.getWidth(), newBm.getHeight());
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawCircle(newBm.getWidth() / 2, newBm.getHeight() / 2, newBm.getHeight() / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return newBm;
    }
    return null;
}

From source file:Main.java

public static Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();

    // Compute the scaling factors to fit the new height and width, respectively.
    // To cover the final image, the final scaling will be the bigger
    // of these two.
    float xScale = (float) newWidth / sourceWidth;
    float yScale = (float) newHeight / sourceHeight;
    float scale = Math.max(xScale, yScale);

    // Now get the size of the source bitmap when scaled
    float scaledWidth = scale * sourceWidth;
    float scaledHeight = scale * sourceHeight;

    // Let's find out the upper left coordinates if the scaled bitmap
    // should be centered in the new size give by the parameters
    float left = (newWidth - scaledWidth) / 2;
    float top = (newHeight - scaledHeight) / 2;

    // The target rectangle for the new, scaled version of the source bitmap will now
    // be//from   w  w w  . j  a v a2 s  .c om
    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

    // Finally, we create a new bitmap of the specified size and draw our new,
    // scaled bitmap onto it.
    Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, CONFIG);
    Canvas canvas = new Canvas(dest);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawBitmap(source, null, targetRect, paint);

    return dest;
}

From source file:Main.java

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius, int border, int color) {
    Bitmap scaledBitmap;/* w  ww .  ja  va 2s  .co  m*/
    if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
        scaledBitmap = ThumbnailUtils.extractThumbnail(bmp, radius - 2, radius - 2);
    } else {
        scaledBitmap = bmp;
    }

    Bitmap output = Bitmap.createBitmap(scaledBitmap.getWidth(), scaledBitmap.getHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(scaledBitmap.getWidth() / 2, scaledBitmap.getHeight() / 2, scaledBitmap.getWidth() / 2,
            paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    final Rect rect = new Rect(0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight());
    canvas.drawBitmap(scaledBitmap, rect, rect, paint);

    if (border > 0) {
        paint.setStrokeWidth(border);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(scaledBitmap.getWidth() / 2, scaledBitmap.getHeight() / 2,
                scaledBitmap.getWidth() / 2, paint);
    }

    return output;
}

From source file:Main.java

private static synchronized Bitmap createScaledBitmap(Bitmap bitmap, final int width, final int height,
        float cornerRadius) {

    if (bitmap == null) {
        return null;
    }/*from  w  w  w . j  a  v  a  2s. c  o  m*/

    int adjustedWidth = width;
    int adjustedHeight = height;

    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    //int inBytes = bitmap.getByteCount();
    if (width >= bitmapWidth && height >= bitmapHeight)
        return bitmap;

    if (width > 0 && height > 0) {
        //if (width < bitmapWidth || height < bitmapHeight) {
        final float ratio = (float) bitmapWidth / bitmapHeight;

        if (bitmapWidth > bitmapHeight) {
            adjustedHeight = (int) (width / ratio);
        } else if (bitmapHeight > bitmapWidth) {
            adjustedWidth = (int) (height * ratio);
        }

        final Bitmap.Config c = Bitmap.Config.ARGB_8888;
        final Bitmap thumb = Bitmap.createBitmap(width, height, c);
        final Canvas canvas = sScaleCanvas;
        final Paint paint = sPaint;
        canvas.setBitmap(thumb);
        paint.setDither(false);
        paint.setFilterBitmap(true);

        Rect sBounds = new Rect();
        Rect sOldBounds = new Rect();

        sBounds.set((width - adjustedWidth) >> 1, (height - adjustedHeight) >> 1, adjustedWidth,
                adjustedHeight);
        sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);

        if (cornerRadius != 0) {
            //Path p = new Path();
            RectF rect = new RectF(sBounds);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(Color.WHITE);
            canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            //p.addRoundRect(rect, cornerRadius, cornerRadius, Direction.CCW);
            //canvas.clipPath(p, Op.REPLACE);
        } else {
            paint.setXfermode(null);
            //canvas.clipRect(0, 0, thumb.getWidth(), thumb.getHeight());
        }

        canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);

        canvas.setBitmap(Bitmap.createBitmap(1, 1, Config.ALPHA_8));

        return thumb;

    }
    return bitmap;
}