用 SVG 矢量圖在 Android 上做出一段「光沿文字邊緣跑動然後填充」的效果,看起來挺唬人,其實拆開來只有幾個問題:
1. 素材是圖片還是文字?
2. 如何拿到圖形的輪廓座標?
3. 拿到座標後,怎麼讓光沿著路徑跑?
4. 各階段之間怎麼銜接?
答案先說:背景是普通位圖,表面的文字則是 SVG 矢量圖——這正是能拿到精確邊沿數據的關鍵。
SVG(Scalable Vector Graphics)是用 XML 描述二維圖形的語言,優勢很明確:
也就是說,我們能從 SVG 裡拿到想要的全部矢量數據。
<svg width="100%" height="100%" version="1.1"
xmlns="https://www.w3.org/2000/svg">
<path d="M250 150 L150 350 L350 350 Z" />
</svg>
其中 M 等同 Path.moveTo(),Z 等同 close()。完整對照:
1)先用 PS 等工具把想要的圖形做出來;
2)用 GIMP 這類矢量軟件導出 SVG:魔棒工具快速建立選區 → 將選區導出為 path → 在右側面板看到生成的路徑 → 導出路徑。
得到的數據形如:
<svg xmlns="https://www.w3.org/2000/svg"
width="6.95746in" height="1.82269in" viewBox="0 0 668 175">
<path id="Selection" fill="none" stroke="black" stroke-width="1"
d="M 530.00,34.00
C 530.00,34.00 526.08,59.00 526.08,59.00
...
C 263.00,134.00 263.00,145.00 263.00,145.00
263.00,145.00 202.00,145.00 202.00,145.00 Z" />
</svg>
圖形越複雜,path 數據越長,但格式始終一致:用指令把座標點串起來。
解析本質就是「遇到指令替換成 Path 的對應方法」,核心骨架如下(省略了 consumeCommand、advanceToNextToken 等詞法細節):
public Path parsePath(String s) throws ParseException {
mCurrentPoint.set(Float.NaN, Float.NaN);
mPathString = s;
mIndex = 0;
mLength = mPathString.length();
Path p = new Path();
p.setFillType(Path.FillType.WINDING);
boolean firstMove = true;
while (mIndex < mLength) {
char command = consumeCommand();
boolean relative = (mCurrentToken == TOKEN_RELATIVE_COMMAND);
switch (command) {
case 'M': case 'm': // 對應 moveTo / lineTo
boolean firstPoint = true;
while (advanceToNextToken() == TOKEN_VALUE) {
consumeAndTransformPoint(tempPoint1, relative);
if (firstPoint) {
p.moveTo(tempPoint1.x, tempPoint1.y);
firstPoint = false;
if (firstMove) {
mCurrentPoint.set(tempPoint1);
firstMove = false;
}
} else {
p.lineTo(tempPoint1.x, tempPoint1.y);
}
}
mCurrentPoint.set(tempPoint1);
break;
case 'C': case 'c': // 對應 cubicTo
consumeAndTransformPoint(tempPoint1, relative);
consumeAndTransformPoint(tempPoint2, relative);
consumeAndTransformPoint(tempPoint3, relative);
p.cubicTo(tempPoint1.x, tempPoint1.y,
tempPoint2.x, tempPoint2.y,
tempPoint3.x, tempPoint3.y);
mCurrentPoint.set(tempPoint3);
break;
case 'L': case 'l': // lineTo
case 'H': case 'h': // 水平線,只讀 x
case 'V': case 'v': // 豎直線,只讀 y
case 'Z': case 'z': // close()
p.close();
break;
}
}
return p;
}
注意相對指令(小寫)要疊加到當前點上,且相對指令執行前必須已有當前點,否則拋 ParseException。
有了 Path,剩下的就是分階段畫。onDraw 裡做三件事:先畫描邊的「跑光」,再畫殘留尾跡,最後做 alpha 漸變填充;未結束則 postInvalidateOnAnimation 觸發下一幀。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mState == STATE_NOT_STARTED || mGlyphData == null) return;
long t = System.currentTimeMillis() - mStartTime;
// 階段一:邊沿線生長 + 光點跑動
for (int i = 0; i < mGlyphData.length; i++) {
float phase = MathUtil.constrain(0, 1,
(t - (mTraceTime - mTraceTimePerGlyph) * i * 1f / mGlyphData.length)
* 1f / mTraceTimePerGlyph);
float distance = INTERPOLATOR.getInterpolation(phase) * mGlyphData[i].length;
// 殘留尾跡
mGlyphData[i].paint.setColor(mTraceResidueColors[i]);
mGlyphData[i].paint.setPathEffect(new DashPathEffect(
new float[] { distance, mGlyphData[i].length }, 0));
canvas.drawPath(mGlyphData[i].path, mGlyphData[i].paint);
// 亮色光頭
mGlyphData[i].paint.setColor(mTraceColors[i]);
mGlyphData[i].paint.setPathEffect(new DashPathEffect(
new float[] { 0, distance, phase > 0 ? mMarkerLength : 0,
mGlyphData[i].length }, 0));
canvas.drawPath(mGlyphData[i].path, mGlyphData[i].paint);
}
// 階段二:整體漸變填充
if (t > mFillStart) {
float phase = MathUtil.constrain(0, 1, (t - mFillStart) * 1f / mFillTime);
for (int i = 0; i < mGlyphData.length; i++) {
GlyphData glyphData = mGlyphData[i];
mFillPaint.setARGB(
(int) (phase * ((float) mFillAlphas[i] / 255) * 255),
mFillReds[i], mFillGreens[i], mFillBlues[i]);
canvas.drawPath(glyphData.path, mFillPaint);
}
}
if (t < mFillStart + mFillTime) {
ViewCompat.postInvalidateOnAnimation(this);
} else {
changeState(STATE_FINISHED);
}
}
整套效果的核心其實就兩件事:DashPathEffect 控制「畫到哪」,插值器控制「畫多快」。理解這兩點後,換素材、改節奏、調顏色都是分鐘級的事,不必重造輪子。