| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.test.hwui; |
| 18 |
|
| 19 |
import android.app.Activity; |
| 20 |
import android.content.Context; |
| 21 |
import android.graphics.Canvas; |
| 22 |
import android.graphics.Paint; |
| 23 |
import android.graphics.Path; |
| 24 |
import android.graphics.PathMeasure; |
| 25 |
import android.os.Bundle; |
| 26 |
import android.view.View; |
| 27 |
|
| 28 |
@SuppressWarnings({"UnusedDeclaration"}) |
| 29 |
public class TextOnPathActivity extends Activity { |
| 30 |
private Path mPath; |
| 31 |
private Path mStraightPath; |
| 32 |
|
| 33 |
@Override |
| 34 |
protected void onCreate(Bundle savedInstanceState) { |
| 35 |
super.onCreate(savedInstanceState); |
| 36 |
|
| 37 |
mPath = makePath(); |
| 38 |
mStraightPath = makeStraightPath(); |
| 39 |
|
| 40 |
final TextOnPathView view = new TextOnPathView(this); |
| 41 |
setContentView(view); |
| 42 |
} |
| 43 |
|
| 44 |
private static Path makePath() { |
| 45 |
Path path = new Path(); |
| 46 |
buildPath(path); |
| 47 |
return path; |
| 48 |
} |
| 49 |
|
| 50 |
private static void buildPath(Path path) { |
| 51 |
path.moveTo(0.0f, 0.0f); |
| 52 |
path.cubicTo(0.0f, 0.0f, 100.0f, 150.0f, 100.0f, 200.0f); |
| 53 |
path.cubicTo(100.0f, 200.0f, 50.0f, 300.0f, -80.0f, 200.0f); |
| 54 |
path.cubicTo(-80.0f, 200.0f, 100.0f, 200.0f, 200.0f, 0.0f); |
| 55 |
} |
| 56 |
|
| 57 |
private static Path makeStraightPath() { |
| 58 |
Path path = new Path(); |
| 59 |
buildStraightPath(path); |
| 60 |
return path; |
| 61 |
} |
| 62 |
|
| 63 |
private static void buildStraightPath(Path path) { |
| 64 |
path.moveTo(0.0f, 0.0f); |
| 65 |
path.lineTo(400.0f, 0.0f); |
| 66 |
} |
| 67 |
|
| 68 |
public class TextOnPathView extends View { |
| 69 |
private static final String TEST_STRING = "Hello OpenGL renderer, text on path! "; |
| 70 |
|
| 71 |
private final Paint mPaint; |
| 72 |
private final Paint mPathPaint; |
| 73 |
private final String mText; |
| 74 |
private final PathMeasure mMeasure; |
| 75 |
private final float mLength; |
| 76 |
private final float[] mLines; |
| 77 |
private final float[] mPos; |
| 78 |
private final float[] mTan; |
| 79 |
|
| 80 |
public TextOnPathView(Context c) { |
| 81 |
super(c); |
| 82 |
|
| 83 |
mPaint = new Paint(); |
| 84 |
mPaint.setAntiAlias(true); |
| 85 |
mPaint.setColor(0xff000000); |
| 86 |
|
| 87 |
mPathPaint = new Paint(); |
| 88 |
mPathPaint.setAntiAlias(true); |
| 89 |
mPathPaint.setStyle(Paint.Style.STROKE); |
| 90 |
mPathPaint.setColor(0xff000099); |
| 91 |
|
| 92 |
StringBuilder builder = new StringBuilder(TEST_STRING.length() * 2); |
| 93 |
for (int i = 0; i < 2; i++) { |
| 94 |
builder.append(TEST_STRING); |
| 95 |
} |
| 96 |
mText = builder.toString(); |
| 97 |
|
| 98 |
mMeasure = new PathMeasure(mPath, false); |
| 99 |
mLength = mMeasure.getLength(); |
| 100 |
|
| 101 |
mLines = new float[100 * 4]; |
| 102 |
mPos = new float[2]; |
| 103 |
mTan = new float[2]; |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
protected void onDraw(Canvas canvas) { |
| 108 |
super.onDraw(canvas); |
| 109 |
|
| 110 |
canvas.drawARGB(255, 255, 255, 255); |
| 111 |
|
| 112 |
canvas.save(); |
| 113 |
canvas.translate(400.0f, 350.0f); |
| 114 |
mPaint.setTextAlign(Paint.Align.LEFT); |
| 115 |
canvas.drawTextOnPath(mText + mText, mPath, 0.0f, 0.0f, mPaint); |
| 116 |
canvas.drawPath(mPath, mPathPaint); |
| 117 |
|
| 118 |
for (int i = 0; i < 100; i++) { |
| 119 |
mMeasure.getPosTan(i * mLength / 100.0f, mPos, mTan); |
| 120 |
mLines[i * 4 ] = mPos[0]; |
| 121 |
mLines[i * 4 + 1] = mPos[1]; |
| 122 |
mLines[i * 4 + 2] = mPos[0] + mTan[1] * 15; |
| 123 |
mLines[i * 4 + 3] = mPos[1] - mTan[0] * 15; |
| 124 |
} |
| 125 |
canvas.drawLines(mLines, mPathPaint); |
| 126 |
|
| 127 |
canvas.translate(200.0f, 0.0f); |
| 128 |
canvas.drawTextOnPath(mText + mText, mStraightPath, 0.0f, 0.0f, mPaint); |
| 129 |
canvas.drawPath(mStraightPath, mPathPaint); |
| 130 |
|
| 131 |
canvas.restore(); |
| 132 |
|
| 133 |
canvas.save(); |
| 134 |
canvas.translate(150.0f, 60.0f); |
| 135 |
canvas.drawTextOnPath(mText, mPath, 0.0f, 10.0f, mPaint); |
| 136 |
mMeasure.getPosTan(5.0f, mPos, mTan); |
| 137 |
canvas.drawLine(mPos[0], mPos[1], mPos[0] + mTan[1] * 10, mPos[1] - mTan[0] * 10, |
| 138 |
mPathPaint); |
| 139 |
canvas.drawPath(mPath, mPathPaint); |
| 140 |
|
| 141 |
canvas.translate(250.0f, 0.0f); |
| 142 |
mPaint.setTextAlign(Paint.Align.CENTER); |
| 143 |
canvas.drawTextOnPath(mText, mPath, 0.0f, 0.0f, mPaint); |
| 144 |
canvas.drawPath(mPath, mPathPaint); |
| 145 |
|
| 146 |
canvas.translate(250.0f, 0.0f); |
| 147 |
mPaint.setTextAlign(Paint.Align.RIGHT); |
| 148 |
canvas.drawTextOnPath(mText, mPath, 0.0f, 0.0f, mPaint); |
| 149 |
canvas.drawPath(mPath, mPathPaint); |
| 150 |
canvas.restore(); |
| 151 |
} |
| 152 |
} |
| 153 |
} |