创建自定义绘图

尝试 Compose 方式
Jetpack Compose 是 Android 推荐使用的界面工具包。了解如何在 Compose 中使用布局。

自定义视图最重要的部分是其外观。根据应用的需求,自定义绘图可以很简单,也可以很复杂。本文档涵盖了一些最常见的操作。

有关更多信息,请参阅 Drawable 概览

重写 onDraw()

绘制自定义视图最重要的一步是重写 onDraw() 方法。onDraw() 的参数是一个 Canvas 对象,视图可以使用它来绘制自身。Canvas 类定义了用于绘制文本、线条、位图和许多其他图形图元的方法。您可以在 onDraw() 中使用这些方法来创建自定义用户界面 (UI)。

首先创建一个 Paint 对象。下一节将更详细地讨论 Paint

创建绘图对象

android.graphics 框架将绘图分为两个领域:

  • 绘制什么,由 Canvas 处理。
  • 如何绘制,由 Paint 处理。

例如,Canvas 提供了一种绘制线条的方法,而 Paint 则提供了定义该线条颜色的方法。Canvas 有一种绘制矩形的方法,而 Paint 定义了是用颜色填充该矩形还是保持为空。Canvas 定义了您可以在屏幕上绘制的形状,而 Paint 则定义了您绘制的每个形状的颜色、样式、字体等。

在绘制任何内容之前,请创建一个或多个 Paint 对象。以下示例是在名为 init 的方法中执行此操作的。该方法在 Java 中从构造函数调用,但在 Kotlin 中可以内联初始化。

Kotlin

@ColorInt
private var textColor    // Obtained from style attributes.

@Dimension
private var textHeight   // Obtained from style attributes.

private val textPaint = Paint(ANTI_ALIAS_FLAG).apply {
    color = textColor
    if (textHeight == 0f) {
        textHeight = textSize
    } else {
        textSize = textHeight
    }
}

private val piePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
    style = Paint.Style.FILL
    textSize = textHeight
}

private val shadowPaint = Paint(0).apply {
    color = 0x101010
    maskFilter = BlurMaskFilter(8f, BlurMaskFilter.Blur.NORMAL)
}

Java

private Paint textPaint;
private Paint piePaint;
private Paint shadowPaint;

@ColorInt
private int textColor;       // Obtained from style attributes.

@Dimension
private float textHeight;    // Obtained from style attributes.

private void init() {
   textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
   textPaint.setColor(textColor);
   if (textHeight == 0) {
       textHeight = textPaint.getTextSize();
   } else {
       textPaint.setTextSize(textHeight);
   }

   piePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
   piePaint.setStyle(Paint.Style.FILL);
   piePaint.setTextSize(textHeight);

   shadowPaint = new Paint(0);
   shadowPaint.setColor(0xff101010);
   shadowPaint.setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL));
   ...
}

提前创建对象是一项重要的优化。视图经常被重绘,许多绘图对象需要昂贵的初始化过程。在 onDraw() 方法内创建绘图对象会显著降低性能,并可能导致您的 UI 变慢。

处理布局事件

要正确绘制自定义视图,需要确定其大小。复杂的自定义视图通常需要根据屏幕区域的大小和形状执行多次布局计算。切勿对视图在屏幕上的大小做出假设。即使只有一个应用使用您的视图,该应用也需要处理不同的屏幕尺寸、多种屏幕密度,以及在纵向和横向模式下的各种宽高比。

虽然 View 有许多用于处理测量的方法,但大多数方法不需要重写。如果您的视图不需要对其大小进行特殊控制,只需重写一个方法:onSizeChanged()

onSizeChanged() 在您的视图首次被分配大小时调用,如果因任何原因导致视图大小发生变化,也会再次调用。请在 onSizeChanged() 中计算位置、尺寸以及与视图大小相关的任何其他值,而不是每次绘制时都重新计算。在以下示例中,onSizeChanged() 是视图计算图表边界矩形以及文本标签和其他视觉元素相对位置的地方。

