canvas.DrawText让文字真正居中显示

canvas.drawColor(getResources().getColor(R.color.colorPrimary));
        super.onDraw(canvas);
        float x = this.getX();
        float y = this.getY();
        int w = this.getWidth();
        int h = this.getHeight();
        Log.e("x", String.valueOf(x));
        Log.e("y", String.valueOf(y));
        Log.e("x+w/2", String.valueOf(x + w / 2));
        Log.e("y+h/2", String.valueOf(y + h / 2));
        Paint paint = new Paint();
        Rect rect = new Rect();
        paint.setTextAlign(Paint.Align.LEFT);
//        paint.setColor(getResources().getColor(R.color.white));
        paint.getTextBounds("click to scale", 0 , "click to scale".length(), rect);
        paint.setAntiAlias(true);
Paint.FontMetrics metricsInt = paint.getFontMetrics();
//        canvas.drawText("click to scale",(getMeasuredWidth() - rect.width()) / 2,(getMeasuredHeight() - metricsInt.descent - metricsInt.ascent) / 2,paint);
        /**
        * because param y is not the text show in center,but the baseline ,so we must set the baseline instead of text to display in center
        * */
        canvas.drawText("click to scale",(getMeasuredWidth() - rect.width()) / 2,(getMeasuredHeight() - (metricsInt.descent + metricsInt.ascent)) / 2,paint);



参考:http://blog.csdn.net/lovexieyuan520/article/details/43153275


源码下载:git@github.com:edwinsnao/AnimationDemo.git

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用以下代码将 StringGrid1 中的文本居中显示: ``` procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin StringGrid1.Canvas.Brush.Color := clWhite; // 设置背景颜色为白色 StringGrid1.Canvas.FillRect(Rect); // 填充矩形区域 StringGrid1.Canvas.Font.Color := clBlack; // 设置字体颜色为黑色 StringGrid1.Canvas.TextRect(Rect, Rect.Left + (Rect.Right - Rect.Left - StringGrid1.Canvas.TextWidth(StringGrid1.Cells[ACol, ARow])) div 2, Rect.Top + (Rect.Bottom - Rect.Top - StringGrid1.Canvas.TextHeight(StringGrid1.Cells[ACol, ARow])) div 2, StringGrid1.Cells[ACol, ARow]); // 将文本居中显示 end; ``` 在 StringGrid1 中的 OnDrawCell 事件中添加以上代码即可。 ### 回答2: 想要在Delphi XE中,将StringGrid1.Canvas居中,可以使用以下步骤: 1. 在StringGrid1的属性窗口中,找到Options参数,将goFixedVertLine和goFixedHorzLine属性设置为False。这样可以清除StringGrid1上方和左侧的固定行和列。 2. 在StringGrid1的OnDrawCell事件中,编写以下代码来居中绘制单元格: procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var Text: string; TextRect: TRect; begin if (ACol > 0) and (ARow > 0) then //排除标题行和标题列 begin Text := StringGrid1.Cells[ACol, ARow]; //获取当前单元格的文本 StringGrid1.Canvas.FillRect(Rect); //清空当前单元格 TextRect := Rect; //创建一个与当前单元格大小相同的矩形 DrawText(StringGrid1.Canvas.Handle, PChar(Text), Length(Text), TextRect, DT_SINGLELINE or DT_CENTER or DT_VCENTER); //在矩形中央绘制居中的文本 end; end; 这段代码会在绘制每个单元格之前触发,并且会检查当前单元格是否为标题行和标题列。如果不是,则获取单元格的文本,并在居中的矩形中绘制文本。 最后,通过启用StringGrid1的默认绘制功能,可以确保文本居中绘制。 请注意,StringGrid1中的固定列和行会影响绘制效果,可能需要根据实际需求进行微调或设置。 ### 回答3: 要使 Delphi XE 中的 StringGrid1.Canvas 居中,您可以使用以下方法: 1. 通过设置 StringGrid1 的属性 DefaultDrawing 为 False,来自定义绘制单元格内容。 2. 在绘制事件 OnDrawCell 中,使用 Canvas.TextExtent 函数来计算单元格内容的宽度。 3. 根据计算出的宽度,使用 (StringGrid1.Width - 宽度) / 2 的公式计算出居中时的左边距。 4. 在绘制单元格内容前,使用 Canvas.TextOut 函数将左边距加上,使内容居中显示。 下面是一个示例代码: ```pascal procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var cellText: string; cellWidth, leftMargin: Integer; begin if (ACol > 0) and (ARow > 0) then begin cellText := StringGrid1.Cells[ACol, ARow]; cellWidth := StringGrid1.Canvas.TextWidth(cellText); // 计算单元格内容的宽度 leftMargin := (Rect.Width - cellWidth) div 2; // 计算居中时的左边距 StringGrid1.Canvas.TextOut(Rect.Left + leftMargin, Rect.Top, cellText); // 绘制居中的内容 end; end; ``` 这样,StringGrid1 中的单元格内容将居中显示。注意,这个方法仅适用于内容居中显示,如果您需要调整单元格本身的对齐方式,需要设置 CellSizing 为 False 并自行处理绘制事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值