Java Code Examples for android.graphics.Paint#ascent()

The following examples show how to use android.graphics.Paint#ascent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Marker.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
/**
 * @since 6.0.3
 */
public void setTextIcon(final String pText) {
	final Paint background = new Paint();
	background.setColor(mTextLabelBackgroundColor);
	final Paint p = new Paint();
	p.setTextSize(mTextLabelFontSize);
	p.setColor(mTextLabelForegroundColor);
	p.setAntiAlias(true);
	p.setTypeface(Typeface.DEFAULT_BOLD);
	p.setTextAlign(Paint.Align.LEFT);
	final int width = (int) (p.measureText(pText) + 0.5f);
	final float baseline = (int) (-p.ascent() + 0.5f);
	final int height = (int) (baseline + p.descent() + 0.5f);
	final Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
	final Canvas c = new Canvas(image);
	c.drawPaint(background);
	c.drawText(pText, 0, baseline, p);
	mIcon = new BitmapDrawable(mResources, image);
	setAnchor(ANCHOR_CENTER, ANCHOR_CENTER);
}
 
Example 2
Source File: IndexScroller.java    From weixin with Apache License 2.0 6 votes vote down vote up
/**
 * 绘制右侧索引条文本
 * 
 * @param canvas
 */
private void onDrawRightIndexText(Canvas canvas) {
	L.d(LOGTAG, "onDrawRightIndexText");
	// 绘画右侧索引条的字母
	Paint indexPaint = new Paint();
	indexPaint.setColor(Color.parseColor(COLOR_RIGHT_TEXT));
	// indexPaint.setAlpha((int) (255 * mAlphaRate));
	indexPaint.setAntiAlias(true);
	indexPaint.setTextSize(14 * mScaledDensity);

	float sectionHeight = (mIndexbarRect.height() - 2 * mIndexbarMargin)
			/ mSections.length;
	float paddingTop = (sectionHeight - (indexPaint.descent() - indexPaint
			.ascent())) / 2;
	for (int i = 0; i < mSections.length; i++) {
		float paddingLeft = (mIndexbarWidth - indexPaint
				.measureText(mSections[i])) / 2;
		canvas.drawText(mSections[i], mIndexbarRect.left + paddingLeft,
				mIndexbarRect.top + mIndexbarMargin + sectionHeight * i
						+ paddingTop - indexPaint.ascent(), indexPaint);
	}
}
 
Example 3
Source File: OngoingNotificationsReceiver.java    From prayer-times-android with Apache License 2.0 6 votes vote down vote up
private Bitmap getIconFromMinutes(Times t) {
    int left = new Period(LocalDateTime.now(), t.getTime(LocalDate.now(), t.getNextTime()), PeriodType.minutes()).getMinutes();
    Resources r = getContext().getResources();

    int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, r.getDisplayMetrics());
    Bitmap b = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(0xFFFFFFFF);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setTextSize(size);
    paint.setTextSize(size * size / paint.measureText((left < 10 ? left * 10 : left) + ""));
    float yPos = c.getHeight() / 2f - (paint.descent() + paint.ascent()) / 2;
    c.drawText(left + "", size / 2f, yPos, paint);
    return b;
}
 
Example 4
Source File: NoboButton_2.java    From NoboButton with Apache License 2.0 6 votes vote down vote up
private Bitmap getFontBitmap() {

		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(iconColor);

		if (awesomeIconTypeFace != null && !isInEditMode()) {
			paint.setTypeface(awesomeIconTypeFace);
			paint.setTextSize(iconSize);
		} else {
			fontIcon = "o";
			paint.setTextSize(iconSize - 15);
		}

		paint.setTextAlign(Paint.Align.LEFT);
		float baseline = -paint.ascent(); // ascent() is negative
		int width = (int) (paint.measureText(fontIcon) + 0.5f); // round
		int height = (int) (baseline + paint.descent() + 0.5f);
		Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(image);
		canvas.drawText(fontIcon, 0, baseline, paint);
		return image;
	}
 