当视图被分配大小时,布局管理器会假设该大小包含视图的内边距 (padding)。计算视图大小时请处理好内边距值。以下是来自 onSizeChanged() 的代码片段,展示了如何执行此操作:

Kotlin

private val showText    // Obtained from styled attributes.
private val textWidth   // Obtained from styled attributes.

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
    super.onSizeChanged(w, h, oldw, oldh)
    // Account for padding.
    var xpad = (paddingLeft + paddingRight).toFloat()
    val ypad = (paddingTop + paddingBottom).toFloat()

    // Account for the label.
    if (showText) xpad += textWidth.toFloat()
    val ww = w.toFloat() - xpad
    val hh = h.toFloat() - ypad

    // Figure out how big you can make the pie.
    val diameter = Math.min(ww, hh)
}

Java

private Boolean showText;    // Obtained from styled attributes.
private int textWidth;       // Obtained from styled attributes.

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    // Account for padding.
    float xpad = (float)(getPaddingLeft() + getPaddingRight());
    float ypad = (float)(getPaddingTop() + getPaddingBottom());

    // Account for the label.
    if (showText) xpad += textWidth;

    float ww = (float)w - xpad;
    float hh = (float)h - ypad;

    // Figure out how big you can make the pie.
    float diameter = Math.min(ww, hh);
}

如果您需要对视图的布局参数进行更精细的控制,请实现 onMeasure()。此方法的参数是 View.MeasureSpec 值,这些值会告诉您父视图希望您的视图有多大,以及该大小是硬性最大值还是仅仅是一个建议。作为一种优化,这些值存储为打包整数,您可以使用 View.MeasureSpec 的静态方法来解包存储在每个整数中的信息。

以下是 onMeasure() 的实现示例。在此实现中,它尝试使其区域足够大,以使图表与其标签一样大:

Kotlin

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    // Try for a width based on your minimum.
    val minw: Int = paddingLeft + paddingRight + suggestedMinimumWidth
    val w: Int = View.resolveSizeAndState(minw, widthMeasureSpec, 1)

    // Whatever the width is, ask for a height that lets the pie get as big as
    // it can.
    val minh: Int = View.MeasureSpec.getSize(w) - textWidth.toInt() + paddingBottom + paddingTop
    val h: Int = View.resolveSizeAndState(minh, heightMeasureSpec, 0)

    setMeasuredDimension(w, h)
}

Java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   // Try for a width based on your minimum.
   int minw = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth();
   int w = resolveSizeAndState(minw, widthMeasureSpec, 1);

   // Whatever the width is, ask for a height that lets the pie get as big as it
   // can.
   int minh = MeasureSpec.getSize(w) - (int)textWidth + getPaddingBottom() + getPaddingTop();
   int h = resolveSizeAndState(minh, heightMeasureSpec, 0);

   setMeasuredDimension(w, h);
}

此代码中有三点需要注意:

  • 计算考虑了视图的内边距。如前所述,这是视图的责任。
  • 辅助方法 resolveSizeAndState() 用于创建最终的宽度和高度值。该辅助方法通过比较视图所需的大小与传递给 onMeasure() 的值,返回一个合适的 View.MeasureSpec 值。
  • onMeasure() 没有返回值。相反,该方法通过调用 setMeasuredDimension() 来传达其结果。调用此方法是强制性的。如果您省略此调用,View 类会抛出运行时异常。

绘制

定义完对象创建和测量代码后,就可以实现 onDraw() 了。每个视图实现 onDraw() 的方式都不同,但大多数视图都有一些共同的操作:

  • 使用 drawText() 绘制文本。通过调用 setTypeface() 指定字体,并通过调用 setColor() 指定文本颜色。
  • 使用 drawRect()drawOval()drawArc() 绘制原始形状。通过调用 setStyle() 来改变形状是填充、描边还是两者兼有。
  • 使用 Path 类绘制更复杂的形状。通过向 Path 对象添加线条和曲线来定义形状,然后使用 drawPath() 绘制该形状。与原始形状一样,路径可以根据 setStyle() 设置为描边、填充或两者兼有。
  • 通过创建 LinearGradient 对象来定义渐变填充。调用 setShader() 以在填充的形状上使用您的 LinearGradient
  • 使用 drawBitmap() 绘制位图。

