Example usage for android.graphics Paint ANTI_ALIAS_FLAG

List of usage examples for android.graphics Paint ANTI_ALIAS_FLAG

Introduction

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

Prototype

int ANTI_ALIAS_FLAG

To view the source code for android.graphics Paint ANTI_ALIAS_FLAG.

Click Source Link

Document

Paint flag that enables antialiasing when drawing.

Usage

From source file:com.example.angel.parkpanda.MainActivity.java

public Bitmap drawTextToBitmap(int gResId, String gText) {
    Resources resources = getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);
    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();

    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }// ww  w  . j av  a 2 s . co  m
    bitmap = bitmap.copy(bitmapConfig, true);
    Canvas canvas = new Canvas(bitmap);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.rgb(0, 0, 0));
    paint.setTextSize((int) (15 * scale));
    paint.setShadowLayer(1f, 0f, 1f, Color.BLACK);

    Rect bounds = new Rect();
    paint.getTextBounds(gText, 0, gText.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 2;
    int y = (bitmap.getHeight() + bounds.height()) / 2 - 10;
    canvas.drawText(gText, x, y, paint);

    return bitmap;
}

From source file:com.tct.mail.browse.MessageHeaderView.java

/**
 * Frames the input bitmap in a circle./*from   w  w w.ja  va 2s .com*/
 */
private static Bitmap frameBitmapInCircle(Bitmap input) {
    if (input == null) {
        return null;
    }

    // Crop the image if not squared.
    int inputWidth = input.getWidth();
    int inputHeight = input.getHeight();
    int targetX, targetY, targetSize;
    if (inputWidth >= inputHeight) {
        targetX = inputWidth / 2 - inputHeight / 2;
        targetY = 0;
        targetSize = inputHeight;
    } else {
        targetX = 0;
        targetY = inputHeight / 2 - inputWidth / 2;
        targetSize = inputWidth;
    }

    // Create an output bitmap and a canvas to draw on it.
    Bitmap output = Bitmap.createBitmap(targetSize, targetSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    // Create a black paint to draw the mask.
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.BLACK);

    // Draw a circle.
    canvas.drawCircle(targetSize / 2, targetSize / 2, targetSize / 2, paint);

    // Replace the black parts of the mask with the input image.
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(input, targetX /* left */, targetY /* top */, paint);

    return output;
}

From source file:com.android.app.MediaPlaybackActivity.java

/**
 * /*  w w  w .j a va 2 s .  c o  m*/
 *
 * @param src
 * @return
 */
private Bitmap createCircleBitmap(Bitmap src) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setARGB(255, 241, 239, 229);

    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap target = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(target);

    canvas.drawCircle(width / 2, width / 2, width / 2, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(src, 0, 0, paint);

    return target;
}

From source file:org.telegram.ui.ArticleViewer.java