Example 5
Source File: MainKeyboardView.java    From simple-keyboard with Apache License 2.0 6 votes vote down vote up
private void drawLanguageOnSpacebar(final Key key, final Canvas canvas, final Paint paint) {
    final Keyboard keyboard = getKeyboard();
    if (keyboard == null) {
        return;
    }
    final int width = key.getWidth();
    final int height = key.getHeight();
    paint.setTextAlign(Align.CENTER);
    paint.setTypeface(Typeface.DEFAULT);
    paint.setTextSize(mLanguageOnSpacebarTextSize);
    final String language = layoutLanguageOnSpacebar(paint, keyboard.mId.mSubtype, width);
    // Draw language text with shadow
    final float descent = paint.descent();
    final float textHeight = -paint.ascent() + descent;
    final float baseline = height / 2 + textHeight / 2;
    paint.setColor(mLanguageOnSpacebarTextColor);
    paint.setAlpha(mLanguageOnSpacebarAnimAlpha);
    canvas.drawText(language, width / 2, baseline - descent, paint);
    paint.clearShadowLayer();
    paint.setTextScaleX(1.0f);
}
 
Example 6
Source File: OngoingNotificationsReceiver.java    From prayer-times-android with Apache License 2.0 6 votes vote down vote up
private Bitmap getIconFromMinutes(Times t) {
    int left = new Period(LocalDateTime.now(), t.getTime(LocalDate.now(), t.getNextTime()), PeriodType.minutes()).getMinutes();
    Resources r = getContext().getResources();

    int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, r.getDisplayMetrics());
    Bitmap b = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(0xFFFFFFFF);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setTextSize(size);
    paint.setTextSize(size * size / paint.measureText((left < 10 ? left * 10 : left) + ""));
    float yPos = c.getHeight() / 2f - (paint.descent() + paint.ascent()) / 2;
    c.drawText(left + "", size / 2f, yPos, paint);
    return b;
}
 
Example 7
Source File: TitlePageIndicator.java    From HomeGenie-Android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculate the bounds for a view's title
 *
 * @param index
 * @param paint
 * @return
 */
private Rect calcBounds(int index, Paint paint) {
    //Calculate the text bounds
    Rect bounds = new Rect();
    CharSequence title = getTitle(index);
    bounds.right = (int) paint.measureText(title, 0, title.length());
    bounds.bottom = (int) (paint.descent() - paint.ascent());
    return bounds;
}
 
Example 8
Source File: TitlePageIndicator.java    From arcusandroid with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate the bounds for a view's title
 *
 * @param index
 * @param paint
 * @return
 */
private Rect calcBounds(int index, Paint paint) {
    //Calculate the text bounds
    Rect bounds = new Rect();
    CharSequence title = getTitle(index);
    bounds.right = (int) paint.measureText(title, 0, title.length());
    bounds.bottom = (int) (paint.descent() - paint.ascent());
    return bounds;
}
 
Example 9
Source File: OnTextCenter.java    From ArcProgressBar with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas, RectF rectF, float x, float y,float strokeWidth,int progress) {
    Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setStrokeWidth(35);
    textPaint.setTextSize(textSize);
    textPaint.setColor(textColor);
    String progressStr = String.valueOf(progress);
    float textX = x-(textPaint.measureText(progressStr)/2);
    float textY = y-((textPaint.descent()+textPaint.ascent())/2);
    canvas.drawText(progressStr,textX,textY,textPaint);
}
 
Example 10
Source File: TitlePageIndicator.java    From wakao-app with MIT License 5 votes vote down vote up
/**
 * Calculate the bounds for a view's title
 *
 * @param index
 * @param paint
 * @return
 */
private Rect calcBounds(int index, Paint paint) {
    //Calculate the text bounds
    Rect bounds = new Rect();
    CharSequence title = getTitle(index);
    bounds.right = (int) paint.measureText(title, 0, title.length());
    bounds.bottom = (int) (paint.descent() - paint.ascent());
    return bounds;
}
 
Example 11
Source File: IndexScroller.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 绘制中间预览view
 * 
 * @param canvas
 */