以下代码绘制了文本、线条和形状的组合:

Kotlin

private val data = mutableListOf<Item>() // A list of items that are displayed.

private var shadowBounds = RectF()       // Calculated in onSizeChanged.
private var pointerRadius: Float = 2f    // Obtained from styled attributes.
private var pointerX: Float = 0f         // Calculated in onSizeChanged.
private var pointerY: Float = 0f         // Calculated in onSizeChanged.
private var textX: Float = 0f            // Calculated in onSizeChanged.
private var textY: Float = 0f            // Calculated in onSizeChanged.
private var bounds = RectF()             // Calculated in onSizeChanged.
private var currentItem: Int = 0         // The index of the currently selected item.

override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)

    canvas.apply {
        // Draw the shadow.
        drawOval(shadowBounds, shadowPaint)

        // Draw the label text.
        drawText(data[currentItem].label, textX, textY, textPaint)

        // Draw the pie slices.
        data.forEach {item ->
            piePaint.shader = item.shader
            drawArc(
                bounds,
                360 - item.endAngle,
                item.endAngle - item.startAngle,
                true,
                piePaint
            )
        }

        // Draw the pointer.
        drawLine(textX, pointerY, pointerX, pointerY, textPaint)
        drawCircle(pointerX, pointerY, pointerRadius, textPaint)
    }
}

// Maintains the state for a data item.
private data class Item(
    var label: String,      
    var value: Float = 0f,

    @ColorInt
    var color: Int = 0,

    // Computed values.
    var startAngle: Float = 0f,
    var endAngle: Float = 0f,

    var shader: Shader
)

Java

private List<Item> data = new ArrayList<Item>();  // A list of items that are displayed.

private RectF shadowBounds;                       // Calculated in onSizeChanged.
private float pointerRadius;                      // Obtained from styled attributes.
private float pointerX;                           // Calculated in onSizeChanged.
private float pointerY;                           // Calculated in onSizeChanged.
private float textX;                              // Calculated in onSizeChanged.
private float textY;                              // Calculated in onSizeChanged.
private RectF bounds;                             // Calculated in onSizeChanged.
private int currentItem = 0;                      // The index of the currently selected item.

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // Draw the shadow.
    canvas.drawOval(
            shadowBounds,
            shadowPaint
    );

    // Draw the label text.
    canvas.drawText(data.get(currentItem).label, textX, textY, textPaint);

    // Draw the pie slices.
    for (int i = 0; i < data.size(); ++i) {
        Item it = data.get(i);
        piePaint.setShader(it.shader);
        canvas.drawArc(
                bounds,
                360 - it.endAngle,
                it.endAngle - it.startAngle,
                true, 
                piePaint
        );
    }

    // Draw the pointer.
    canvas.drawLine(textX, pointerY, pointerX, pointerY, textPaint);
    canvas.drawCircle(pointerX, pointerY, pointerRadius, textPaint);
}

// Maintains the state for a data item.
private class Item {
    public String label;
    public float value;
    @ColorInt
    public int color;

    // Computed values.
    public int startAngle;
    public int endAngle;

    public Shader shader;
}    

应用图形效果

Android 12(API 级别 31)添加了 RenderEffect 类,它将模糊、颜色滤镜、Android 着色器效果等常见图形效果应用于 View 对象和渲染层次结构。您可以将效果组合为链式效果(由内部和外部效果组成)或混合效果。此功能的支持程度因设备的处理能力而异。

您还可以通过调用 View.setRenderEffect(RenderEffect) 将效果应用于 View 的底层 RenderNode

要实现 RenderEffect 对象,请执行以下操作:

view.setRenderEffect(RenderEffect.createBlurEffect(radiusX, radiusY, SHADER_TILE_MODE))

您可以以编程方式创建视图,或者从 XML 布局中解析它,并使用 视图绑定findViewById() 检索它。