Example usage for android.text TextPaint setAntiAlias

List of usage examples for android.text TextPaint setAntiAlias

Introduction

In this page you can find the example usage for android.text TextPaint setAntiAlias.

Prototype

public void setAntiAlias(boolean aa) 

Source Link

Document

Helper for setFlags(), setting or clearing the ANTI_ALIAS_FLAG bit AntiAliasing smooths out the edges of what is being drawn, but is has no impact on the interior of the shape.

Usage

From source file:com.ecuamobi.deckwallet.util.Renderer.java

public static void printQR(final Activity context, final String addressUri) {
    new AsyncTask<Void, Void, Bitmap>() {

        @Override//from w  ww.  j  av  a 2s .  c o m
        protected Bitmap doInBackground(Void... params) {
            TextPaint textPaint = new TextPaint();
            textPaint.setAntiAlias(true);
            textPaint.setColor(0xFF000000);
            final int bitmapMargin = 100;//big margin is to prevent possible clipping
            final int textHeight = 28;
            textPaint.setTextSize(textHeight);
            textPaint.setTextAlign(Paint.Align.CENTER);
            final int qrCodePadding = (int) (textPaint.descent() * 2);
            int textWidth = getTextWidth(addressUri, textPaint);
            QRCode addressQrCode = QRCode.getMinimumQRCode(addressUri, ErrorCorrectLevel.M);
            Bitmap addressQrCodeBitmap = addressQrCode.createImage(textWidth);
            Bitmap bmp = Bitmap.createBitmap(textWidth + bitmapMargin * 2,
                    addressQrCodeBitmap.getHeight() + qrCodePadding * 2 + bitmapMargin * 2,
                    Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(bmp);
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setARGB(0xFF, 0xFF, 0xFF, 0xFF);
            paint.setAntiAlias(false);
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);

            int centerXForAddress = bitmapMargin + textWidth / 2;
            int y = bitmapMargin + qrCodePadding;
            Paint qrCodePaint = new Paint();
            qrCodePaint.setAntiAlias(false);
            qrCodePaint.setDither(false);
            canvas.drawBitmap(addressQrCodeBitmap, centerXForAddress - addressQrCodeBitmap.getWidth() / 2, y,
                    qrCodePaint);
            y += qrCodePadding - textPaint.ascent();
            canvas.drawText(addressUri, centerXForAddress, y + addressQrCodeBitmap.getHeight(), textPaint);
            return bmp;
        }

        @Override
        protected void onPostExecute(final Bitmap bitmap) {
            if (bitmap != null) {
                //DEBUG
                //                    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
                //                    android.widget.ImageView view = new android.widget.ImageView(context);
                //                    view.setImageBitmap(bitmap);
                //                    builder.setView(view);
                //                    builder.setPositiveButton(android.R.string.ok, null);
                //                    builder.show();

                PrintHelper printHelper = new PrintHelper(context);
                printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
                printHelper.printBitmap(addressUri, bitmap);
            }

        }
    }.execute();

}

From source file:com.ecuamobi.deckwallet.util.Renderer.java

