Example usage for android.graphics Paint setTextAlign

List of usage examples for android.graphics Paint setTextAlign

Introduction

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

Prototype

public void setTextAlign(Align align) 

Source Link

Document

Set the paint's text alignment.

Usage

From source file:com.gruporaido.tasker_library.util.Helper.java

/**
 * @param drawableId//  ww w  .  jav a2  s. co m
 * @param text
 * @param textSize
 * @param offsetX
 * @param offsetY
 * @return
 */
public Bitmap drawTextOnDrawable(int drawableId, String text, int textSize, int offsetX, int offsetY) {

    Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), drawableId).copy(Bitmap.Config.ARGB_8888,
            true);

    Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTypeface(tf);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setTextSize(dpToPx(textSize));

    Rect textRect = new Rect();
    paint.getTextBounds(text, 0, text.length(), textRect);

    Canvas canvas = new Canvas(bm);

    //If the text is bigger than the canvas , reduce the font size
    if (textRect.width() >= (canvas.getWidth() - 4)) //the padding on either sides is considered as 4, so as to appropriately fit in the text
        paint.setTextSize(dpToPx(textSize / 2)); //Scaling needs to be used for different dpi's

    //Calculate the positions
    int xPos = (canvas.getWidth() / 2) - 2 + dpToPx(offsetX); //-2 is for regulating the x position offset

    //"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) + dpToPx(offsetY);

    canvas.drawText(text, xPos, yPos, paint);

    return bm;
}

From source file:com.hippo.widget.Slider.java

@SuppressWarnings("deprecation")
private void init(Context context, AttributeSet attrs) {
    mContext = context;//  w ww .j a  v a 2 s  .c o  m
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setTextAlign(Paint.Align.CENTER);

    Resources resources = context.getResources();
    mBubbleMinWidth = resources.getDimensionPixelOffset(R.dimen.slider_bubble_width);
    mBubbleMinHeight = resources.getDimensionPixelOffset(R.dimen.slider_bubble_height);

    mBubble = new BubbleView(context, textPaint);
    mBubble.setScaleX(0.0f);
    mBubble.setScaleY(0.0f);
    AbsoluteLayout absoluteLayout = new AbsoluteLayout(context);
    absoluteLayout.addView(mBubble);
    absoluteLayout.setBackgroundDrawable(null);
    mPopup = new PopupWindow(absoluteLayout);
    mPopup.setOutsideTouchable(false);
    mPopup.setTouchable(false);
    mPopup.setFocusable(false);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Slider);
    textPaint.setColor(a.getColor(R.styleable.Slider_textColor, Color.WHITE));
    textPaint.setTextSize(a.getDimensionPixelSize(R.styleable.Slider_textSize, 12));

    updateTextSize();

    setRange(a.getInteger(R.styleable.Slider_start, 0), a.getInteger(R.styleable.Slider_end, 0));
    setProgress(a.getInteger(R.styleable.Slider_slider_progress, 0));
    mThickness = a.getDimension(R.styleable.Slider_thickness, 2);
    mRadius = a.getDimension(R.styleable.Slider_radius, 6);
    setColor(a.getColor(R.styleable.Slider_color, Color.BLACK));

    mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mBgPaint.setColor(a.getBoolean(R.styleable.Slider_dark, false) ? 0x4dffffff : 0x42000000);

    a.recycle();

    mProgressAnimation = new ValueAnimator();
    mProgressAnimation.setInterpolator(AnimationUtils.FAST_SLOW_INTERPOLATOR);
    mProgressAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(@NonNull ValueAnimator animation) {
            float value = (Float) animation.getAnimatedValue();
            mDrawPercent = value;
            mDrawProgress = Math.round(MathUtils.lerp((float) mStart, mEnd, value));
            updateBubblePosition();
            mBubble.setProgress(mDrawProgress);
            invalidate();
        }
    });

    mBubbleScaleAnimation = new ValueAnimator();
    mBubbleScaleAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(@NonNull ValueAnimator animation) {
            float value = (Float) animation.getAnimatedValue();
            mDrawBubbleScale = value;
            mBubble.setScaleX(value);
            mBubble.setScaleY(value);
            invalidate();
        }
    });
}

From source file:com.codetroopers.shakemytours.ui.activity.TripActivity.java

