static void UpdateLayout(TextView textview, Element element, Android.Views.View v) { if (string.IsNullOrEmpty(textview.Text)) { return; } var margin = AddText.GetMargin(element); margin.Left = (int)v.Context.ToPixels(margin.Left); margin.Top = (int)v.Context.ToPixels(margin.Top); margin.Right = (int)v.Context.ToPixels(margin.Right); margin.Bottom = (int)v.Context.ToPixels(margin.Bottom); var textpaint = textview.Paint; var rect = new Android.Graphics.Rect(); textpaint.GetTextBounds(textview.Text, 0, textview.Text.Length, rect); var xPos = 0; if (AddText.GetHorizontalAlign(element) == Xamarin.Forms.TextAlignment.End) { xPos = v.Width - rect.Width() - textview.PaddingLeft - textview.PaddingRight - (int)margin.Right - 4; if (xPos < (int)margin.Left) { xPos = (int)margin.Left; } textview.Right = v.Width - (int)margin.Right; } else { xPos = (int)margin.Left; textview.Right = (int)margin.Left + rect.Width() + textview.PaddingLeft + textview.PaddingRight + 4; if (textview.Right >= v.Width) { textview.Right = v.Width - (int)margin.Right; } } textview.Left = xPos; var fm = textpaint.GetFontMetrics(); var height = (int)(Math.Abs(fm.Top) + fm.Bottom + textview.PaddingTop + textview.PaddingEnd); var yPos = AddText.GetVerticalAlign(element) == Xamarin.Forms.TextAlignment.Start ? 0 + (int)margin.Top : v.Height - height - (int)margin.Bottom; textview.Top = yPos; textview.Bottom = yPos + height; }
// In OnLayoutChange, decide size and position of child element. // For some reason, in layout that was added to container, it does not work all gravity options and all layout options. public void OnLayoutChange(Android.Views.View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { if (string.IsNullOrEmpty(_textview.Text)) { return; } var margin = AddText.GetMargin(_element); margin.Left = (int)Forms.Context.ToPixels(margin.Left); margin.Top = (int)Forms.Context.ToPixels(margin.Top); margin.Right = (int)Forms.Context.ToPixels(margin.Right); margin.Bottom = (int)Forms.Context.ToPixels(margin.Bottom); var textpaint = _textview.Paint; var rect = new Android.Graphics.Rect(); textpaint.GetTextBounds(_textview.Text, 0, _textview.Text.Length, rect); var xPos = 0; if (AddText.GetHorizontalAlign(_element) == Xamarin.Forms.TextAlignment.End) { xPos = v.Width - rect.Width() - _textview.PaddingLeft - _textview.PaddingRight - (int)margin.Right - 4; if (xPos < (int)margin.Left) { xPos = (int)margin.Left; } _textview.Right = v.Width - (int)margin.Right; } else { xPos = (int)margin.Left; _textview.Right = (int)margin.Left + rect.Width() + _textview.PaddingLeft + _textview.PaddingRight + 4; if (_textview.Right >= v.Width) { _textview.Right = v.Width - (int)margin.Right; } } _textview.Left = xPos; var fm = textpaint.GetFontMetrics(); var height = (int)(Math.Abs(fm.Top) + fm.Bottom + _textview.PaddingTop + _textview.PaddingEnd); var yPos = AddText.GetVerticalAlign(_element) == Xamarin.Forms.TextAlignment.Start ? 0 + (int)margin.Top : v.Height - height - (int)margin.Bottom; _textview.Top = yPos; _textview.Bottom = yPos + height; }
protected override void Draw(Canvas canvas, Rect bounds) { int saveCount = canvas.Save(); RectF arcBounds = mCurrentBounds; arcBounds.Set(bounds); //draw bottle mPaint.SetStyle(Paint.Style.Stroke); mPaint.Color = new Color(mBottleColor); canvas.DrawPath(CreateBottlePath(mBottleBounds), mPaint); //draw water mPaint.SetStyle(Paint.Style.FillAndStroke); mPaint.Color = new Color(mWaterColor); canvas.DrawPath(CreateWaterPath(mWaterBounds, mProgress), mPaint); //draw water drop mPaint.SetStyle(Paint.Style.Fill); mPaint.Color = new Color(mWaterColor); foreach (WaterDropHolder waterDropHolder in mWaterDropHolders) { if (waterDropHolder.mNeedDraw) { canvas.DrawCircle(waterDropHolder.mInitX, waterDropHolder.mCurrentY, waterDropHolder.mRadius, mPaint); } } //draw loading text mPaint.Color = new Color(mBottleColor); canvas.DrawText(LOADING_TEXT, mBottleBounds.CenterX() - mLoadingBounds.Width() / 2.0f, mBottleBounds.Bottom + mBottleBounds.Height() * 0.2f, mPaint); canvas.RestoreToCount(saveCount); }
public double StringSize(string text) { var bounds = new Android.Graphics.Rect(); TextView view = new TextView(Forms.Context); view.Paint.GetTextBounds(text, 0, text.Length, bounds); var length = bounds.Width(); return(length / Resources.System.DisplayMetrics.ScaledDensity); }
private static int ComputeWidth(TextView tv, bool isOrdered) { Android.Graphics.Paint paint = tv.Paint; using var bounds = new Android.Graphics.Rect(); var startString = isOrdered ? "99. " : "• "; paint.GetTextBounds(startString, 0, startString.Length, bounds); var width = bounds.Width(); var pt = Android.Util.TypedValue.ApplyDimension(Android.Util.ComplexUnitType.Pt, width, tv.Context.Resources.DisplayMetrics); return (int)pt; }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); Android.Graphics.Rect rect = new Android.Graphics.Rect(); Window.DecorView.GetWindowVisibleDisplayFrame(rect); JitterPhysicsGame.PreferredSize = new Vector2(rect.Width(), rect.Height()); var game = new JitterPhysicsGame(); var view = (View)game.Services.GetService(typeof(View)); SetContentView(view); game.Run(); }
public Size MeasureString(string text, Font font) { APaint.TextSize = APixels(font.Size); APaint.SetTypeface(Android.Graphics.Typeface.Default);//TODO APaint.SetStyle(Android.Graphics.Paint.Style.Stroke); var fm = APaint.GetFontMetricsInt(); var bounds = new Android.Graphics.Rect(); APaint.GetTextBounds(text, 0, text.Length, bounds); var width = bounds.Width(); var height = -fm.Top + fm.Bottom; fm.Dispose(); bounds.Dispose(); return(new Size(width, height)); }
private static int ComputeWidth(TextView tv, bool ordered) { Android.Graphics.Paint paint = tv.Paint; //paint.setTypeface(tv.getPaint().getTypeface()); //paint.setTextSize(tv.getPaint().getTextSize()); // Now compute! var bounds = new Android.Graphics.Rect(); string myString = ordered ? "99. " : "• "; paint.GetTextBounds(myString, 0, myString.Length, bounds); int width = bounds.Width(); float pt = Android.Util.TypedValue.ApplyDimension(Android.Util.ComplexUnitType.Pt, width, tv.Context.Resources.DisplayMetrics); float sp = Android.Util.TypedValue.ApplyDimension(Android.Util.ComplexUnitType.Sp, width, tv.Context.Resources.DisplayMetrics); float dip = Android.Util.TypedValue.ApplyDimension(Android.Util.ComplexUnitType.Dip, width, tv.Context.Resources.DisplayMetrics); float px = Android.Util.TypedValue.ApplyDimension(Android.Util.ComplexUnitType.Px, width, tv.Context.Resources.DisplayMetrics); float mm = Android.Util.TypedValue.ApplyDimension(Android.Util.ComplexUnitType.Mm, width, tv.Context.Resources.DisplayMetrics); return((int)pt); }
public Size MeasureString(string text, Font font) { APaint.TextSize = APixels(font.Size); APaint.SetTypeface(Android.Graphics.Typeface.Default);//TODO APaint.SetStyle(Android.Graphics.Paint.Style.Stroke); var fm = APaint.GetFontMetricsInt(); var bounds = new Android.Graphics.Rect(); APaint.GetTextBounds(text, 0, text.Length, bounds); var width = bounds.Width(); var height = -fm.Top + fm.Bottom; fm.Dispose(); bounds.Dispose(); return new Size(width, height); }
protected override void Draw(Canvas canvas, Rect bounds) { int saveCount = canvas.Save(); RectF arcBounds = mTempBounds; arcBounds.Set(bounds); arcBounds.Inset(mStrokeXInset, mStrokeYInset); mCurrentProgressBounds.Set(arcBounds.Left, arcBounds.Bottom - 2 * mCenterRadius, arcBounds.Right, arcBounds.Bottom); //Draw loading Drawable mLoadingDrawable.SetBounds((int)arcBounds.CenterX() - mLoadingDrawable.IntrinsicWidth / 2, 0, (int)arcBounds.CenterX() + mLoadingDrawable.IntrinsicWidth / 2, mLoadingDrawable.IntrinsicHeight); mLoadingDrawable.Draw(canvas); //Draw progress background float progressInset = mCenterRadius - mProgressCenterRadius; RectF progressRect = new RectF(mCurrentProgressBounds); //sub DEFAULT_STROKE_INTERVAL, otherwise will have a interval between progress background and progress outline progressRect.Inset(progressInset - DEFAULT_STROKE_INTERVAL, progressInset - DEFAULT_STROKE_INTERVAL); mPaint.Color = new Color(mProgressBgColor); mPaint.SetStyle(Paint.Style.Fill); canvas.DrawRoundRect(progressRect, mProgressCenterRadius, mProgressCenterRadius, mPaint); //Draw progress mPaint.Color = new Color(mProgressColor); mPaint.SetStyle(Paint.Style.Fill); canvas.DrawPath(CreateProgressPath(mProgress, mProgressCenterRadius, progressRect), mPaint); //Draw leaves for (int i = 0; i < mLeafHolders.Count; i++) { int leafSaveCount = canvas.Save(); LeafHolder leafHolder = mLeafHolders[i]; Rect leafBounds = leafHolder.mLeafRect; canvas.Rotate(leafHolder.mLeafRotation, leafBounds.CenterX(), leafBounds.CenterY()); mLeafDrawable.Bounds = leafBounds; mLeafDrawable.Draw(canvas); canvas.RestoreToCount(leafSaveCount); } //Draw progress background outline, //after Drawing the leaves and then Draw the outline of the progress background can //prevent the leaves from flying to the outside RectF progressOutlineRect = new RectF(mCurrentProgressBounds); float progressOutlineStrokeInset = (mCenterRadius - mProgressCenterRadius) / 2.0f; progressOutlineRect.Inset(progressOutlineStrokeInset, progressOutlineStrokeInset); mPaint.SetStyle(Paint.Style.Stroke); mPaint.Color = new Color(mProgressBgColor); mPaint.StrokeWidth = mCenterRadius - mProgressCenterRadius; canvas.DrawRoundRect(progressOutlineRect, mCenterRadius, mCenterRadius, mPaint); //Draw electric fan outline float electricFanCenterX = arcBounds.Right - mCenterRadius; float electricFanCenterY = arcBounds.Bottom - mCenterRadius; mPaint.Color = new Color(mElectricFanOutlineColor); mPaint.SetStyle(Paint.Style.Stroke); mPaint.StrokeWidth = mStrokeWidth; canvas.DrawCircle(arcBounds.Right - mCenterRadius, arcBounds.Bottom - mCenterRadius, mCenterRadius - mStrokeWidth / 2.0f, mPaint); //Draw electric background mPaint.Color = new Color(mElectricFanBgColor); mPaint.SetStyle(Paint.Style.Fill); canvas.DrawCircle(arcBounds.Right - mCenterRadius, arcBounds.Bottom - mCenterRadius, mCenterRadius - mStrokeWidth + DEFAULT_STROKE_INTERVAL, mPaint); //Draw electric fan int rotateSaveCount = canvas.Save(); canvas.Rotate(mRotation, electricFanCenterX, electricFanCenterY); mElectricFanDrawable.SetBounds((int)(electricFanCenterX - mElectricFanDrawable.IntrinsicWidth / 2 * mScale), (int)(electricFanCenterY - mElectricFanDrawable.IntrinsicHeight / 2 * mScale), (int)(electricFanCenterX + mElectricFanDrawable.IntrinsicWidth / 2 * mScale), (int)(electricFanCenterY + mElectricFanDrawable.IntrinsicHeight / 2 * mScale)); mElectricFanDrawable.Draw(canvas); canvas.RestoreToCount(rotateSaveCount); //Draw 100% text if (mScale < 1.0f) { mPaint.TextSize = mTextSize * (1 - mScale); mPaint.Color = new Color(mElectricFanOutlineColor); Rect textRect = new Rect(); mPaint.GetTextBounds(PERCENTAGE_100, 0, PERCENTAGE_100.Length, textRect); canvas.DrawText(PERCENTAGE_100, electricFanCenterX - textRect.Width() / 2.0f, electricFanCenterY + textRect.Height() / 2.0f, mPaint); } canvas.RestoreToCount(saveCount); }