【问题标题】:Custom created View自定义创建的视图
【发布时间】:2015-06-12 03:15:32
【问题描述】:

我创建了一个自定义视图,在单独的矩形框中显示数字的数字:

onDraw() 的代码如下:

protected void onDraw(Canvas canvas){
    Rect rect = new Rect(20, 20, 170, 220);

    int xx = rect.left + 12;
    int yy = (rect.bottom - 25);

    int yyy = (rect.bottom +10);
    int xxx = (rect.left-10);

    for (int i = counter,j=0; i >= 0; i--,j++) {
        // Drawing the rectangle and using the rectangle as reference, drawing the digit.
        drawColoredDigit(canvas, rect, String.valueOf(digits[i]),xx,yy);
        // Updating the reference values for the rectangle, and in turn for the digits inside the reference.
        rect.left += (20 + 150);
        rect.right += (20 + 150);
        xx = (rect.left+ 20);
        xxx = (rect.left - 20);

        if(((counter-j)%3==0)&&(counter!=j))
        {
            canvas.drawText(",",xxx,yyy,commaPaint);
            rect.left += 20;
            rect.right += 20;
            xx = (rect.left + 20);
        }

    }
}

private void drawColoredDigit(Canvas canvas, Rect rect, String digit, int xx, int yy) {
    canvas.drawRect(rect, widgetPaint);
    canvas.drawText(digit, xx, yy, textPaint);
}

有没有一种方法可以使此代码更加动态,以便根据数字的长度选择呈现数字的矩形的大小。然后根据渲染的矩形的大小选择数字的大小。 还需要处理各种设备上的像素处理。 基本上试图使视图更具动态性。

【问题讨论】:

    标签: android dynamic android-custom-view ondraw custom-view


    【解决方案1】:

    我看到TextViews 的一些实现会自动调整文本大小以适应视图的大小。我的建议是从其中一个开始并在文本上使用跨度,而不是尝试自己绘制矩形和事物。查看ForegroundColorSpanBackgroundColorSpan,并使用SpannableString 将跨度附加到文本。

    【讨论】:

      猜你喜欢
      • 2011-05-24
      • 2016-04-30
      • 2012-07-25
      • 2022-08-02
      • 1970-01-01
      • 2014-08-07
      • 2016-01-21
      相关资源
      最近更新 更多