示例#1
0
 internal virtual void Invalidate()
 {
     charWidths.Clear();
     Paint.FontMetrics fm = textPaint.GetFontMetrics();
     charHeight   = fm.Bottom - fm.Top;
     charBaseline = -fm.Top;
 }
示例#2
0
        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 Paint {
                AntiAlias = true
            };
            textPaint.SetTypeface(Typeface.Create("sans-serif-condensed", TypefaceStyle.Normal));
            textPaint.TextSize  = textSize;
            textPaint.TextAlign = Paint.Align.Center;
            textPaint.Color     = normalColor;

            fontMetrics   = textPaint.GetFontMetrics();
            letterSpacing = fontMetrics.Bottom;
            ComputeCharacterSizes();
        }
示例#3
0
        public override FontMetrics GetFontMetrics(Font font)
        {
            Paint paint = font.RendererData as Paint;

            if (paint == null || Math.Abs(font.RealSize - font.Size * Scale) > 2)
            {
                FreeFont(font);
                LoadFont(font);
                paint = font.RendererData as Paint;
            }

            Paint.FontMetrics metrics = paint.GetFontMetrics();

            FontMetrics fm = new FontMetrics
                             (
                -metrics.Top - metrics.Leading,
                -metrics.Ascent,
                metrics.Descent,
                -metrics.Top + metrics.Bottom,
                metrics.Leading,
                paint.FontSpacing,
                0
                             );

            return(fm);
        }
示例#4
0
 public void Text(string text)
 {
     if (mBitmap != null)
     {
         int width  = mBitmap.Width;
         int height = mBitmap.Height;
         mPaint.Color = new Color(COLORS[mColorIndex]);
         mPaint.Alpha = 255;
         int size = height;
         mPaint.TextSize = size;
         var bounds = new Rect();
         mPaint.GetTextBounds(text, 0, text.Length, bounds);
         int twidth = bounds.Width();
         twidth += (twidth / 4);
         if (twidth > width)
         {
             size            = (size * width) / twidth;
             mPaint.TextSize = size;
             mPaint.GetTextBounds(text, 0, text.Length, bounds);
         }
         Paint.FontMetrics fm = mPaint.GetFontMetrics();
         mCanvas.DrawText(text, (width - bounds.Width()) / 2,
                          ((height - size) / 2) - fm.Ascent, mPaint);
         mFadeSteps = 0;
         Invalidate();
     }
 }
示例#5
0
        public void DrawText(string text, float x, float y, float width, float height, Xamarin.Forms.TextAlignment hAlignment, Xamarin.Forms.TextAlignment vAlignment)
        {
            Paint.FontMetrics fm = paintFill.GetFontMetrics();

            float textWidth  = paintFill.MeasureText(text);
            float textHeight = fm.Descent - fm.Ascent;

            if ((hAlignment & Xamarin.Forms.TextAlignment.Center) != 0)
            {
                x += (width - textWidth) / 2;
            }
            else if ((hAlignment & Xamarin.Forms.TextAlignment.End) != 0)
            {
                x += width - textWidth;
            }

            if ((vAlignment & Xamarin.Forms.TextAlignment.Center) != 0)
            {
                y += (height - textHeight) / 2;
            }
            else if ((vAlignment & Xamarin.Forms.TextAlignment.End) != 0)
            {
                y += height - textHeight;
            }

            canvas.DrawText(text, x, y - fm.Ascent, paintFill);
        }
示例#6
0
 public PolyToPoly(Context context)
     : base(context)
 {
     // for when the style is STROKE
     mPaint.setStrokeWidth(4);
     // for when we draw text
     mPaint.setTextSize(40);
     mPaint.setTextAlign(Paint.Align.CENTER);
     mFontMetrics = mPaint.getFontMetrics();
 }
示例#7
0
 public void SetFont(Xamarin.Forms.Font f)
 {
     if (!equal(f, _font_xf))
     {
         _font_xf = f;
         _paints.Text.SetTypeface(f.ToTypeface());
         _paints.Text.TextSize = (float)f.FontSize;
         TextFontMetrics       = _paints.Text.GetFontMetrics();
     }
 }
示例#8
0
 public void SetFont(Font f)
 {
     if (f != _lastFont)
     {
         _lastFont = f;
         //_paints.Text.SetTypeface (f.ToTypeface());
         _paints.Text.SetTypeface(Typeface.Default);                  // TODO temporary hack
         _paints.Text.TextSize = (float)f.Size;
         TextFontMetrics       = _paints.Text.GetFontMetrics();
     }
 }
示例#9
0
        public ColorFilters(Context context)
            : base(context)
        {
            mDrawable = getResources().getDrawable(R.drawable.btn_default_normal);
            mDrawable.setBounds(0, 0, 150, 48);
            mDrawable.setDither(true);

            int[] resIDs = new int[] {
                R.drawable.btn_circle_normal,
                R.drawable.btn_check_off,
                R.drawable.btn_check_on
            };
            mDrawables = new Drawable[resIDs.Length];
            Drawable prev = mDrawable;

            for (int i = 0; i < resIDs.Length; i++)
            {
                mDrawables [i] = getResources().getDrawable(resIDs [i]);
                mDrawables [i].setDither(true);
                addToTheRight(mDrawables [i], prev);
                prev = mDrawables [i];
            }

            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setTextSize(16);
            mPaint.setTextAlign(Paint.Align.CENTER);

            mPaint2 = new Paint(mPaint);
            mPaint2.setAlpha(64);

            Paint.FontMetrics fm = mPaint.getFontMetrics();
            mPaintTextOffset = (fm.descent + fm.ascent) * 0.5f;

            mColors = new uint[] {
                0, 0xCC0000FF, 0x880000FF, 0x440000FF, 0xFFCCCCFF,
                0xFF8888FF, 0xFF4444FF
            };

            mModes = new PorterDuff.Mode[] {
                PorterDuff.Mode.SRC_ATOP, PorterDuff.Mode.MULTIPLY,
            };
            mModeIndex = 0;

            updateTitle();
        }