private Bitmap createMarker(int radius, int strokeWidth, String letter, int strokeColor, int backgroundColor) {
    int width = (radius * 2) + (strokeWidth * 2);
    Bitmap marker = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(marker);

    Paint strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    strokePaint.setColor(strokeColor);//ww  w .j a v  a2  s  . co m
    strokePaint.setShadowLayer(strokeWidth, 1.0f, 1.0f, Color.BLACK);
    canvas.drawCircle(width / 2, width / 2, radius, strokePaint);

    Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    backgroundPaint.setColor(backgroundColor);
    canvas.drawCircle(width / 2, width / 2, radius - strokeWidth, backgroundPaint);

    if (letter != null) {
        Rect result = new Rect();
        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.map_marker_text_size));
        textPaint.setColor(strokeColor);
        textPaint.getTextBounds(letter, 0, letter.length(), result);
        int yOffset = result.height() / 2;

        canvas.drawText(letter, width / 2, (width / 2) + yOffset, textPaint);
    }
    return marker;
}

From source file:com.metinkale.prayerapp.vakit.fragments.MainFragment.java

private void export(int csvpdf, LocalDate from, LocalDate to) throws IOException {
    File outputDir = getActivity().getCacheDir();
    if (!outputDir.exists())
        outputDir.mkdirs();//from  w  w w .  java  2 s. c om
    File outputFile = new File(outputDir, mTimes.getName().replace(" ", "_") + (csvpdf == 0 ? ".csv" : ".pdf"));
    if (outputDir.exists())
        outputFile.delete();
    FileOutputStream outputStream;

    outputStream = new FileOutputStream(outputFile);
    if (csvpdf == 0) {
        outputStream.write("Date;Fajr;Shuruq;Dhuhr;Asr;Maghrib;Ishaa\n".getBytes());

        do {
            outputStream.write((from.toString("yyyy-MM-dd") + ";").getBytes());
            outputStream.write((mTimes.getTime(from, 0) + ";").getBytes());
            outputStream.write((mTimes.getTime(from, 1) + ";").getBytes());
            outputStream.write((mTimes.getTime(from, 2) + ";").getBytes());
            outputStream.write((mTimes.getTime(from, 3) + ";").getBytes());
            outputStream.write((mTimes.getTime(from, 4) + ";").getBytes());
            outputStream.write((mTimes.getTime(from, 5) + "\n").getBytes());
        } while (!(from = from.plusDays(1)).isAfter(to));
        outputStream.close();

    } else {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            PdfDocument document = new PdfDocument();

            PdfDocument.PageInfo pageInfo = null;
            int pw = 595;
            int ph = 842;
            pageInfo = new PdfDocument.PageInfo.Builder(pw, ph, 1).create();
            PdfDocument.Page page = document.startPage(pageInfo);
            Drawable launcher = Drawable.createFromStream(getActivity().getAssets().open("pdf/launcher.png"),
                    null);
            Drawable qr = Drawable.createFromStream(getActivity().getAssets().open("pdf/qrcode.png"), null);
            Drawable badge = Drawable.createFromStream(
                    getActivity().getAssets().open("pdf/badge_" + Prefs.getLanguage() + ".png"), null);

            launcher.setBounds(30, 30, 30 + 65, 30 + 65);
            qr.setBounds(pw - 30 - 65, 30 + 65 + 5, pw - 30, 30 + 65 + 5 + 65);
            int w = 100;
            int h = w * badge.getIntrinsicHeight() / badge.getIntrinsicWidth();
            badge.setBounds(pw - 30 - w, 30 + (60 / 2 - h / 2), pw - 30, 30 + (60 / 2 - h / 2) + h);

            Canvas canvas = page.getCanvas();

            Paint paint = new Paint();
            paint.setARGB(255, 0, 0, 0);
            paint.setTextSize(10);
            paint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText("com.metinkale.prayer", pw - 30 - w / 2, 30 + (60 / 2 - h / 2) + h + 10, paint);

            launcher.draw(canvas);
            qr.draw(canvas);
            badge.draw(canvas);

            paint.setARGB(255, 61, 184, 230);
            canvas.drawRect(30, 30 + 60, pw - 30, 30 + 60 + 5, paint);

            if (mTimes.getSource().resId != 0) {
                Drawable source = getResources().getDrawable(mTimes.getSource().resId);

                h = 65;
                w = h * source.getIntrinsicWidth() / source.getIntrinsicHeight();
                source.setBounds(30, 30 + 65 + 5, 30 + w, 30 + 65 + 5 + h);
                source.draw(canvas);
            }

            paint.setARGB(255, 0, 0, 0);
            paint.setTextSize(40);
            paint.setTextAlign(Paint.Align.LEFT);
            canvas.drawText(getText(R.string.appName).toString(), 30 + 65 + 5, 30 + 50, paint);
            paint.setTextAlign(Paint.Align.CENTER);
            paint.setFakeBoldText(true);
            canvas.drawText(mTimes.getName(), pw / 2.0f, 30 + 65 + 50, paint);

            paint.setTextSize(12);
            int y = 30 + 65 + 5 + 65 + 30;
            int p = 30;
            int cw = (pw - p - p) / 7;
            canvas.drawText(getString(R.string.date), 30 + (0.5f * cw), y, paint);
            canvas.drawText(Vakit.IMSAK.getString(), 30 + (1.5f * cw), y, paint);
            canvas.drawText(Vakit.GUNES.getString(), 30 + (2.5f * cw), y, paint);
            canvas.drawText(Vakit.OGLE.getString(), 30 + (3.5f * cw), y, paint);
            canvas.drawText(Vakit.IKINDI.getString(), 30 + (4.5f * cw), y, paint);
            canvas.drawText(Vakit.AKSAM.getString(), 30 + (5.5f * cw), y, paint);
            canvas.drawText(Vakit.YATSI.getString(), 30 + (6.5f * cw), y, paint);
            paint.setFakeBoldText(false);
            do {
                y += 20;
                canvas.drawText((from.toString("dd.MM.yyyy")), 30 + (0.5f * cw), y, paint);
                canvas.drawText((mTimes.getTime(from, 0)), 30 + (1.5f * cw), y, paint);
                canvas.drawText((mTimes.getTime(from, 1)), 30 + (2.5f * cw), y, paint);
                canvas.drawText((mTimes.getTime(from, 2)), 30 + (3.5f * cw), y, paint);
                canvas.drawText((mTimes.getTime(from, 3)), 30 + (4.5f * cw), y, paint);
                canvas.drawText((mTimes.getTime(from, 4)), 30 + (5.5f * cw), y, paint);
                canvas.drawText((mTimes.getTime(from, 5)), 30 + (6.5f * cw), y, paint);
            } while (!(from = from.plusDays(1)).isAfter(to));
            document.finishPage(page);

            document.writeTo(outputStream);

            // close the document
            document.close();

        } else {
            Toast.makeText(getActivity(), R.string.versionNotSupported, Toast.LENGTH_LONG).show();
        }
    }

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType(csvpdf == 0 ? "text/csv" : "application/pdf");

    Uri uri = FileProvider.getUriForFile(getActivity(), "com.metinkale.prayer.fileprovider", outputFile);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.export)));
}