private void onDrawMiddlePreview(Canvas canvas) {
	L.d(LOGTAG, "onDrawMiddlePreview");
	Paint previewPaint = new Paint(); // 用来绘画预览文本背景的画笔
	previewPaint.setColor(Color.parseColor(COLOR_MIDDLE_BACKGROUND));// 设置画笔颜色为黑色
	previewPaint.setAlpha(96); // 设置透明度
	previewPaint.setAntiAlias(true);// 设置抗锯齿
	previewPaint.setShadowLayer(3, 0, 0, Color.argb(64, 0, 0, 0)); // 设置阴影层

	Paint previewTextPaint = new Paint(); // 用来绘画预览字母的画笔
	previewTextPaint.setColor(Color.parseColor(COLOR_MIDDLE_TEXT)); // 设置画笔为白色
	previewTextPaint.setAntiAlias(true); // 设置抗锯齿
	previewTextPaint.setTextSize(60 * mScaledDensity); // 设置字体大小

	// 单个文本的宽度
	float previewTextWidth = previewTextPaint
			.measureText(mSections[mCurrentSection]);

	float previewSize = 2 * mPreviewPadding + previewTextPaint.descent()
			- previewTextPaint.ascent();

	RectF previewRect = new RectF((mListViewWidth - previewSize) / 2,
			(mListViewHeight - previewSize) / 2,
			(mListViewWidth - previewSize) / 2 + previewSize,
			(mListViewHeight - previewSize) / 2 + previewSize);

	// 中间索引的那个框
	canvas.drawRoundRect(previewRect, 5 * mDensity, 5 * mDensity,
			previewPaint);

	// 绘画索引字母
	canvas.drawText(mSections[mCurrentSection], previewRect.left
			+ (previewSize - previewTextWidth) / 2 - 1, previewRect.top
			+ mPreviewPadding - previewTextPaint.ascent() + 1,
			previewTextPaint);
}
 
Example 12
Source File: TitlePageIndicator.java    From InfiniteViewPager with MIT License 5 votes vote down vote up
/**
 * Calculate the bounds for a view's title
 *
 * @param index
 * @param paint
 * @return
 */
private Rect calcBounds(int index, Paint paint) {
    //Calculate the text bounds
    Rect bounds = new Rect();
    CharSequence title = getTitle(index);
    bounds.right = (int) paint.measureText(title, 0, title.length());
    bounds.bottom = (int) (paint.descent() - paint.ascent());
    return bounds;
}
 
Example 13
Source File: TitlePageIndicator.java    From AndroidCacheFoundation with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate the bounds for a view's title
 *
 * @param index
 * @param paint
 * @return
 */
private Rect calcBounds(int index, Paint paint) {
    //Calculate the text bounds
    Rect bounds = new Rect();
    CharSequence title = getTitle(index);
    bounds.right = (int) paint.measureText(title, 0, title.length());
    bounds.bottom = (int) (paint.descent() - paint.ascent());
    return bounds;
}
 
Example 14
Source File: TitlePageIndicator.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate the bounds for a view's title
 *
 * @param index
 * @param paint
 * @return
 */
private Rect calcBounds(int index, Paint paint) {
    //Calculate the text bounds
    Rect bounds = new Rect();
    CharSequence title = getTitle(index);
    bounds.right = (int) paint.measureText(title, 0, title.length());
    bounds.bottom = (int) (paint.descent() - paint.ascent());
    return bounds;
}
 
Example 15
Source File: MapsActivity.java    From vocefiscal-android with Apache License 2.0 5 votes vote down vote up
private Bitmap writeTextOnDrawable(int drawableId, String text) 
{
	Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);

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

	Paint paint = new Paint();
	paint.setStyle(Style.FILL);
	paint.setColor(Color.WHITE);
	paint.setTypeface(tf);
	paint.setTextAlign(Align.CENTER);
	paint.setTextSize(convertToPixels(getBaseContext(), 14));

	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(convertToPixels(getBaseContext(), 7));        //Scaling needs to be used for different dpi's

	//Calculate the positions
	int xPos = (canvas.getWidth() / 2) - 2;     //-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) - 10) ;  

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

	return  bm;
}
 
Example 16
Source File: DrawableUtils.java    From Xndroid with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a rounded square of a certain color with
 * a character imprinted in white on it.
 *
 * @param character the character to write on the image.
 * @param width     the width of the final image.
 * @param height    the height of the final image.
 * @param color     the background color of the rounded square.
 * @return a valid bitmap of a rounded square with a character on it.
 */
@NonNull
public static Bitmap getRoundedLetterImage(@NonNull Character character, int width, int height, int color) {
    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    Paint paint = new Paint();
    paint.setColor(color);
    Typeface boldText = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
    paint.setTypeface(boldText);
    paint.setTextSize(Utils.dpToPx(14));
    paint.setAntiAlias(true);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));

    int radius = Utils.dpToPx(2);

    RectF outer = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
    canvas.drawRoundRect(outer, radius, radius, paint);

    int xPos = (canvas.getWidth() / 2);
    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));

    paint.setColor(Color.WHITE);
    canvas.drawText(character.toString(), xPos, yPos, paint);

    return image;
}
 
Example 17
Source File: RadialTimePickerView.java    From DateTimePicker with Apache License 2.0 5 votes vote down vote up
/**
 * Using the trigonometric Unit Circle, calculate the positions that the text will need to be
 * drawn at based on the specified circle radius. Place the values in the textGridHeights and
 * textGridWidths parameters.
 */