private TextPaint getTextPaint(TLRPC.RichText parentRichText, TLRPC.RichText richText,
        TLRPC.PageBlock parentBlock) {//from   w w w  .ja  v a 2 s  . com
    int flags = getTextFlags(richText);
    HashMap<Integer, TextPaint> currentMap = null;
    int textSize = AndroidUtilities.dp(14);
    int textColor = 0xffff0000;

    if (parentBlock instanceof TLRPC.TL_pageBlockPhoto) {
        currentMap = captionTextPaints;
        textSize = AndroidUtilities.dp(14);
        textColor = 0xff838c96;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockTitle) {
        currentMap = titleTextPaints;
        textSize = AndroidUtilities.dp(24);
        textColor = 0xff000000;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockAuthorDate) {
        currentMap = authorTextPaints;
        textSize = AndroidUtilities.dp(14);
        textColor = 0xff838c96;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockFooter) {
        currentMap = footerTextPaints;
        textSize = AndroidUtilities.dp(14);
        textColor = 0xff838c96;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockSubtitle) {
        currentMap = subtitleTextPaints;
        textSize = AndroidUtilities.dp(21);
        textColor = 0xff000000;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockHeader) {
        currentMap = headerTextPaints;
        textSize = AndroidUtilities.dp(21);
        textColor = 0xff000000;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockSubheader) {
        currentMap = subheaderTextPaints;
        textSize = AndroidUtilities.dp(18);
        textColor = 0xff000000;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockBlockquote
            || parentBlock instanceof TLRPC.TL_pageBlockPullquote) {
        if (parentBlock.text == parentRichText) {
            currentMap = quoteTextPaints;
            textSize = AndroidUtilities.dp(15);
            textColor = 0xff000000;
        } else if (parentBlock.caption == parentRichText) {
            currentMap = subquoteTextPaints;
            textSize = AndroidUtilities.dp(14);
            textColor = 0xff838c96;
        }
    } else if (parentBlock instanceof TLRPC.TL_pageBlockPreformatted) {
        currentMap = preformattedTextPaints;
        textSize = AndroidUtilities.dp(14);
        textColor = 0xff000000;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockParagraph) {
        if (parentBlock.caption == parentRichText) {
            currentMap = embedPostCaptionTextPaints;
            textSize = AndroidUtilities.dp(14);
            textColor = 0xff838c96;
        } else {
            currentMap = paragraphTextPaints;
            textSize = AndroidUtilities.dp(16);
            textColor = 0xff000000;
        }
    } else if (parentBlock instanceof TLRPC.TL_pageBlockList) {
        currentMap = listTextPaints;
        textSize = AndroidUtilities.dp(15);
        textColor = 0xff000000;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockEmbed) {
        currentMap = embedTextPaints;
        textSize = AndroidUtilities.dp(14);
        textColor = 0xff838c96;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockSlideshow) {
        currentMap = slideshowTextPaints;
        textSize = AndroidUtilities.dp(14);
        textColor = 0xff838c96;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockEmbedPost) {
        if (richText != null) {
            currentMap = embedPostTextPaints;
            textSize = AndroidUtilities.dp(14);
            textColor = 0xff000000;
        }
    } else if (parentBlock instanceof TLRPC.TL_pageBlockVideo) {
        currentMap = videoTextPaints;
        textSize = AndroidUtilities.dp(14);
        textColor = 0xff000000;
    }
    if (currentMap == null) {
        if (errorTextPaint == null) {
            errorTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
            errorTextPaint.setColor(0xffff0000);
        }
        errorTextPaint.setTextSize(AndroidUtilities.dp(14));
        return errorTextPaint;
    }
    TextPaint paint = currentMap.get(flags);
    if (paint == null) {
        paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        if ((flags & TEXT_FLAG_MONO) != 0) {
            paint.setTypeface(AndroidUtilities.getTypeface("fonts/rmono.ttf"));
        } else {
            if (parentBlock instanceof TLRPC.TL_pageBlockTitle
                    || parentBlock instanceof TLRPC.TL_pageBlockHeader
                    || parentBlock instanceof TLRPC.TL_pageBlockSubtitle
                    || parentBlock instanceof TLRPC.TL_pageBlockSubheader) {
                if ((flags & TEXT_FLAG_MEDIUM) != 0 && (flags & TEXT_FLAG_ITALIC) != 0) {
                    paint.setTypeface(Typeface.create("serif", Typeface.BOLD_ITALIC));
                } else if ((flags & TEXT_FLAG_MEDIUM) != 0) {
                    paint.setTypeface(Typeface.create("serif", Typeface.BOLD));
                } else if ((flags & TEXT_FLAG_ITALIC) != 0) {
                    paint.setTypeface(Typeface.create("serif", Typeface.ITALIC));
                } else {
                    paint.setTypeface(Typeface.create("serif", Typeface.NORMAL));
                }
            } else {
                if ((flags & TEXT_FLAG_MEDIUM) != 0 && (flags & TEXT_FLAG_ITALIC) != 0) {
                    paint.setTypeface(AndroidUtilities.getTypeface("fonts/rmediumitalic.ttf"));
                } else if ((flags & TEXT_FLAG_MEDIUM) != 0) {
                    paint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
                } else if ((flags & TEXT_FLAG_ITALIC) != 0) {
                    paint.setTypeface(AndroidUtilities.getTypeface("fonts/ritalic.ttf"));
                }
            }
        }
        if ((flags & TEXT_FLAG_STRIKE) != 0) {
            paint.setFlags(paint.getFlags() | TextPaint.STRIKE_THRU_TEXT_FLAG);
        }
        if ((flags & TEXT_FLAG_UNDERLINE) != 0) {
            paint.setFlags(paint.getFlags() | TextPaint.UNDERLINE_TEXT_FLAG);
        }
        if ((flags & TEXT_FLAG_URL) != 0) {
            textColor = 0xff4d83b3;
        }
        paint.setColor(textColor);
        currentMap.put(flags, paint);
    }
    paint.setTextSize(textSize);
    return paint;
}

From source file:org.telegram.ui.ArticleViewer.java

