public SizeF GetStringSize(string aString, string aFontName, float aFontSize, HorizontalAlignment aHorizontalAlignment, VerticalAlignment aVerticalAlignment) { if (aString == null) { return(new SizeF()); } var vTextPaint = new TextPaint { TextSize = aFontSize }; vTextPaint.SetTypeface(NativeFontService.Instance.GetTypeface(aFontName)); Layout.Alignment vAlignment; switch (aHorizontalAlignment) { case HorizontalAlignment.Center: vAlignment = Layout.Alignment.AlignCenter; break; case HorizontalAlignment.Right: vAlignment = Layout.Alignment.AlignOpposite; break; default: vAlignment = Layout.Alignment.AlignNormal; break; } StaticLayout vLayout = TextLayoutUtils.CreateLayout(aString, vTextPaint, null, vAlignment); SizeF vSize = vLayout.GetTextSizeAsSizeF(false); vLayout.Dispose(); return(vSize); }
private void SetFont() { using (var typeface = Typeface.Create(_FontFamily, IsItalic ? TypefaceStyle.Italic : TypefaceStyle.Normal)) { _native.SetTypeface(typeface); } }
private static TextPaint InnerBuildPaint(FontWeight fontWeight, FontStyle fontStyle, FontFamily fontFamily, double fontSize, double characterSpacing, Windows.UI.Color foreground, BaseLineAlignment baselineAlignment, UnderlineStyle underlineStyle) { var paint = new TextPaint(PaintFlags.AntiAlias); var paintSpecs = BuildPaintValueSpecs(fontSize, characterSpacing); paint.Density = paintSpecs.density; paint.TextSize = paintSpecs.textSize; paint.UnderlineText = underlineStyle == UnderlineStyle.Single; if (baselineAlignment == BaseLineAlignment.Superscript) { paint.BaselineShift += (int)(paint.Ascent() / 2); } if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop) { paint.LetterSpacing = paintSpecs.letterSpacing; } else { LogCharacterSpacingNotSupported(); } var typefaceStyle = TypefaceStyleHelper.GetTypefaceStyle(fontStyle, fontWeight); var typeface = FontHelper.FontFamilyToTypeFace(fontFamily, fontWeight, typefaceStyle); paint.SetTypeface(typeface); paint.Color = foreground; return(paint); }
private void DrawText(Canvas canvas, string text, float x, float y, float textSize, Typeface typeface, Color color, float pictureWidth = 0) { var paintText = new TextPaint(PaintFlags.LinearText | PaintFlags.AntiAlias) { TextSize = textSize }; var textRect = new Rect(); paintText.GetTextBounds(text, 0, text.Length, textRect); paintText.SetARGB(color.A, color.R, color.G, color.B); paintText.SetTypeface(typeface); var p = new TextPaint(); p.SetTypeface(typeface); p.TextSize = textSize; var ellipsizedText = TextUtils.Ellipsize(text, p, Width - 75.ToPixels(), TextUtils.TruncateAt.End); if (ellipsizedText.IsNullOrEmpty()) { ellipsizedText = text; } if (this.Services().Localize.IsRightToLeft) { x = canvas.Width - pictureWidth - 15.ToPixels() - textRect.Width() - (ShowRightArrow ? 35.ToPixels() : 0); } canvas.DrawText(ellipsizedText, x, y, paintText); }
/// <summary> /// Create an <see cref="IconDrawable" />. /// </summary> /// <param name="context">Your activity or application context.</param> /// <param name="icon">The icon you want this drawable to display.</param> public IconDrawable(Context context, IIcon icon) { var module = Iconize.FindModuleOf(icon); Console.WriteLine("IconDrawable.Icon " + icon); if (icon is null) { throw new Java.Lang.IllegalStateException("El icono no puede estar nulo."); } if (module is null) { throw new Java.Lang.IllegalStateException($"Unable to find the module associated with icon {icon.Key}, have you registered the module you are trying to use with Iconize.With(...) in your Application?"); } _context = context; _icon = icon; _paint = new TextPaint { AntiAlias = true, TextAlign = Paint.Align.Center, UnderlineText = false }; _paint.SetStyle(Paint.Style.Fill); _paint.SetTypeface(module.ToTypeface(context)); }
public static Bitmap GetBitmap(Context context, string text, string backgroundColor, string textColor, int height = 3000, int width = 3000) { Bitmap bitmap = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888); Canvas canvas = new Canvas(bitmap); canvas.DrawColor(GetColorFromHex(backgroundColor)); // new antialiased Paint TextPaint paint = new TextPaint(PaintFlags.AntiAlias) { // Set text Color Color = GetColorFromHex(textColor) }; Typeface typeFace = Typeface.CreateFromAsset(context.Assets, "Neuton-Bold.ttf"); paint.SetTypeface(typeFace); // text size in pixels paint.TextSize = GetFontSize(text); paint.TextAlign = Paint.Align.Left; // set text width to canvas width minus screen padding(10 percent of width) in dp int textWidth = ((int)(canvas.Width - (0.1 * width))); var textPreview = new SpannableString(text); // Reduce the text of the brand name textPreview.SetSpan(new RelativeSizeSpan(0.5f), text.Length - Helpers.Constants.BRAND_NAME.Length, text.Length, SpanTypes.ExclusiveExclusive); // init StaticLayout for text StaticLayout textLayout; if (Build.VERSION.SdkInt >= BuildVersionCodes.M) { textLayout = StaticLayout.Builder.Obtain(textPreview, 0, text.Length, paint, textWidth).Build(); } else { textLayout = new StaticLayout(textPreview, paint, textWidth, Layout.Alignment.AlignNormal, 1.0f, 0.0f, false); } // get height of multiline text int?textHeight = textLayout?.Height; // get position of text's top left corner float x = (bitmap.Width - textWidth) / 2; float y = (bitmap.Height - textHeight ?? 0) / 2; // draw text to the Canvas center canvas.Save(); canvas.Translate(x, y); textLayout.Draw(canvas); canvas.Restore(); return(bitmap); }
void Apply(TextPaint paint) { paint.SetTypeface(_font.ToTypeface(_fontManager)); paint.TextSize = TypedValue.ApplyDimension( _font.AutoScalingEnabled ? ComplexUnitType.Sp : ComplexUnitType.Dip, (float)_font.Size, (_context ?? AAplication.Context)?.Resources?.DisplayMetrics); }
//http://egoco.de/post/19077604048/calculating-the-height-of-text-in-android //http://stackoverflow.com/questions/16082359/how-to-auto-adjust-text-size-on-a-multi-line-textview-according-to-the-view-max public SizeF MeasureString(string text, int maxWidth = 2147483647) { TextPaint paint = AndroidBrush.CreateTextPaint(); paint.TextSize = SizeInPoints; paint.SetTypeface(InnerFont); AndroidTextLayout layout = new AndroidTextLayout(paint); return(layout.MeasureString(text, maxWidth)); }
private void Init(Context context, Icon icon) { _context = context; _icon = icon; _paint = new TextPaint(); _paint.SetTypeface(Iconify.FindTypefaceOf(icon).GetTypeface(context)); _paint.SetStyle(Paint.Style.Fill); _paint.TextAlign = Paint.Align.Center; _paint.UnderlineText = false; _paint.Color = Color.Black; _paint.AntiAlias = true; }
//http://egoco.de/post/19077604048/calculating-the-height-of-text-in-android //http://stackoverflow.com/questions/16082359/how-to-auto-adjust-text-size-on-a-multi-line-textview-according-to-the-view-max public SizeF MeasureString(string text, int maxWidth = 2147483647) { var key = new TextMeasureKey(text, InnerFont, SizeInPoints, maxWidth); return(_measurements.GetOrAdd(key, k => { TextPaint paint = AndroidBrush.CreateTextPaint(); paint.TextSize = k.SizeInPoints; paint.SetTypeface(k.Font); AndroidTextLayout layout = new AndroidTextLayout(paint); return layout.MeasureString(k.Text, k.MaxWidth); })); }
public Bitmap drawTextToBitmap(Photo photo) { Android.Content.Res.Resources resources = context.Resources; BitmapFactory.Options options = new BitmapFactory.Options(); options.InScaled = false; Bitmap bitmap = BitmapFactory.DecodeResource(resources, photo.mPhotoID, options); try { float scale = resources.DisplayMetrics.Density; Bitmap.Config bitmapConfig = bitmap.GetConfig(); // set default bitmap config if none if (bitmapConfig == null) { bitmapConfig = Bitmap.Config.Argb8888; } // resource bitmaps are imutable, // so we need to convert it to mutable one bitmap = bitmap.Copy(bitmapConfig, true); Console.WriteLine("Height:" + bitmap.Height + " Width:" + bitmap.Width); Canvas canvas = new Canvas(bitmap); Console.WriteLine("Canvas Height:" + canvas.Height + " Width:" + canvas.Width); TextPaint mTextPaint = new TextPaint(PaintFlags.AntiAlias); mTextPaint.Color = Color.ParseColor(photo.Hex); mTextPaint.SetTypeface(Typeface.Create(Typeface.DefaultBold, TypefaceStyle.Bold)); int size = 30; mTextPaint.TextSize = ((int)(size * scale)); StaticLayout mTextLayout = new StaticLayout(TrophyName, mTextPaint, (int)photo.W, Layout.Alignment.AlignCenter, 1.0f, 0.0f, false); while (true) { if (mTextLayout.Height > photo.H) { mTextPaint.TextSize = (((size--) * scale)); mTextLayout = new StaticLayout(TrophyName, mTextPaint, (int)photo.W, Layout.Alignment.AlignCenter, 1.0f, 0.0f, false); } else { break; } } canvas.Save(); canvas.Translate(photo.X, photo.Y + (photo.H - mTextLayout.Height) / 2); mTextLayout.Draw(canvas); canvas.Restore(); } catch (Exception e) { Console.WriteLine("Bitmap Text Merge Exception:" + e.ToString()); } return(bitmap); }
private void ApplyFont (TextPaint p) { var oldTypeface = p.Typeface; var oldStyle = oldTypeface != null ? oldTypeface.Style : TypefaceStyle.Normal; var fakeStyle = oldStyle & ~font.Typeface.Style; if ((fakeStyle & TypefaceStyle.Bold) != 0) p.FakeBoldText = true; if ((fakeStyle & TypefaceStyle.Italic) != 0) p.TextSkewX = -0.25f; p.SetTypeface (font.Typeface); }
public static TextPaint GlobalGetFontPaint (Font font, TextAlignment alignment) { var paint = new TextPaint (PaintFlags.AntiAlias); paint.TextAlign = Paint.Align.Left; if (alignment == TextAlignment.Center) paint.TextAlign = Paint.Align.Center; else if (alignment == TextAlignment.Right) paint.TextAlign = Paint.Align.Right; paint.TextSize = (float)font.Size; var typeface = Typeface.Create (font.Family, TypefaceStyle.Normal); paint.SetTypeface (typeface); return paint; }
public FontDrawable(Context context, string text, Color iconColor, int iconSizeDP, string font) { this.text = text; if (font == null || font.Length <= 0) { font = "fontawesome.ttf"; } paint.SetTypeface(Typeface.CreateFromAsset(context.Assets, font)); paint.SetStyle(Paint.Style.Fill); paint.TextAlign = Paint.Align.Center; paint.Color = iconColor; paint.AntiAlias = true; size = GetPX(context, iconSizeDP); SetBounds(0, 0, size, size); }
private TextPaint getPaint(IBrush brush) { TextPaint paint = ((AndroidBrush)brush).InnerBrush; paint.SetTypeface(((AndroidFont)_config.Font).InnerFont); paint.TextSize = _config.Font.SizeInPoints; if (_config.Font.Style.HasFlag(FontStyle.Underline)) { paint.Flags |= PaintFlags.UnderlineText; } if (_config.Font.Style.HasFlag(FontStyle.Strikeout)) { paint.Flags |= PaintFlags.StrikeThruText; } return(paint); }
private void InitPaint() { mTextPaint = new TextPaint(); mTextPaint.AntiAlias = true; mTextPaint.TextSize = TextSize; mTextPaint.Color = mColor; mTextPaint.SetStyle(APaint.Style.Fill); mTextPaint.SetTypeface(Typeface); mTextPaintOutline = new TextPaint(); mTextPaintOutline.AntiAlias = true; mTextPaintOutline.TextSize = TextSize; mTextPaintOutline.Color = mBorderColor; mTextPaintOutline.SetStyle(APaint.Style.Stroke); mTextPaintOutline.SetTypeface(Typeface); mTextPaintOutline.StrokeWidth = mBorderSize; }
private void InitPaint() { mTextPaint = new TextPaint(); mTextPaint.AntiAlias = true; // (true); mTextPaint.TextSize = TextSize; // (getTextSize()); mTextPaint.Color = mColor; // SetColor(mColor); mTextPaint.SetStyle(Android.Graphics.Paint.Style.Fill); mTextPaint.SetTypeface(Typeface); mTextPaintOutline = new TextPaint(); mTextPaintOutline.AntiAlias = true;// (true); mTextPaintOutline.TextSize = TextSize; mTextPaintOutline.Color = mBorderColor; mTextPaintOutline.SetStyle(Android.Graphics.Paint.Style.Stroke); mTextPaintOutline.SetTypeface(Typeface); mTextPaintOutline.StrokeWidth = mBorderSize;// SetStrokeWidth(mBorderSize); }
private void ApplyFont(TextPaint p) { var oldTypeface = p.Typeface; var oldStyle = oldTypeface != null ? oldTypeface.Style : TypefaceStyle.Normal; var fakeStyle = oldStyle & ~font.Typeface.Style; if ((fakeStyle & TypefaceStyle.Bold) != 0) { p.FakeBoldText = true; } if ((fakeStyle & TypefaceStyle.Italic) != 0) { p.TextSkewX = -0.25f; } p.SetTypeface(font.Typeface); }
private static void ApplyCustomTypeFace(TextPaint paint, Typeface typeFace) { TypefaceStyle oldStyle; Typeface old = paint.Typeface; if (old == null) { oldStyle = TypefaceStyle.Normal; } else { oldStyle = old.Style; } // TODO: Ver si tenemos que setear el fake bold, italic paint.SetTypeface(typeFace); }
private void Init(Context context, string fontFacePath) { _context = context; _paint = new TextPaint { AntiAlias = true, Color = Android.Graphics.Color.Black, TextAlign = Paint.Align.Center, UnderlineText = false }; _paint.SetStyle(Paint.Style.Fill); if (fontFacePath != null) { _paint.SetTypeface(Typeface.CreateFromAsset(context.Assets, $"{fontFacePath}.ttf")); } }
/// <summary> /// Initializes the specified context. /// </summary> /// <param name="context">The context.</param> /// <param name="icon">The icon.</param> /// <exception cref="Java.Lang.IllegalStateException">Unable to find the module associated + /// with icon + icon.Key + , have you registered the module + /// you are trying to use with Iconize.With(...) in your Application?</exception> private void Init(Context context, IIcon icon) { _context = context; _icon = icon; _paint = new TextPaint(); var module = Iconize.FindModuleOf(icon); if (module == null) { throw new Java.Lang.IllegalStateException("Unable to find the module associated " + "with icon " + icon.Key + ", have you registered the module " + "you are trying to use with Iconize.With(...) in your Application?"); } _paint.SetTypeface(module.ToTypeface(context)); _paint.SetStyle(Paint.Style.Fill); _paint.TextAlign = Paint.Align.Center; _paint.UnderlineText = false; _paint.Color = Android.Graphics.Color.Black; _paint.AntiAlias = true; }
public SizeF GetStringSize(string value, string fontName, float fontSize) { if (value == null) { return(new SizeF()); } var textPaint = new TextPaint { TextSize = fontSize }; textPaint.SetTypeface(NativeFontService.Instance.GetTypeface(fontName)); var staticLayout = TextLayoutUtils.CreateLayout(value, textPaint, null, Layout.Alignment.AlignNormal); var size = staticLayout.GetTextSizeAsSizeF(false); staticLayout.Dispose(); return(size); }
public MainRecyclerViewTouchCallback(Context context, MainRecyclerView recyclerView) : base(0, ItemTouchHelper.Left | ItemTouchHelper.Right) { this.recyclerView = recyclerView; backgrounds = new[] { Resource.Color.playButtonRed, Resource.Color.playButtonGreen } .Select(res => new Color(ContextCompat.GetColor(context, res))) .Select(color => new ColorDrawable(color)) .ToArray(); textMargin = 16.DpToPixels(context); textPaint.Color = Color.White; textPaint.TextAlign = Paint.Align.Left; textPaint.TextSize = 15.SpToPixels(context); textPaint.SetTypeface(Typeface.Create("sans-serif-medium", TypefaceStyle.Normal)); deleteTextWidth = (int)textPaint.MeasureText(Delete); }
/// <summary> /// Create an <see cref="IconDrawable" />. /// </summary> /// <param name="context">Your activity or application context.</param> /// <param name="iconKey">The icon key you want this drawable to display.</param> /// <exception cref="ArgumentException">If the key doesn't match any icon.</exception> public IconDrawable(Context context, String iconKey) { if (FontAwesomeRegular.Items.TryGetValue(iconKey, out char iconChar)) { return; } _iconChar = iconChar; _context = context; _paint = new TextPaint { AntiAlias = true, TextAlign = Paint.Align.Center, UnderlineText = false }; _paint.SetStyle(Paint.Style.Fill); _paint.SetTypeface(GetTypeface(context)); }
private void Init(Context context, IIcon icon) { _context = context; _icon = icon; _paint = new TextPaint(); var module = Iconize.FindModuleOf(icon); if (module == null) { throw new Java.Lang.IllegalStateException("Unable to find the module associated " + "with icon " + icon.Key + ", have you registered the module " + "you are trying to use with Iconize.With(...) in your Application?"); } _paint.SetTypeface(module.GetTypeface(context)); _paint.SetStyle(Paint.Style.Fill); _paint.TextAlign = Paint.Align.Center; _paint.UnderlineText = false; _paint.Color = global::Android.Graphics.Color.Black; _paint.AntiAlias = true; }
private void Init(Context context, IIcon icon) { this.context = context; this.icon = icon; paint = new TextPaint(); IconFontDescriptorWrapper descriptor = Iconify.FindTypefaceOf(icon); if (descriptor == null) { throw new IllegalStateException("Unable to find the module associated " + "with icon " + icon.Key + ", have you registered the module " + "you are trying to use with Iconify.with(...) in your Application?"); } paint.SetTypeface(descriptor.GetTypeface(context)); paint.SetStyle(Paint.Style.Fill); paint.TextAlign = Paint.Align.Center; paint.UnderlineText = false; paint.Color = new Android.Graphics.Color(0, 0, 0); //paint.Color = Color.Black; paint.AntiAlias = true; }
private static NativeCanvasState CreateNewState(object context) { if (_defaultFillPaint == null) { _defaultFillPaint = new global::Android.Graphics.Paint(); _defaultFillPaint.SetARGB(255, 255, 255, 255); _defaultFillPaint.SetStyle(global::Android.Graphics.Paint.Style.Fill); _defaultFillPaint.AntiAlias = true; _defaultStrokePaint = new global::Android.Graphics.Paint(); _defaultStrokePaint.SetARGB(255, 0, 0, 0); _defaultStrokePaint.StrokeWidth = 1; _defaultStrokePaint.StrokeMiter = CanvasDefaults.DefaultMiterLimit; _defaultStrokePaint.SetStyle(global::Android.Graphics.Paint.Style.Stroke); _defaultStrokePaint.AntiAlias = true; _defaultFontPaint = new TextPaint(); _defaultFontPaint.SetARGB(255, 0, 0, 0); _defaultFontPaint.AntiAlias = true; var arial = NativeFontService.Instance.GetTypeface("Arial"); if (arial != null) { _defaultFontPaint.SetTypeface(arial); } else { Logger.Warn("Unable to set the default font paint to Arial"); } } var state = new NativeCanvasState { FillPaint = new global::Android.Graphics.Paint(_defaultFillPaint), StrokePaint = new global::Android.Graphics.Paint(_defaultStrokePaint), FontPaint = new TextPaint(_defaultFontPaint), FontName = NativeFontService.SystemFont }; return(state); }
TextPaint GetFontPaint(Font font, TextAlignment alignment) { var paint = new TextPaint(PaintFlags.AntiAlias); paint.TextAlign = Paint.Align.Left; if (alignment == TextAlignment.Center) { paint.TextAlign = Paint.Align.Center; } else if (alignment == TextAlignment.Right) { paint.TextAlign = Paint.Align.Right; } paint.TextSize = (float)font.Size; var typeface = Typeface.Create(font.Family, TypefaceStyle.Normal); paint.SetTypeface(typeface); return(paint); }
private void Init(Context context, IIcon icon) { this.context = context; this.icon = icon; paint = new TextPaint(); IconFontDescriptorWrapper descriptor = Iconify.FindTypefaceOf(icon); if (descriptor == null) { throw new IllegalStateException("Unable to find the module associated " + "with icon " + icon.Key + ", have you registered the module " + "you are trying to use with Iconify.with(...) in your Application?"); } paint.SetTypeface(descriptor.GetTypeface(context)); paint.SetStyle(Paint.Style.Fill); paint.TextAlign = Paint.Align.Center; paint.UnderlineText = false; paint.Color = new Android.Graphics.Color(0, 0, 0); //paint.Color = Color.Black; paint.AntiAlias = true; }
public static TextPaint GlobalGetFontPaint(Font font, TextAlignment alignment) { var paint = new TextPaint(PaintFlags.AntiAlias); paint.TextAlign = Paint.Align.Left; if (alignment == TextAlignment.Center) { paint.TextAlign = Paint.Align.Center; } else if (alignment == TextAlignment.Right) { paint.TextAlign = Paint.Align.Right; } var density = global::Android.App.Application.Context.Resources.DisplayMetrics.Density; paint.TextSize = (float)font.Size * density; var typeface = Typeface.Create(font.Family, TypefaceStyle.Normal); paint.SetTypeface(typeface); return(paint); }
void Initialize () { Clickable = true; Click += HandleClick; textSize = TypedValue.ApplyDimension (ComplexUnitType.Sp, 14, Resources.DisplayMetrics); spacing = TypedValue.ApplyDimension (ComplexUnitType.Dip, 24, Resources.DisplayMetrics); knobWidth = TypedValue.ApplyDimension (ComplexUnitType.Dip, 6, Resources.DisplayMetrics); knobPadding = TypedValue.ApplyDimension (ComplexUnitType.Dip, 2, Resources.DisplayMetrics); cornerRadius = TypedValue.ApplyDimension (ComplexUnitType.Dip, 2, Resources.DisplayMetrics); normalColor = Resources.GetColor (Resource.Color.dimmed_text_color); selectedColor = Resources.GetColor (Resource.Color.highlight_text_color); bgColor = Resources.GetColor (Resource.Color.primary_background); textPaint = new TextPaint { AntiAlias = true }; textPaint.SetTypeface (Typeface.Create ("sans-serif-condensed", TypefaceStyle.Normal)); textPaint.TextSize = textSize; textPaint.TextAlign = Paint.Align.Center; textPaint.Color = normalColor; CreateTextLayouts (); }
public override void Draw(Canvas canvas) { base.Draw(canvas); TextPaint paint = new TextPaint(); paint.AntiAlias = true; paint.SetTypeface(Control.Typeface); paint.TextSize = myFontSize * scaledDensity; paint.Color = Android.Graphics.Color.Red; Paint.FontMetrics metrics = paint.GetFontMetrics(); Rect bounds = new Rect(); paint.GetTextBounds(Element.Text, 0, Element.Text.Length, bounds); canvas.DrawText( Element.Text, 0.5f * MeasuredWidth - bounds.Left - 0.5f * bounds.Width(), 0.5f * MeasuredHeight - metrics.Ascent - 0.5f * (metrics.Bottom - metrics.Top), paint ); }
public override Carto.Graphics.Bitmap OnDrawPopup(PopupDrawInfo popupDrawInfo) { PopupStyle style = popupDrawInfo.Popup.Style; // Calculate scaled dimensions float DPToPX = popupDrawInfo.DPToPX; float PXTODP = 1 / DPToPX; if (style.ScaleWithDPI) { DPToPX = 1; } else { PXTODP = 1; } float screenWidth = popupDrawInfo.ScreenBounds.GetWidth() * PXTODP; float screenHeight = popupDrawInfo.ScreenBounds.GetHeight() * PXTODP; // Update sizes based on scale (uses extension method, cf. Shared/Extensions int fontSize = FontSize.Update(DPToPX); int triangleWidth = TriangleSize.Update(DPToPX); int triangleHeight = TriangleSize.Update(DPToPX); int strokeWidth = StrokeWidth.Update(DPToPX); int screenPadding = ScreenPadding.Update(DPToPX); // Set font var font = Android.Graphics.Typeface.Create("HelveticaNeue-Light", Android.Graphics.TypefaceStyle.Normal); // Calculate the maximum popup size, adjust with dpi int maxPopupWidth = (int)(Math.Min(screenWidth, screenHeight)); float halfStrokeWidth = strokeWidth * 0.5f; int maxTextWidth = maxPopupWidth - (2 * screenPadding + strokeWidth); // Measure text TextPaint textPaint = new TextPaint { Color = TextColor, TextSize = fontSize }; textPaint.SetTypeface(font); var textLayout = new StaticLayout(text, textPaint, maxTextWidth, Layout.Alignment.AlignNormal, 1, 0, false); int textX = (int)Math.Min(textPaint.MeasureText(text), textLayout.Width); int textY = textLayout.Height; int popupWidth = textX + (2 * PopupPadding + strokeWidth + triangleWidth); int popupHeight = textY + (2 * PopupPadding + strokeWidth); var bitmap = Android.Graphics.Bitmap.CreateBitmap(popupWidth, popupHeight, Android.Graphics.Bitmap.Config.Argb8888); var canvas = new Android.Graphics.Canvas(bitmap); var trianglePath = new Android.Graphics.Path(); trianglePath.MoveTo(triangleWidth, 0); trianglePath.LineTo(halfStrokeWidth, triangleHeight * 0.5f); trianglePath.LineTo(triangleWidth, triangleHeight); trianglePath.Close(); int triangleOffsetX = 0; int triangleOffsetY = (popupHeight - triangleHeight) / 2; // Create paint object var paint = new Android.Graphics.Paint(); paint.AntiAlias = true; paint.SetStyle(Android.Graphics.Paint.Style.Stroke); paint.StrokeWidth = strokeWidth; paint.Color = StrokeColor; // Stroke background var background = new Android.Graphics.RectF(); background.Left = triangleWidth; background.Top = halfStrokeWidth; background.Right = popupWidth - strokeWidth; background.Bottom = popupHeight - strokeWidth; canvas.DrawRect(background, paint); // Stroke triangle canvas.Save(); canvas.Translate(triangleOffsetX, triangleOffsetY); canvas.DrawPath(trianglePath, paint); canvas.Restore(); // Fill background paint.SetStyle(Android.Graphics.Paint.Style.Fill); paint.Color = BackgroundColor; canvas.DrawRect(background, paint); // Fill triangle canvas.Save(); canvas.Translate(triangleOffsetX, triangleOffsetY); canvas.DrawPath(trianglePath, paint); canvas.Restore(); if (textLayout != null) { // Draw text canvas.Save(); canvas.Translate(halfStrokeWidth + triangleWidth + PopupPadding, halfStrokeWidth + PopupPadding); textLayout.Draw(canvas); canvas.Restore(); } return(BitmapUtils.CreateBitmapFromAndroidBitmap(bitmap)); }
public override void UpdateMeasureState(TextPaint tp) { tp.SetTypeface(mTypeface); tp.Flags = tp.Flags | PaintFlags.SubpixelText; }
public CompassView(Context context, Android.Util.IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle) { _paint = new Paint(); _paint.SetStyle(Paint.Style.Fill); _paint.AntiAlias = true; _paint.TextSize = (float)DIRECTION_TEXT_HEIGHT; _paint.SetTypeface(Typeface.Create("sans-serif-thin", TypefaceStyle.Normal)); _tickPaint = new Paint(); _tickPaint.SetStyle(Paint.Style.Stroke); _tickPaint.StrokeWidth = (float)TICK_WIDTH; _tickPaint.AntiAlias = true; _tickPaint.Color = Color.White; _placePaint = new TextPaint(); _placePaint.SetStyle(Paint.Style.Fill); _placePaint.AntiAlias = true; _placePaint.Color = Color.White; _placePaint.TextSize = (float)PLACE_TEXT_HEIGHT; _placePaint.SetTypeface(Typeface.Create("sans-serif-light", TypefaceStyle.Normal)); _path = new Path(); _textBounds = new Rect(); _allBounds = new List<Rect>(); _distanceFormat = NumberFormat.GetNumberInstance(Locale.Default); _distanceFormat.MinimumFractionDigits = 0; _distanceFormat.MaximumFractionDigits = 1; _placeBitmap = BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.place_mark); _animatedHeading = Double.NaN; _directions = context.Resources.GetStringArray(Resource.Array.direction_abbreviations); _animator = new ValueAnimator(); setupAnimator(); }
public override Carto.Graphics.Bitmap OnDrawPopup(PopupDrawInfo popupDrawInfo) { PopupStyle style = popupDrawInfo.Popup.Style; // Calculate scaled dimensions float DPToPX = popupDrawInfo.DPToPX; float PXTODP = 1 / DPToPX; if (style.ScaleWithDPI) { DPToPX = 1; } else { PXTODP = 1; } float screenWidth = popupDrawInfo.ScreenBounds.GetWidth() * PXTODP; float screenHeight = popupDrawInfo.ScreenBounds.GetHeight() * PXTODP; // Update sizes based on scale (uses extension method, cf. Shared/Extensions int fontSize = FontSize.Update(DPToPX); int triangleWidth = TriangleSize.Update(DPToPX); int triangleHeight = TriangleSize.Update(DPToPX); int strokeWidth = StrokeWidth.Update(DPToPX); int screenPadding = ScreenPadding.Update(DPToPX); // Set font var font = Android.Graphics.Typeface.Create("HelveticaNeue-Light", Android.Graphics.TypefaceStyle.Normal); // Calculate the maximum popup size, adjust with dpi int maxPopupWidth = (int)(Math.Min(screenWidth, screenHeight)); float halfStrokeWidth = strokeWidth * 0.5f; int maxTextWidth = maxPopupWidth - (2 * screenPadding + strokeWidth); // Measure text TextPaint textPaint = new TextPaint { Color = TextColor, TextSize = fontSize }; textPaint.SetTypeface(font); var textLayout = new StaticLayout(text, textPaint, maxTextWidth, Layout.Alignment.AlignNormal, 1, 0, false); int textX = (int)Math.Min(textPaint.MeasureText(text), textLayout.Width); int textY = textLayout.Height; int popupWidth = textX + (2 * PopupPadding + strokeWidth + triangleWidth); int popupHeight = textY + (2 * PopupPadding + strokeWidth); var bitmap = Android.Graphics.Bitmap.CreateBitmap(popupWidth, popupHeight, Android.Graphics.Bitmap.Config.Argb8888); var canvas = new Android.Graphics.Canvas(bitmap); var trianglePath = new Android.Graphics.Path(); trianglePath.MoveTo(triangleWidth, 0); trianglePath.LineTo(halfStrokeWidth, triangleHeight * 0.5f); trianglePath.LineTo(triangleWidth, triangleHeight); trianglePath.Close(); int triangleOffsetX = 0; int triangleOffsetY = (popupHeight - triangleHeight) / 2; // Create paint object var paint = new Android.Graphics.Paint(); paint.AntiAlias = true; paint.SetStyle(Android.Graphics.Paint.Style.Stroke); paint.StrokeWidth = strokeWidth; paint.Color = StrokeColor; // Stroke background var background = new Android.Graphics.RectF(); background.Left = triangleWidth; background.Top = halfStrokeWidth; background.Right = popupWidth - strokeWidth; background.Bottom = popupHeight - strokeWidth; canvas.DrawRect(background, paint); // Stroke triangle canvas.Save(); canvas.Translate(triangleOffsetX, triangleOffsetY); canvas.DrawPath(trianglePath, paint); canvas.Restore(); // Fill background paint.SetStyle(Android.Graphics.Paint.Style.Fill); paint.Color = BackgroundColor; canvas.DrawRect(background, paint); // Fill triangle canvas.Save(); canvas.Translate(triangleOffsetX, triangleOffsetY); canvas.DrawPath(trianglePath, paint); canvas.Restore(); if (textLayout != null) { // Draw text canvas.Save(); canvas.Translate(halfStrokeWidth + triangleWidth + PopupPadding, halfStrokeWidth + PopupPadding); textLayout.Draw(canvas); canvas.Restore(); } return BitmapUtils.CreateBitmapFromAndroidBitmap(bitmap); }
public override void UpdateDrawState(TextPaint tp) { tp.SetTypeface(mTypeface); }
void Initialize() { Clickable = true; Click += HandleClick; textSize = TypedValue.ApplyDimension (ComplexUnitType.Sp, 14, Resources.DisplayMetrics); spacing = TypedValue.ApplyDimension (ComplexUnitType.Dip, 24, Resources.DisplayMetrics); knobWidth = TypedValue.ApplyDimension (ComplexUnitType.Dip, 6, Resources.DisplayMetrics); knobPadding = TypedValue.ApplyDimension (ComplexUnitType.Dip, 2, Resources.DisplayMetrics); cornerRadius = TypedValue.ApplyDimension (ComplexUnitType.Dip, 2, Resources.DisplayMetrics); normalColor = Resources.GetColor (Resource.Color.dimmed_text_color); selectedColor = Resources.GetColor (Resource.Color.highlight_text_color); bgColor = Resources.GetColor (Resource.Color.primary_background); textPaint = new TextPaint { AntiAlias = true }; textPaint.SetTypeface (Typeface.Create ("sans-serif-condensed", TypefaceStyle.Normal)); textPaint.TextSize = textSize; textPaint.TextAlign = Paint.Align.Center; textPaint.Color = normalColor; CreateTextLayouts (); }
internal CCTexture2D CreateTextSprite(string text, CCFontDefinition textDefinition) { if (string.IsNullOrEmpty(text)) return new CCTexture2D(); int imageWidth; int imageHeight; var textDef = textDefinition; var contentScaleFactorWidth = CCLabel.DefaultTexelToContentSizeRatios.Width; var contentScaleFactorHeight = CCLabel.DefaultTexelToContentSizeRatios.Height; textDef.FontSize *= contentScaleFactorWidth; textDef.Dimensions.Width *= contentScaleFactorWidth; textDef.Dimensions.Height *= contentScaleFactorHeight; bool hasPremultipliedAlpha; var display = Game.Activity.WindowManager.DefaultDisplay; var metrics = new DisplayMetrics(); display.GetMetrics(metrics); // Do not take into account ScaleDensity for now. // var fontScaleFactor = metrics.ScaledDensity; // textDef.FontSize = (int)(textDef.FontSize * fontScaleFactor); // out paint object to hold our drawn text // var paintFlags = new PaintFlags(); // if (textDefinition.isShouldAntialias) // paintFlags = PaintFlags.AntiAlias | PaintFlags.SubpixelText; var textPaint = new TextPaint(); textPaint.Color = Android.Graphics.Color.White; textPaint.TextAlign = Paint.Align.Left; textPaint.AntiAlias = textDefinition.isShouldAntialias; textPaint.TextSize = textDef.FontSize; var fontName = textDef.FontName; var ext = System.IO.Path.GetExtension(fontName); if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf") { CCContentManager.SharedContentManager.GetAssetStreamAsBytes(fontName, out fontName); var activity = Game.Activity; try { var typeface = Typeface.CreateFromAsset(activity.Assets, fontName); textPaint.SetTypeface(typeface); } catch (Exception) { textPaint.SetTypeface(Typeface.Create(fontName, TypefaceStyle.Normal)); } } else { textPaint.SetTypeface(Typeface.Create(fontName, TypefaceStyle.Normal)); } // color var foregroundColor = Android.Graphics.Color.White; textPaint.Color = foregroundColor; // alignment var horizontalAlignment = textDef.Alignment; var verticleAlignement = textDef.LineAlignment; var textAlign = (CCTextAlignment.Right == horizontalAlignment) ? Layout.Alignment.AlignOpposite : (CCTextAlignment.Center == horizontalAlignment) ? Layout.Alignment.AlignCenter : Layout.Alignment.AlignNormal; // LineBreak // TODO: Find a way to specify the type of line breaking if possible. var dimensions = new CCSize(textDef.Dimensions.Width, textDef.Dimensions.Height); var layoutAvailable = true; if (dimensions.Width <= 0) { dimensions.Width = 8388608; layoutAvailable = false; } if (dimensions.Height <= 0) { dimensions.Height = 8388608; layoutAvailable = false; } // Get bounding rectangle - we need its attribute and method values var layout = new StaticLayout(text, textPaint, (int)dimensions.Width, textAlign, 1.0f, 0.0f, false); var boundingRect = new Rect(); var lineBounds = new Rect(); // Loop through all the lines so we can find our drawing offsets var lineCount = layout.LineCount; // early out if something went wrong somewhere and nothing is to be drawn if (lineCount == 0) return new CCTexture2D(); for (int lc = 0; lc < lineCount; lc++) { layout.GetLineBounds(lc, lineBounds); var max = (int)Math.Ceiling(layout.GetLineMax(lc)); if (boundingRect.Right < max) boundingRect.Right = max; boundingRect.Bottom = lineBounds.Bottom; } if (!layoutAvailable) { if (dimensions.Width == 8388608) { dimensions.Width = boundingRect.Right; } if (dimensions.Height == 8388608) { dimensions.Height = boundingRect.Bottom; } } imageWidth = (int)dimensions.Width; imageHeight = (int)dimensions.Height; // Recreate our layout based on calculated dimensions so that we can draw the text correctly // in our image when Alignment is not Left. if (textAlign != Layout.Alignment.AlignNormal) { layout = new StaticLayout(text, textPaint, (int)dimensions.Width, textAlign, 1.0f, 0.0f, false); } // Line alignment var yOffset = (CCVerticalTextAlignment.Bottom == verticleAlignement || boundingRect.Bottom >= dimensions.Height) ? dimensions.Height - boundingRect.Bottom // align to bottom : (CCVerticalTextAlignment.Top == verticleAlignement) ? 0 // align to top : (imageHeight - boundingRect.Bottom) * 0.5f; // align to center try { // Create our platform dependant image to be drawn to. using (Bitmap textureBitmap = Bitmap.CreateBitmap(imageWidth, imageHeight, Bitmap.Config.Argb8888)) { using (Canvas drawingCanvas = new Canvas(textureBitmap)) { drawingCanvas.DrawARGB(0, 255, 255, 255); // Set our vertical alignment drawingCanvas.Translate(0, yOffset); // Now draw the text using our layout layout.Draw(drawingCanvas); // Create a pixel array int[] pixels = new int[imageWidth * imageHeight]; // Now lets get our pixels. // We use CopyPixelsToBuffer so that it is in Premultiplied Alpha already. // Using Bitmap.GetPixels return non Premultiplied Alpha which causes blending problems Java.Nio.IntBuffer byteBuffer = Java.Nio.IntBuffer.Allocate(imageWidth * imageHeight); textureBitmap.CopyPixelsToBuffer(byteBuffer); if (byteBuffer.HasArray) { byteBuffer.Rewind(); byteBuffer.Get(pixels, 0, pixels.Length); } // Make sure we recycle - Let's keep it green textureBitmap.Recycle(); // Here we create our Texture and then set our pixel data. var texture = new CCTexture2D(imageWidth, imageHeight); texture.XNATexture.SetData<int>(pixels); return texture; } } } catch (Exception exc) { CCLog.Log ("CCLabel Android: Error creating native label:{0}\n{1}", exc.Message, exc.StackTrace); return new CCTexture2D(); } }
TextPaint GetFontPaint(Font font, TextAlignment alignment) { var paint = new TextPaint (PaintFlags.AntiAlias); paint.TextAlign = Paint.Align.Left; if (alignment == TextAlignment.Center) paint.TextAlign = Paint.Align.Left; else if (alignment == TextAlignment.Right) paint.TextAlign = Paint.Align.Right; // paint.TextSize = (float)font.Size; // var typeface = Typeface.Create (font.Family, TypefaceStyle.Normal); // paint.SetTypeface (typeface); var typeface = Typeface.Default; paint.SetTypeface (typeface); float androidFontSize = AndroidPlatform.SizeToAndroid ((float)font.Size); paint.TextSize = androidFontSize; return paint; }
public override void UpdateDrawState (TextPaint ds) { base.UpdateDrawState (ds); ds.UnderlineText = false; ds.SetTypeface (Android.Graphics.Typeface.DefaultBold); }
public override void UpdateDrawState(TextPaint tp) { tp.SetTypeface(typeFace); tp.Flags = tp.Flags | PaintFlags.SubpixelText; }
public override void UpdateMeasureState(TextPaint p) { p.SetTypeface(typeFace); p.Flags = p.Flags | PaintFlags.SubpixelText; }
private void Init(Context context, Icon icon) { _context = context; _icon = icon; _paint = new TextPaint(); _paint.SetTypeface(Iconify.FindTypefaceOf(icon).GetTypeface(context)); _paint.SetStyle(Paint.Style.Fill); _paint.TextAlign = Paint.Align.Center; _paint.UnderlineText = false; _paint.Color = Color.Black; _paint.AntiAlias = true; }