From source file:foam.starwisp.DrawableMap.java

public Marker AddText(final LatLng location, final String text, final int padding, final int fontSize,
        int colour) {
    Marker marker = null;/*w  w  w.j  a v  a  2s .  c om*/

    final TextView textView = new TextView(m_Context);
    textView.setText(text);
    textView.setTextSize(fontSize);
    textView.setTypeface(m_Context.m_Typeface);

    final Paint paintText = textView.getPaint();

    final Rect boundsText = new Rect();
    paintText.getTextBounds(text, 0, textView.length(), boundsText);
    paintText.setTextAlign(Paint.Align.CENTER);

    final Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    final Bitmap bmpText = Bitmap.createBitmap(boundsText.width() + 2 * padding,
            boundsText.height() + 2 * padding, conf);

    final Canvas canvasText = new Canvas(bmpText);
    paintText.setColor(Color.BLACK);

    canvasText.drawText(text, (canvasText.getWidth() / 2) + 3,
            (canvasText.getHeight() - padding - boundsText.bottom) + 3, paintText);

    paintText.setColor(colour);

    canvasText.drawText(text, canvasText.getWidth() / 2, canvasText.getHeight() - padding - boundsText.bottom,
            paintText);

    final MarkerOptions markerOptions = new MarkerOptions().position(location)
            .icon(BitmapDescriptorFactory.fromBitmap(bmpText)).anchor(0.5f, 1);

    marker = map.addMarker(markerOptions);

    return marker;
}

From source file:com.android.gallery3d.data.UriImage.java

