protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) { var width = (int)Math.Ceiling(textPaint.MeasureText(text)) + 2; var metrics = textPaint.GetFontMetricsInt(); var height = -metrics.Top + metrics.Bottom + 2; SetMeasuredDimension(width, height); }
/// <summary> /// Draws the specified string to the backing store. /// </summary> /// <param name="text">The <see cref="System.String"/> to draw.</param> /// <param name="font">The <see cref="System.Drawing.Font"/> that will be used.</param> /// <param name="brush">The <see cref="System.Drawing.Brush"/> that will be used.</param> /// <param name="point">The location of the text on the backing store, in 2d pixel coordinates. /// The origin (0, 0) lies at the top-left corner of the backing store.</param> public void DrawString(string text, Paint paint, Point point) { m_Canvas.DrawText(text, point.X, point.Y - paint.GetFontMetricsInt().Ascent, paint); // render text on the bitmap // Fix ugly looking anti-alias IntPtr data = m_Bitmap.LockPixels(); unsafe { // Pointer to the current pixel uint *pPixel = (uint *)data; // Pointer value at which we terminate the loop (end of pixel data) var pLastPixel = pPixel + m_Bitmap.Width * m_Bitmap.Height; uint pixelValue, brightness; while (pPixel < pLastPixel) { // Get pixel data pixelValue = *pPixel; // Average RGB brightness = (((pixelValue & 0xff) + ((pixelValue >> 8) & 0xff) + ((pixelValue >> 16) & 0xff)) * 21845) >> 16; // Division by 3 // Use brightness for alpha value, set R, G, and B 0xff (white) pixelValue = brightness << 24 | 0xffffff; // Copy back to image *pPixel = pixelValue; // Next pixel pPixel++; } } m_Bitmap.UnlockPixels(); OpenTK.LoadTextureInternal(m_Texture, m_Bitmap); // copy bitmap to gl texture }
override public void Draw(Canvas canvas, ICharSequence text, int start, int end, float defaultX, int top, int defaultY, int bottom, Paint paint) { float x = 0; float y = 0; switch (position) { case RIGHT_TOP: x = canvas.Width - paint.MeasureText(text, start, end); y = paint.TextSize; break; case RIGHT_CENTER: x = canvas.Width - paint.MeasureText(text, start, end); y = canvas.Height / 2; break; case RIGHT_BOTTOM: x = canvas.Width - paint.MeasureText(text, start, end); y = canvas.Height - paint.GetFontMetricsInt().Bottom; break; } if (color != -1) { paint.Color = color; } canvas.DrawText(text, start, end, x, y, paint); }
public void drawLine() { if (canvas != null) { //canvas.DrawColor(Color.Transparent); canvas.DrawColor(Color.Argb(160, 166, 166, 166)); Rect targetRect = new Rect(0, top - textRectHeight, width, textRectHeight); Paint paint = new Paint(PaintFlags.AntiAlias); paint.StrokeWidth = 2; paint.TextSize = 36; //canvas.DrawRect(targetRect, paint); paint.Color = txtColor; FontMetricsInt fontMetrics = paint.GetFontMetricsInt(); int baseline = (targetRect.Bottom + targetRect.Top - fontMetrics.Bottom - fontMetrics.Top) / 2 + 80; paint.TextAlign = Align.Center; canvas.DrawText(tip, targetRect.CenterX(), baseline, paint); Paint linePaint = new Paint(); linePaint.AntiAlias = true; linePaint.Color = rectColor; linePaint.StrokeWidth = 5; linePaint.SetStyle(Style.Stroke); //左上 int lefttop_x = middle - rectWidthhalf; canvas.DrawLine(lefttop_x, top, lefttop_x + len, top, linePaint); canvas.DrawLine(lefttop_x, top, lefttop_x, top + len, linePaint); //右上 int righttop_x = middle + rectWidthhalf; canvas.DrawLine(righttop_x, top, righttop_x - len, top, linePaint); canvas.DrawLine(righttop_x, top, righttop_x, top + len, linePaint); //左下 int leftbottom_x = lefttop_x; int leftbottom_y = top + rectHeight; canvas.DrawLine(leftbottom_x, leftbottom_y, leftbottom_x + len, leftbottom_y, linePaint); canvas.DrawLine(leftbottom_x, leftbottom_y, leftbottom_x, leftbottom_y - len, linePaint); //右下 int rightbottom_x = middle + rectWidthhalf; int rightbottom_y = top + rectHeight; canvas.DrawLine(rightbottom_x, rightbottom_y, rightbottom_x - len, rightbottom_y, linePaint); canvas.DrawLine(rightbottom_x, rightbottom_y, rightbottom_x, rightbottom_y - len, linePaint); //清屏 Paint clearPaint = new Paint(); clearPaint.AntiAlias = true; clearPaint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.Clear)); canvas.DrawRect(new Rect { Left = lefttop_x, Top = top, Right = rightbottom_x, Bottom = rightbottom_y }, clearPaint); sh.UnlockCanvasAndPost(canvas); } }
public override void Draw(Canvas canvas, ICharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { canvas.Save(); var cachedDrawable = getCachedDrawable(); var transY = bottom - cachedDrawable.Bounds.Bottom - (paint.GetFontMetricsInt().Descent - (int)(paint.GetFontMetrics().Ascent / 2f)); canvas.Translate(x, (-paint.GetFontMetrics().Ascent) / 2f + transY / 2f); cachedDrawable.Draw(canvas); canvas.Restore(); }
protected override void OnDraw(Canvas canvas) { if (null != text) { // 画圆 canvas.DrawCircle(Width / 2, Width / 2, Width / 2, mPaintBackground); // 写字 mPaintText.TextSize = (Width / 2); mPaintText.StrokeWidth = (3); mPaintText.GetTextBounds(text, 0, 1, mRect); // 垂直居中 Paint.FontMetricsInt fontMetrics = mPaintText.GetFontMetricsInt(); int baseline = (MeasuredHeight - fontMetrics.Bottom - fontMetrics.Top) / 2; // 左右居中 mPaintText.TextAlign = Align.Center; canvas.DrawText(text, Width / 2, baseline, mPaintText); } }
public override int GetSize(Paint paint, ICharSequence text, int start, int end, Paint.FontMetricsInt fm) { Rect rect = drawable.Bounds; if (fm != null) { Paint.FontMetricsInt fmPaint = paint.GetFontMetricsInt(); int fontHeight = fmPaint.Bottom - fmPaint.Top; int drHeight = rect.Bottom - rect.Top; int top = drHeight / 2 - fontHeight / 4; int bottom = drHeight / 2 + fontHeight / 4; fm.Ascent = -bottom; fm.Top = -bottom; fm.Bottom = top; fm.Descent = top; } return(rect.Right); }
protected override void OnDraw(Canvas canvas) { base.OnDraw(canvas); int hourHeight = mHourHeight; Paint dividerPaint = mDividerPaint; dividerPaint.Color = mDividerColor; dividerPaint.SetStyle(Paint.Style.Fill); Paint labelPaint = mLabelPaint; labelPaint.Color = mLabelColor; labelPaint.TextSize = mLabelTextSize; labelPaint.SetTypeface(Typeface.DefaultBold); labelPaint.AntiAlias = true; var metrics = labelPaint.GetFontMetricsInt(); int labelHeight = Math.Abs(metrics.Ascent); int labelOffset = labelHeight + mLabelPaddingLeft; int right = Right; // Walk left side of canvas drawing timestamps int hours = mEndHour - mStartHour; for (int i = 0; i < hours; i++) { int dividerY = hourHeight * i; int nextDividerY = hourHeight * (i + 1); canvas.DrawLine(0, dividerY, right, dividerY, dividerPaint); // draw text title for timestamp canvas.DrawRect(0, dividerY, mHeaderWidth, nextDividerY, dividerPaint); // TODO: localize these labels better, including handling // 24-hour mode when set in framework. int hour = mStartHour + i; String label; if (hour == 0) { label = "12am"; } else if (hour <= 11) { label = hour + "am"; } else if (hour == 12) { label = "12pm"; } else { label = (hour - 12) + "pm"; } float labelWidth = labelPaint.MeasureText(label); canvas.DrawText(label, 0, label.Length, mHeaderWidth - labelWidth - mLabelPaddingLeft, dividerY + labelOffset, labelPaint); } }