private StaticLayout createLayoutForText(CharSequence plainText, TLRPC.RichText richText, int width,
        TLRPC.PageBlock parentBlock) {/*from w  ww . ja  v  a2  s .c o  m*/
    if (plainText == null && (richText == null || richText instanceof TLRPC.TL_textEmpty)) {
        return null;
    }

    if (quoteLinePaint == null) {
        quoteLinePaint = new Paint();
        quoteLinePaint.setColor(0xff000000);

        preformattedBackgroundPaint = new Paint();
        preformattedBackgroundPaint.setColor(0xfff5f8fc);

        urlPaint = new Paint();
        urlPaint.setColor(0x3362a9e3);
    }

    CharSequence text;
    if (plainText != null) {
        text = plainText;
    } else {
        text = getText(richText, richText, parentBlock);
    }
    if (TextUtils.isEmpty(text)) {
        return null;
    }
    TextPaint paint;
    if (parentBlock instanceof TLRPC.TL_pageBlockEmbedPost && richText == null) {
        if (parentBlock.author == plainText) {
            if (embedPostAuthorPaint == null) {
                embedPostAuthorPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
                embedPostAuthorPaint.setColor(0xff000000);
            }
            embedPostAuthorPaint.setTextSize(AndroidUtilities.dp(15));
            paint = embedPostAuthorPaint;
        } else {
            if (embedPostDatePaint == null) {
                embedPostDatePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
                embedPostDatePaint.setColor(0xff8f97a0);
            }
            embedPostDatePaint.setTextSize(AndroidUtilities.dp(14));
            paint = embedPostDatePaint;
        }
    } else {
        paint = getTextPaint(richText, richText, parentBlock);
    }
    if (parentBlock instanceof TLRPC.TL_pageBlockPullquote) {
        return new StaticLayout(text, paint, width, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
    } else {
        return new StaticLayout(text, paint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, AndroidUtilities.dp(4),
                false);
    }
}

From source file:org.telegram.ui.ChannelAdminLogActivity.java

private TextureView createTextureView(boolean add) {
    if (parentLayout == null) {
        return null;
    }/*w  ww. j a v a 2  s  .  c  o  m*/
    if (roundVideoContainer == null) {
        if (Build.VERSION.SDK_INT >= 21) {
            roundVideoContainer = new FrameLayout(getParentActivity()) {
                @Override
                public void setTranslationY(float translationY) {
                    super.setTranslationY(translationY);
                    contentView.invalidate();
                }
            };
            roundVideoContainer.setOutlineProvider(new ViewOutlineProvider() {
                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void getOutline(View view, Outline outline) {
                    outline.setOval(0, 0, AndroidUtilities.roundMessageSize, AndroidUtilities.roundMessageSize);
                }
            });
            roundVideoContainer.setClipToOutline(true);
        } else {
            roundVideoContainer = new FrameLayout(getParentActivity()) {
                @Override
                protected void onSizeChanged(int w, int h, int oldw, int oldh) {
                    super.onSizeChanged(w, h, oldw, oldh);
                    aspectPath.reset();
                    aspectPath.addCircle(w / 2, h / 2, w / 2, Path.Direction.CW);
                    aspectPath.toggleInverseFillType();
                }

                @Override
                public void setTranslationY(float translationY) {
                    super.setTranslationY(translationY);
                    contentView.invalidate();
                }

                @Override
                public void setVisibility(int visibility) {
                    super.setVisibility(visibility);
                    if (visibility == VISIBLE) {
                        setLayerType(View.LAYER_TYPE_HARDWARE, null);
                    }
                }

                @Override
                protected void dispatchDraw(Canvas canvas) {
                    super.dispatchDraw(canvas);
                    canvas.drawPath(aspectPath, aspectPaint);
                }
            };
            aspectPath = new Path();
            aspectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            aspectPaint.setColor(0xff000000);
            aspectPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        }
        roundVideoContainer.setWillNotDraw(false);
        roundVideoContainer.setVisibility(View.INVISIBLE);

        aspectRatioFrameLayout = new AspectRatioFrameLayout(getParentActivity());
        aspectRatioFrameLayout.setBackgroundColor(0);
        if (add) {
            roundVideoContainer.addView(aspectRatioFrameLayout,
                    LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
        }

        videoTextureView = new TextureView(getParentActivity());
        videoTextureView.setOpaque(false);
        aspectRatioFrameLayout.addView(videoTextureView,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
    }
    if (roundVideoContainer.getParent() == null) {
        contentView.addView(roundVideoContainer, 1, new FrameLayout.LayoutParams(
                AndroidUtilities.roundMessageSize, AndroidUtilities.roundMessageSize));
    }
    roundVideoContainer.setVisibility(View.INVISIBLE);
    aspectRatioFrameLayout.setDrawingReady(false);
    return videoTextureView;
}