public Bitmap drawTextToBitmap(Context gContext, String gText, Bitmap bitmap) {
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;

    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }/*from  www  . ja va 2  s . c  o m*/
    // resource bitmaps are imutable, 
    // so we need to convert it to mutable one
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);
    // new antialised Paint
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(Color.rgb(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (25 * scale));
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    // draw text to the Canvas center
    Rect bounds = new Rect();
    paint.setTextAlign(Align.CENTER);

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

    canvas.drawText(gText, x * scale, y * scale, paint);

    return bitmap;
}

From source file:com.rks.musicx.misc.utils.Helper.java

/**
 * Return text As bitmap//from   w ww.ja  v a 2 s.  c o m
 *
 * @param text
 * @param textSize
 * @param textColor
 * @return
 */
public static Bitmap textAsBitmap(String text, float textSize, int textColor) {
    Paint paint = new Paint(ANTI_ALIAS_FLAG);
    paint.setTextSize(textSize); //text size
    paint.setColor(textColor); //text color
    paint.setTextAlign(Paint.Align.LEFT); //align center
    float baseline = -paint.ascent(); // ascent() is negative
    int width = (int) (paint.measureText(text) + 0.0f); // round
    int height = (int) (baseline + paint.descent() + 0.0f);
    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    canvas.drawText(text, 0, baseline, paint); //draw text
    return image;
}

From source file:com.cssweb.android.view.FinanceMini.java

public void drawHKIndex(Canvas canvas) {
    Paint paint = this.mPaint;
    paint.setTypeface(Typeface.DEFAULT_BOLD);
    paint.setAntiAlias(true);/*  w w w.  ja  va  2s  .  c o m*/
    if (quoteData != null) {
        try {
            JSONArray jArr = quoteData.getJSONArray("data");
            JSONObject jo = jArr.getJSONObject(0);

            paint.setTextAlign(Paint.Align.LEFT);
            paint.setTextSize(mTextSize);
            paint.setColor(GlobalColor.colorLabelName);
            canvas.translate(0, DY);
            canvas.drawText("", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x + tips, y, paint);

            paint.setTextAlign(Paint.Align.RIGHT);
            canvas.translate(width, -DY * 3);
            paint.setColor(GlobalColor.colorpriceUp);
            canvas.translate(0, DY);
            canvas.drawText(String.valueOf(jo.getInt("zj")), x - tips, y, paint);

            paint.setColor(GlobalColor.colorPriceEqual);
            canvas.translate(0, DY);
            canvas.drawText(String.valueOf(jo.getInt("pj")), x - tips, y, paint);

            paint.setColor(GlobalColor.colorPriceDown);
            canvas.translate(0, DY);
            canvas.drawText(String.valueOf(jo.getInt("dj")), x - tips, y, paint);

        } catch (JSONException e) {
            Log.e(TAG, e.toString());
        }
    }
}

From source file:com.cssweb.android.view.FinanceMini.java

public void drawIndex(Canvas canvas) {
    //canvas.restore();
    Paint paint = this.mPaint;
    paint.setTypeface(Typeface.DEFAULT_BOLD);
    paint.setAntiAlias(true);//from w  ww .j av  a2s  .  c o m
    if (quoteData != null) {
        try {
            JSONArray jArr = quoteData.getJSONArray("data");
            JSONObject jo = jArr.getJSONObject(0);

            paint.setTextAlign(Paint.Align.LEFT);
            paint.setTextSize(mTextSize);
            paint.setColor(GlobalColor.colorLabelName);
            canvas.translate(0, DY);
            canvas.drawText("?", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("???", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x, y, paint);

            paint.setTextAlign(Paint.Align.RIGHT);
            canvas.translate(width, -DY * 5);
            paint.setColor(GlobalColor.colorStockName);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("a"), true), x, y, paint);
            canvas.translate(0, DY);
            paint.setColor(GlobalColor.colorStockName);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("b"), true), x, y, paint);
            canvas.translate(0, DY);
            paint.setColor(GlobalColor.colorStockName);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("govbond"), true), x, y, paint);
            canvas.translate(0, DY);
            paint.setColor(GlobalColor.colorStockName);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("fund"), true), x, y, paint);
            canvas.translate(0, DY);
            paint.setColor(GlobalColor.colorStockName);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("warrant"), true), x, y, paint);
            canvas.translate(0, DY);
            paint.setColor(GlobalColor.colorStockName);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("bond"), true), x, y, paint);

        } catch (JSONException e) {
            Log.e(TAG, e.toString());
        }
    }
}