示例#10
0
        static void drawIntoBitmap(Bitmap bm)
        {
            float  x = bm.getWidth();
            float  y = bm.getHeight();
            Canvas c = new Canvas(bm);
            Paint  p = new Paint();

            p.setAntiAlias(true);

            p.setAlpha(0x80);
            c.drawCircle(x / 2, y / 2, x / 2, p);

            p.setAlpha(0x30);
            p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
            p.setTextSize(60);
            p.setTextAlign(Paint.Align.CENTER);
            Paint.FontMetrics fm = p.getFontMetrics();
            c.drawText("Alpha", x / 2, (y - fm.ascent) / 2, p);
        }
示例#11
0
            private void DrawIntoBitmap(Bitmap bm)
            {
                float  x = bm.Width;
                float  y = bm.Height;
                Canvas c = new Canvas(bm);
                Paint  p = new Paint();

                p.AntiAlias = true;

                p.Alpha = 0x80;
                c.DrawCircle(x / 2, y / 2, x / 2, p);

                p.Alpha = 0x30;
                p.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.Src));
                p.TextSize  = 60;
                p.TextAlign = Paint.Align.Center;
                Paint.FontMetrics fm = p.GetFontMetrics();
                c.DrawText("Alpha", x / 2, (y - fm.Ascent) / 2, p);
            }
示例#12
0
        private void CreateFont(string fontName, float fontSize, CCRawList <char> charset)
        {
            if (_paint == null)
            {
                var contex  = (Activity)CCApplication.SharedApplication.Game.Window.Context;
                var display = contex.WindowManager.DefaultDisplay;
                var metrics = new DisplayMetrics();
                display.GetMetrics(metrics);

                _fontScaleFactor = metrics.ScaledDensity;

                _paint           = new Paint(PaintFlags.AntiAlias);
                _paint.Color     = Android.Graphics.Color.White;
                _paint.TextAlign = Paint.Align.Left;
                // _paint.LinearText = true;
            }

            _paint.TextSize = fontSize;

            var ext = System.IO.Path.GetExtension(fontName);

            if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf")
            {
                var path     = System.IO.Path.Combine(CCApplication.SharedApplication.Game.Content.RootDirectory, fontName);
                var activity = (Activity)CCApplication.SharedApplication.Game.Window.Context;

                try
                {
                    var typeface = Typeface.CreateFromAsset(activity.Assets, path);
                    _paint.SetTypeface(typeface);
                }
                catch (Exception)
                {
                    _paint.SetTypeface(Typeface.Create(fontName, TypefaceStyle.Normal));
                }
            }
            else
            {
                _paint.SetTypeface(Typeface.Create(fontName, TypefaceStyle.Normal));
            }

            _fontMetrix = _paint.GetFontMetrics();
        }
示例#13
0
        /// <summary>Define paints.</summary>
        private void InitPaint()
        {
            // Round rectangle paint
            _strokePaint = new Paint
            {
                Color       = _selectedColor.ToColor(),
                AntiAlias   = true,
                StrokeWidth = _strokeWidth
            };
            _strokePaint.SetStyle(Paint.Style.Stroke);


            // Selected paint
            _fillPaint = new Paint
            {
                Color = _selectedColor.ToColor()
            };
            _strokePaint.AntiAlias = true;
            _fillPaint.SetStyle(Paint.Style.FillAndStroke);


            // Selected text paint
            _selectedTextPaint = new TextPaint(PaintFlags.AntiAlias)
            {
                TextSize = _textSize,
                Color    = unchecked ((int)0xffffffff).ToColor()
            };
            _strokePaint.AntiAlias = true;

            // Unselected text paint
            _unselectedTextPaint = new TextPaint(PaintFlags.AntiAlias)
            {
                TextSize = _textSize,
                Color    = _selectedColor.ToColor()
            };

            _strokePaint.AntiAlias = true;
            _textHeightOffset      = -(_selectedTextPaint.Ascent() + _selectedTextPaint.Descent()) * 0.5f;
            _fontMetrics           = _selectedTextPaint.GetFontMetrics();
        }
        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
                );
        }
示例#15
0
 public AndroidFontMetrics(Paint.FontMetrics metrics)
 {
     LineHeight = metrics.Descent - metrics.Ascent;
     Descent    = metrics.Descent;
 }
示例#16
0
        public Xamarin.Forms.Size MeasureText(string text)
        {
            Paint.FontMetrics fm = paintFill.GetFontMetrics();

            return(new Xamarin.Forms.Size(paintFill.MeasureText(text), fm.Bottom - fm.Ascent));
        }