static void printWallet(final Activity context, final String label, final String addressUri,
        final String privateKey) {
    new AsyncTask<Void, Void, Bitmap>() {

        @Override//from www  .  j a v a  2s  .com
        protected Bitmap doInBackground(Void... params) {

            TextPaint textPaint = new TextPaint();
            textPaint.setAntiAlias(true);
            textPaint.setColor(0xFF000000);
            final int bitmapMargin = 100;//big margin is to prevent possible clipping
            final int textHeight = 28;
            final int spaceBetweenQrCodes = 60;
            textPaint.setTextSize(textHeight);
            textPaint.setTextAlign(Paint.Align.CENTER);
            final int qrCodePadding = (int) (textPaint.descent() * 2);
            Rect bounds = new Rect();
            textPaint.getTextBounds(privateKey, 0, privateKey.length(), bounds);
            int textWidth = getTextWidth(privateKey, textPaint);
            ArrayList<String> labelLinesRelaxed = wrap(label, textWidth, false, textPaint);
            for (String titleLine : labelLinesRelaxed) {
                textWidth = Math.max(textWidth, getTextWidth(titleLine, textPaint));
            }
            textWidth = Math.max(textWidth, getTextWidth(addressUri, textPaint));
            QRCode privateKeyQrCode = QRCode.getMinimumQRCode(privateKey, ErrorCorrectLevel.M);
            Bitmap privateKeyQrCodeBitmap = privateKeyQrCode.createImage(textWidth);
            QRCode addressQrCode = QRCode.getMinimumQRCode(addressUri, ErrorCorrectLevel.M);
            Bitmap addressQrCodeBitmap = addressQrCode.createImage(textWidth);
            ArrayList<String> labelLines = wrap(label, textWidth, true, textPaint);
            Bitmap bmp = Bitmap.createBitmap(
                    textWidth * 2 + bitmapMargin * 2 + spaceBetweenQrCodes, privateKeyQrCodeBitmap.getHeight()
                            + textHeight * (labelLines.size() + 1) + qrCodePadding * 2 + bitmapMargin * 2,
                    Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(bmp);
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setARGB(0xFF, 0xFF, 0xFF, 0xFF);
            paint.setAntiAlias(false);
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);

            int centerXForAddress = bitmapMargin + textWidth / 2;
            int centerXForPrivateKey = bitmapMargin + textWidth + spaceBetweenQrCodes + textWidth / 2;
            int y = (int) (bitmapMargin - textPaint.ascent());
            for (int i = 0; i < labelLines.size(); i++) {
                canvas.drawText(labelLines.get(i), centerXForPrivateKey, y + i * textHeight, textPaint);
            }
            y = bitmapMargin + labelLines.size() * textHeight + qrCodePadding;
            Paint qrCodePaint = new Paint();
            qrCodePaint.setAntiAlias(false);
            qrCodePaint.setDither(false);
            canvas.drawBitmap(addressQrCodeBitmap, centerXForAddress - addressQrCodeBitmap.getWidth() / 2, y,
                    qrCodePaint);
            canvas.drawBitmap(privateKeyQrCodeBitmap,
                    centerXForPrivateKey - privateKeyQrCodeBitmap.getWidth() / 2, y, qrCodePaint);
            y += qrCodePadding - textPaint.ascent();
            canvas.drawText(addressUri, centerXForAddress, y + addressQrCodeBitmap.getHeight(), textPaint);
            canvas.drawText(privateKey, centerXForPrivateKey, y + privateKeyQrCodeBitmap.getHeight(),
                    textPaint);
            return bmp;
        }

        @Override
        protected void onPostExecute(final Bitmap bitmap) {
            if (bitmap != null) {
                //DEBUG
                //                    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
                //                    android.widget.ImageView view = new android.widget.ImageView(context);
                //                    view.setImageBitmap(bitmap);
                //                    builder.setView(view);
                //                    builder.setPositiveButton(android.R.string.ok, null);
                //                    builder.show();

                PrintHelper printHelper = new PrintHelper(context);
                printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
                printHelper.printBitmap(label, bitmap);
            }

        }
    }.execute();
}

From source file:org.wikipedia.page.shareafact.SnippetImage.java

@NonNull
private static Layout drawTextSnippet(@NonNull Canvas canvas, @NonNull CharSequence textSnippet) {
    final int top = TOP_PADDING;
    final int maxHeight = 225;
    final int maxLines = 5;
    final float maxFontSize = 195.0f;
    final float minFontSize = 32.0f;

    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(Color.WHITE);
    textPaint.setTextSize(maxFontSize);//from  w ww .  ja  va2 s  . com
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setTypeface(Typeface.DEFAULT_BOLD);
    textPaint.setShadowLayer(1.0f, 1.0f, 1.0f, Color.GRAY);

    StaticLayout textLayout = optimizeTextSize(
            new TextLayoutParams(textSnippet, textPaint, TEXT_WIDTH, SPACING_MULTIPLIER), maxHeight, maxLines,
            maxFontSize, minFontSize);

    canvas.save();
    int horizontalCenterOffset = top + (maxHeight - textLayout.getHeight()) / QUARTER;
    canvas.translate(HORIZONTAL_PADDING, horizontalCenterOffset);
    textLayout.draw(canvas);
    canvas.restore();

    return textLayout;
}

From source file:org.wikipedia.page.shareafact.SnippetImage.java

private static void drawWordmarkFromText(@NonNull Context context, @NonNull Canvas canvas,
        boolean isArticleRTL) {
    final int maxWidth = WIDTH - DESCRIPTION_WIDTH - 2 * HORIZONTAL_PADDING;
    final float fontSize = 20.0f;
    final float scaleX = 1.06f;

    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(Color.LTGRAY);
    textPaint.setTextSize(fontSize);//  ww w.  ja va2  s .  com
    textPaint.setTypeface(SERIF);
    textPaint.setTextScaleX(scaleX);

    Spanned wikipedia = StringUtil.fromHtml(context.getString(R.string.wp_stylized));
    Layout.Alignment align = L10nUtil.isDeviceRTL() ? ALIGN_OPPOSITE : ALIGN_NORMAL;
    StaticLayout wordmarkLayout = buildLayout(
            new TextLayoutParams(wikipedia, textPaint, maxWidth, 1.0f, align));
    final int width = (int) wordmarkLayout.getLineWidth(0);
    final int height = wordmarkLayout.getHeight();

    final int bottom = HEIGHT - BOTTOM_PADDING;
    final int top = bottom - height;

    int left = WIDTH - HORIZONTAL_PADDING - width;
    if (isArticleRTL) {
        left = HORIZONTAL_PADDING;
    }

    canvas.save(); // --
    canvas.translate(left, top);
    wordmarkLayout.draw(canvas);
    canvas.restore(); // --
}