private static void calculatePositions(Paint paint, float radius, float xCenter, float yCenter,
        float textSize, float[] x, float[] y) {
    // Adjust yCenter to account for the text's baseline.
    paint.setTextSize(textSize);
    yCenter -= (paint.descent() + paint.ascent()) / 2;

    for (int i = 0; i < NUM_POSITIONS; i++) {
        x[i] = xCenter - radius * COS_30[i];
        y[i] = yCenter - radius * SIN_30[i];
    }
}
 
Example 18
Source File: EntityWidgetProvider.java    From homeassist with Apache License 2.0 4 votes vote down vote up
public static void updateEntityWidget(Context context, Widget widget) {
    Log.d("YouQi", "Widget updateEntityWidget: " + CommonUtil.deflate(widget));
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    String iconText = widget.getMdiIcon(); //MDIFont.getIcon("mdi:weather-hail");
    int iconColor = ResourcesCompat.getColor(context.getResources(), (widget.isToggleable() && !widget.isCurrentStateActive()) ? R.color.md_grey_500 : R.color.xiaomiPrimaryTextSelected, null);

    Bitmap myBitmap = Bitmap.createBitmap(160, 160, Bitmap.Config.ARGB_8888);
    myBitmap.eraseColor(Color.TRANSPARENT);

    Typeface typeface = ResourcesCompat.getFont(context, R.font.mdi);
    Paint paint = new Paint();
    paint.setFlags(Paint.ANTI_ALIAS_FLAG);
    paint.setAntiAlias(true);
    paint.setTypeface(typeface);
    paint.setColor(iconColor);
    paint.setTextSize(160);
    //paint.setStrokeWidth(24); // Text Size
    //setTextSizeForWidth(paint, 48, iconText);
    //paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern

    Canvas canvas = new Canvas(myBitmap);
    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));
    int xPos = (canvas.getWidth() - yPos) / 2;//(canvas.getWidth() / 2);
    canvas.drawText(iconText, 0, yPos, paint);

    RemoteViews remoteViews = new RemoteViews("com.axzae.homeassistant", R.layout.widget_entity);
    remoteViews.setImageViewBitmap(R.id.image_icon, myBitmap);
    remoteViews.setTextViewText(R.id.text_state, widget.getFriendlyStateRow());
    remoteViews.setTextColor(R.id.text_state, iconColor);
    remoteViews.setTextViewText(R.id.text_group, widget.getFriendlyName());

    //https://stackoverflow.com/questions/21311917/onreceive-will-always-receive-the-last-appwidgetid-even-different-instance-widg
    Intent newIntent = new Intent(context, EntityWidgetProvider.class);
    newIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    newIntent.putExtra("appWidgetId", widget.appWidgetId);
    newIntent.putExtra("widget", CommonUtil.deflate(widget));
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, widget.appWidgetId, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.item, pendingIntent);
    appWidgetManager.updateAppWidget(widget.appWidgetId, remoteViews);
    Log.d("YouQi", "appWidgetManager updating (" + widget.appWidgetId + "): " + widget.getFriendlyState());
}
 
Example 19
Source File: SmileRating.java    From SmileyRating with Apache License 2.0 4 votes vote down vote up
private void drawTextCentered(String text, float x, float y, Paint paint, Canvas canvas) {
    float xPos = x - (paint.measureText(text) / 2);
    float yPos = (y - ((paint.descent() + paint.ascent()) / 2));

    canvas.drawText(text, xPos, yPos, paint);
}
 
Example 20
Source File: DrawableUtils.java    From Xndroid with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
public static Bitmap getRoundedNumberImage(int number, int width, int height, int color, int thickness) {
    String text;

    if (number > 99) {
        text = "\u221E";
    } else {
        text = String.valueOf(number);
    }

    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    Paint paint = new Paint();
    paint.setColor(color);
    Typeface boldText = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
    paint.setTypeface(boldText);
    paint.setTextSize(Utils.dpToPx(14));
    paint.setAntiAlias(true);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));

    int radius = Utils.dpToPx(2);

    RectF outer = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
    canvas.drawRoundRect(outer, radius, radius, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

    radius--;
    RectF inner = new RectF(thickness, thickness, canvas.getWidth() - thickness, canvas.getHeight() - thickness);
    canvas.drawRoundRect(inner, radius, radius, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));

    int xPos = (canvas.getWidth() / 2);
    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));

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

    return image;
}