List of usage examples for android.graphics Paint setTypeface
public Typeface setTypeface(Typeface typeface)
From source file:im.neon.util.VectorUtils.java
/** * Create an avatar bitmap from a text.//w ww .j a va 2s . c om * * @param backgroundColor the background color. * @param text the text to display. * @param pixelsSide the avatar side in pixels * @return the generated bitmap */ private static Bitmap createAvatar(int backgroundColor, String text, int pixelsSide) { android.graphics.Bitmap.Config bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; Bitmap bitmap = Bitmap.createBitmap(pixelsSide, pixelsSide, bitmapConfig); Canvas canvas = new Canvas(bitmap); canvas.drawColor(backgroundColor); // prepare the text drawing Paint textPaint = new Paint(); textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); textPaint.setColor(Color.WHITE); // the text size is proportional to the avatar size. // by default, the avatar size is 42dp, the text size is 28 dp (not sp because it has to be fixed). textPaint.setTextSize(pixelsSide * 2 / 3); // get its size Rect textBounds = new Rect(); textPaint.getTextBounds(text, 0, text.length(), textBounds); // draw the text in center canvas.drawText(text, (canvas.getWidth() - textBounds.width() - textBounds.left) / 2, (canvas.getHeight() + textBounds.height() - textBounds.bottom) / 2, textPaint); // Return the avatar return bitmap; }
From source file:com.android.deskclock.timer.TimerView.java
/** * Measure the text and add a start padding to the view * @param textView view to measure and onb to which add start padding *///from w w w . j a v a 2 s. co m private void addStartPadding(TextView textView) { final float gapPadding = 0.45f; // allDigits will contain ten digits: "0123456789" in the default locale String allDigits = String.format(Locale.getDefault(), "%010d", 123456789); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setTextSize(textView.getTextSize()); paint.setTypeface(textView.getTypeface()); float widths[] = new float[allDigits.length()]; int ll = paint.getTextWidths(allDigits, widths); int largest = 0; for (int ii = 1; ii < ll; ii++) { if (widths[ii] > widths[largest]) { largest = ii; } } // Add left padding to the view - Note: layout inherits LTR textView.setPadding((int) (gapPadding * widths[largest]), 0, 0, 0); }
From source file:foam.jellyfish.StarwispCanvas.java
public void DrawText(Canvas canvas, JSONArray prim, float sx, float sy) { try {// ww w.ja v a2 s. c o m canvas.save(); if (prim.getString(6).equals("vertical")) canvas.rotate(-90, prim.getInt(2) * sx, prim.getInt(3) * sy); Paint myPaint = new Paint(); JSONArray c = prim.getJSONArray(4); myPaint.setColor(Color.rgb(c.getInt(0), c.getInt(1), c.getInt(2))); myPaint.setTextSize(prim.getInt(5)); myPaint.setTypeface(m_Typeface); canvas.drawText(prim.getString(1), prim.getInt(2) * sx, prim.getInt(3) * sy, myPaint); canvas.restore(); } catch (JSONException e) { Log.e("starwisp", "Error parsing data " + e.toString()); } }
From source file:de.treichels.hott.ui.android.html.AndroidCurveImageGenerator.java
private Bitmap getBitmap(final Curve curve, final float scale, final boolean description) { final boolean pitchCurve = curve.getPoint()[0].getPosition() == 0; final float scale1 = scale * 0.75f; // smaller images on the android // platform// ww w . ja va 2 s .co m final Bitmap image = Bitmap.createBitmap((int) (10 + 200 * scale1), (int) (10 + 250 * scale1), Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(image); final Paint backgroundPaint = new Paint(); backgroundPaint.setColor(Color.WHITE); backgroundPaint.setStyle(Style.FILL); final Paint forgroundPaint = new Paint(); forgroundPaint.setColor(Color.BLACK); forgroundPaint.setStyle(Style.STROKE); forgroundPaint.setStrokeWidth(1.0f); forgroundPaint.setStrokeCap(Cap.BUTT); forgroundPaint.setStrokeJoin(Join.ROUND); forgroundPaint.setStrokeMiter(0.0f); final Paint curvePaint = new Paint(forgroundPaint); curvePaint.setFlags(Paint.ANTI_ALIAS_FLAG); curvePaint.setStrokeWidth(2.0f); final Paint pointPaint = new Paint(curvePaint); pointPaint.setStrokeWidth(5.0f); pointPaint.setStyle(Style.FILL_AND_STROKE); final Paint helpLinePaint = new Paint(forgroundPaint); helpLinePaint.setColor(Color.GRAY); helpLinePaint.setPathEffect(new DashPathEffect(new float[] { 5.0f, 5.0f }, 2.5f)); final Paint textPaint = new Paint(forgroundPaint); textPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD)); textPaint.setTextSize(12.0f); textPaint.setTextAlign(Align.CENTER); textPaint.setStyle(Style.FILL); canvas.drawRect(0, 0, 10 + 200 * scale1, 10 + 250 * scale1, backgroundPaint); canvas.drawRect(5, 5, 5 + 200 * scale1, 5 + 250 * scale1, forgroundPaint); canvas.drawLine(5, 5 + 25 * scale1, 5 + 200 * scale1, 5 + 25 * scale1, helpLinePaint); canvas.drawLine(5, 5 + 225 * scale1, 5 + 200 * scale1, 5 + 225 * scale1, helpLinePaint); if (!pitchCurve) { canvas.drawLine(5, 5 + 125 * scale1, 5 + 200 * scale1, 5 + 125 * scale1, helpLinePaint); canvas.drawLine(5 + 100 * scale1, 5, 5 + 100 * scale1, 5 + 250 * scale1, helpLinePaint); } if (curve.getPoint() != null) { int numPoints = 0; for (final CurvePoint p : curve.getPoint()) { if (p.isEnabled()) { numPoints++; } } final double[] xVals = new double[numPoints]; final double[] yVals = new double[numPoints]; int i = 0; for (final CurvePoint p : curve.getPoint()) { if (p.isEnabled()) { if (i == 0) { xVals[i] = pitchCurve ? 0 : -100; } else if (i == numPoints - 1) { xVals[i] = 100; } else { xVals[i] = p.getPosition(); } yVals[i] = p.getValue(); if (description) { float x0; float y0; if (pitchCurve) { x0 = (float) (5 + xVals[i] * 2 * scale1); y0 = (float) (5 + (225 - yVals[i] * 2) * scale1); } else { x0 = (float) (5 + (100 + xVals[i]) * scale1); y0 = (float) (5 + (125 - yVals[i]) * scale1); } canvas.drawPoint(x0, y0, pointPaint); if (y0 < 5 + 125 * scale1) { canvas.drawRect(x0 - 4, y0 + 5, x0 + 3, y0 + 18, backgroundPaint); canvas.drawText(Integer.toString(p.getNumber() + 1), x0 - 1, y0 + 16, textPaint); } else { canvas.drawRect(x0 - 4, y0 - 5, x0 + 3, y0 - 18, backgroundPaint); canvas.drawText(Integer.toString(p.getNumber() + 1), x0 - 1, y0 - 7, textPaint); } } i++; } } if (numPoints > 2 && curve.isSmoothing()) { final SplineInterpolator s = new SplineInterpolator(); final PolynomialSplineFunction function = s.interpolate(xVals, yVals); float x0 = 5; float y0; if (pitchCurve) { y0 = (float) (5 + (225 - yVals[0] * 2) * scale1); } else { y0 = (float) (5 + (125 - yVals[0]) * scale1); } while (x0 < 4 + 200 * scale1) { final float x1 = x0 + 1; float y1; if (pitchCurve) { y1 = (float) (5 + (225 - function.value((x1 - 5) / scale1 / 2) * 2) * scale1); } else { y1 = (float) (5 + (125 - function.value((x1 - 5) / scale1 - 100)) * scale1); } canvas.drawLine(x0, y0, x1, y1, curvePaint); x0 = x1; y0 = y1; } } else { for (i = 0; i < numPoints - 1; i++) { float x0, y0, x1, y1; if (pitchCurve) { x0 = (float) (5 + xVals[i] * 2 * scale1); y0 = (float) (5 + (225 - yVals[i] * 2) * scale1); x1 = (float) (5 + xVals[i + 1] * 2 * scale1); y1 = (float) (5 + (225 - yVals[i + 1] * 2) * scale1); } else { x0 = (float) (5 + (100 + xVals[i]) * scale1); y0 = (float) (5 + (125 - yVals[i]) * scale1); x1 = (float) (5 + (100 + xVals[i + 1]) * scale1); y1 = (float) (5 + (125 - yVals[i + 1]) * scale1); } canvas.drawLine(x0, y0, x1, y1, curvePaint); } } } return image; }
From source file:org.stockchart.core.Appearance.java
public void applyText(Paint p) { p.reset();// w ww .ja v a 2 s.c om p.setTextSize(fFont.getSize()); p.setColor(fFont.getColor()); p.setTypeface(fFont.getTypeface()); p.setAntiAlias(fIsAntialias); p.setPathEffect(null); p.setStyle(Style.FILL); }
From source file:org.cicadasong.samples.tubestatus.TubeStatus.java
protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setTextAlign(Paint.Align.CENTER); paint.setTypeface(Typeface.DEFAULT_BOLD); // We've centered the output vertically, so it works with the reduced canvas height in // widget mode. int y = canvas.getHeight() / 2; int x = canvas.getWidth() / 2; paint.setTypeface(Typeface.DEFAULT); paint.setTextSize(11);/*w w w .jav a 2 s.com*/ canvas.drawText(TubeLine.allLines.get(selectionIndex).name, x, y - paint.descent() - 1, paint); paint.setTextSize(11); // TODO dynamically adjust font size depending on length of status string? canvas.drawText(status, x, y + (int) -paint.ascent() + 1, paint); }
From source file:com.gruporaido.tasker_library.util.Helper.java
/** * @param drawableId/*ww w. ja v a 2s.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.cssweb.android.view.FinanceMini.java
public void drawHKIndex(Canvas canvas) { Paint paint = this.mPaint; paint.setTypeface(Typeface.DEFAULT_BOLD); paint.setAntiAlias(true);//w ww. j av a2 s. c om 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 .ja v a2s . c om*/ 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()); } } }
From source file:org.jraf.android.piclabel.app.form.FormActivity.java
private void drawText(Canvas canvas) { Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setAntiAlias(true);//from ww w . j a va 2 s . c om paint.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/" + getSelectedFontName())); int textSize = canvas.getHeight() / 35; paint.setTextSize(textSize); int margin = textSize / 5; // Measure date/time String dateTime = mEdtDateTime.getText().toString(); Rect boundsDateTime = new Rect(); paint.getTextBounds(dateTime, 0, dateTime.length(), boundsDateTime); // Measure location String location = mEdtLocation.getText().toString(); Rect boundsLocation = new Rect(); paint.getTextBounds(location, 0, location.length(), boundsLocation); int totalWidth = boundsDateTime.width() + textSize * 2 + boundsLocation.width(); if (totalWidth > canvas.getWidth()) { // Draw on 2 lines // Draw a rectangle paint.setColor(Color.argb(180, 0, 0, 0)); canvas.drawRect(0, 0, canvas.getWidth(), -boundsDateTime.top + boundsDateTime.bottom + -boundsLocation.top + boundsLocation.bottom + margin * 3, paint); // Draw date/time paint.setColor(Color.WHITE); canvas.drawText(dateTime, margin, margin + -boundsDateTime.top, paint); // Draw location canvas.drawText(location, canvas.getWidth() - boundsLocation.right - boundsLocation.left - margin, margin + -boundsDateTime.top + boundsDateTime.bottom + margin + -boundsLocation.top, paint); } else { // Draw on 1 line // Draw a rectangle paint.setColor(Color.argb(180, 0, 0, 0)); canvas.drawRect(0, 0, canvas.getWidth(), margin + Math.max(boundsDateTime.height(), boundsLocation.height()) + margin, paint); // Draw date/time paint.setColor(Color.WHITE); canvas.drawText(dateTime, margin, margin + -boundsDateTime.top, paint); // Draw location canvas.drawText(location, canvas.getWidth() - boundsLocation.right - boundsLocation.left - margin, margin + -boundsLocation.top, paint); } }