From source file:org.wikipedia.page.shareafact.SnippetImage.java

private static int drawDescription(@NonNull Canvas canvas, @Nullable String description, int top,
        boolean isArticleRTL) {
    final int marginBottom = 5;
    final int maxHeight = 23;
    final int maxLines = 2;
    final float maxFontSize = 15.0f;
    final float minFontSize = 10.0f;

    if (TextUtils.isEmpty(description)) {
        return top - marginBottom;
    }//from  w w  w .jav  a2  s.c o  m
    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(Color.WHITE);
    textPaint.setTextSize(maxFontSize);
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setShadowLayer(1.0f, 0.0f, 0.0f, Color.GRAY);

    StaticLayout textLayout = optimizeTextSize(
            new TextLayoutParams(description, textPaint, DESCRIPTION_WIDTH, SPACING_MULTIPLIER), maxHeight,
            maxLines, maxFontSize, minFontSize);
    int left = HORIZONTAL_PADDING;
    if (isArticleRTL) {
        left = WIDTH - HORIZONTAL_PADDING - textLayout.getWidth();
    }

    top = top - marginBottom - textLayout.getHeight();
    canvas.save();
    canvas.translate(left, top);
    textLayout.draw(canvas);
    canvas.restore();

    return top;
}

From source file:org.wikipedia.page.shareafact.SnippetImage.java

private static void drawTitle(@NonNull Canvas canvas, @NonNull String title, int top, boolean isArticleRTL) {
    final int marginBottom = 0;
    final int maxHeight = 70;
    final int maxLines = 2;
    final float maxFontSize = 30.0f;
    final float spacingMultiplier = 0.7f;

    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(Color.WHITE);
    textPaint.setTextSize(maxFontSize);/*www  .  j  a v  a  2s .c om*/
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setTypeface(SERIF);
    textPaint.setShadowLayer(1.0f, 0.0f, 1.0f, Color.GRAY);

    StaticLayout textLayout = optimizeTextSize(
            new TextLayoutParams(title, textPaint, DESCRIPTION_WIDTH, spacingMultiplier), maxHeight, maxLines,
            maxFontSize, maxFontSize);
    int left = HORIZONTAL_PADDING;
    if (isArticleRTL) {
        left = WIDTH - HORIZONTAL_PADDING - textLayout.getWidth();
    }
    int marginBottomTotal = marginBottom;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        // versions < 5.0 don't compensate for bottom margin correctly when line
        // spacing is less than 1.0, so we'll compensate ourselves
        final int marginBoost = 10;
        marginBottomTotal += marginBoost;
    }

    top = top - marginBottomTotal - textLayout.getHeight();
    canvas.save();
    canvas.translate(left, top);
    textLayout.draw(canvas);
    canvas.restore();
}

From source file:com.mobisci_lab.virtualkeyboard.softkeyboard.LatinKeyboardView.java

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    TextPaint paint = new TextPaint();
    paint.setAntiAlias(true);
    paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    paint.setTextSize(11 * getResources().getDisplayMetrics().density);
    paint.setColor(ContextCompat.getColor(this.getContext(), R.color.candidate_other));

    List<Key> keys = getKeyboard().getKeys();
    for (Key key : keys) {
        float x = key.x + key.width / 2 + key.width / 5;
        float y = key.y + key.width / 2;
        String text = "";
        switch (key.codes[0]) {
        case 113:
            text = "1";
            break;
        case 119:
            text = "2";
            break;
        case 101:
            text = "3";
            break;
        case 114:
            text = "4";
            break;
        case 116:
            text = "5";
            break;
        case 121:
            text = "6";
            break;
        case 117:
            text = "7";
            break;
        case 105:
            text = "8";
            break;
        case 111:
            text = "9";
            break;
        case 112:
            text = "0";
            break;

        }/*from  www.  j  av a2s  .co m*/
        canvas.drawText(text, x, y, paint);
    }
}

From source file:com.busdrone.android.ui.VehicleMarkerRenderer.java

private Bitmap render(int color, String text) {
    TextPaint textPaint = new TextPaint();
    textPaint.setColor(Color.WHITE);
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setAntiAlias(true);
    textPaint.setTextSize(mTextSize);/*w  w w . j a  v  a  2 s . co m*/

    Rect textBounds = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), textBounds);

    int width = mPadding + textBounds.width() + mPadding;
    int height = mPadding + textBounds.height() + mPadding;

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(color);
    canvas.drawRoundRect(new RectF(0, 0, width, height), mCornerRadius, mCornerRadius, paint);

    canvas.drawText(text, (width / 2f) - (textBounds.width() / 2f), (height / 2f) + (textBounds.height() / 2f),
            textPaint);

    return bitmap;
}

From source file:com.grarak.kerneladiutor.elements.SplashView.java

private void draw(Canvas canvas, int x, int y, int radius) {
    if (radius > 0)
        canvas.drawCircle(x / 2, y / 2, radius, mPaintCircle);
    matrix.postRotate(rotate);//from  ww  w.j a v  a  2  s .c  o m
    Bitmap iconRotate = Bitmap.createBitmap(icon, 0, 0, icon.getWidth(), icon.getHeight(), matrix, false);
    canvas.drawBitmap(iconRotate, x / 2 - iconRotate.getWidth() / 2, y / 2 - iconRotate.getHeight() / 2,
            mPaintCircle);

    TextPaint textPaint = new TextPaint();
    textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    textPaint.setColor(textColor);
    textPaint.setAntiAlias(true);
    textPaint.setTextAlign(Paint.Align.CENTER);
    textPaint.setTextSize(textSize);
    float textHeight = textPaint.descent() - textPaint.ascent();
    float textOffset = (textHeight / 2) - textPaint.descent();

    canvas.drawText(getResources().getString(R.string.root_waiting), x / 2, y - textOffset - y / 4, textPaint);
}

From source file:com.lab47billion.appchooser.HorizontalPicker.java

public HorizontalPicker(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    _context = context;//from   w  ww  .  j ava2  s .  co m

    // create the selector wheel paint
    TextPaint paint = new TextPaint();
    paint.setAntiAlias(true);
    mTextPaint = paint;

    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.HorizontalPicker, defStyle, 0);

    CharSequence[] values;
    int ellipsize = 3; // END default value
    int sideItems = mSideItems;

    try {
        mTextColor = a.getColorStateList(R.styleable.HorizontalPicker_android_textColor);
        if (mTextColor == null) {
            mTextColor = ColorStateList.valueOf(0xFF000000);
        }

        values = a.getTextArray(R.styleable.HorizontalPicker_values);
        ellipsize = a.getInt(R.styleable.HorizontalPicker_android_ellipsize, ellipsize);
        mDividerSize = a.getDimension(R.styleable.HorizontalPicker_dividerSize, mDividerSize);
        mNormalTextSize = a.getDimension(R.styleable.HorizontalPicker_normalTextSize, 20);
        mSelectedTextSize = a.getDimension(R.styleable.HorizontalPicker_selectedTextSize, -1);
        sideItems = a.getInt(R.styleable.HorizontalPicker_sideItems, sideItems);

        if (mNormalTextSize > -1) {
            setTextSize(mNormalTextSize);
        }
        if (mSelectedTextSize == -1) {
            mSelectedTextSize = mNormalTextSize;
        }

    } finally {
        a.recycle();
    }

    switch (ellipsize) {
    case 1:
        setEllipsize(TextUtils.TruncateAt.START);
        break;
    case 2:
        setEllipsize(TextUtils.TruncateAt.MIDDLE);
        break;
    case 3:
        setEllipsize(TextUtils.TruncateAt.END);
        break;
    case 4:
        setEllipsize(TextUtils.TruncateAt.MARQUEE);
        break;
    }

    Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
    mBoringMetrics = new BoringLayout.Metrics();
    mBoringMetrics.ascent = fontMetricsInt.ascent;
    mBoringMetrics.bottom = fontMetricsInt.bottom;
    mBoringMetrics.descent = fontMetricsInt.descent;
    mBoringMetrics.leading = fontMetricsInt.leading;
    mBoringMetrics.top = fontMetricsInt.top;
    mBoringMetrics.width = mItemWidth;

    setWillNotDraw(false);

    mFlingScrollerX = new OverScroller(context);
    mAdjustScrollerX = new OverScroller(context, new DecelerateInterpolator(2.5f));

    // initialize constants
    ViewConfiguration configuration = ViewConfiguration.get(context);
    mTouchSlop = configuration.getScaledTouchSlop();
    mMinimumFlingVelocity = configuration.getScaledMinimumFlingVelocity();
    mMaximumFlingVelocity = configuration.getScaledMaximumFlingVelocity()
            / SELECTOR_MAX_FLING_VELOCITY_ADJUSTMENT;
    mOverscrollDistance = configuration.getScaledOverscrollDistance();

    mPreviousScrollerX = Integer.MIN_VALUE;

    setValues(values);
    setSideItems(sideItems);

    /*mTouchHelper = new PickerTouchHelper(this);
    ViewCompat.setAccessibilityDelegate(this, mTouchHelper);*/

}