设为首页 加入收藏

TOP

Android使用SVG矢量图打造酷炫动画效果(二)
2015-08-31 21:23:45 来源: 作者: 【 】 浏览:70
Tags:Android 使用 SVG 矢量图 打造 动画 效果
的path数据复杂度也不一样,但格式也算是非常的清楚,即采用一定的指令把数据点进行拼接;
现在有了这些数据点,我们需要做的则是对数据进行解析,封装成我们要的Path;


解析的过程也无非是 遇到指令则采用android Path 里的对应方法进行置换,解析方式如下:


public Path parsePath(String s) throws ParseException {
? ? ? ? mCurrentPoint.set(Float.NaN, Float.NaN);
? ? ? ? mPathString = s;
? ? ? ? mIndex = 0;
? ? ? ? mLength = mPathString.length();


? ? ? ? PointF tempPoint1 = new PointF();
? ? ? ? PointF tempPoint2 = new PointF();
? ? ? ? PointF tempPoint3 = new PointF();


? ? ? ? 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': {
? ? ? ? ? ? ? ? ? ? // m指令,相当于android 里的 moveTo()
? ? ? ? ? ? ? ? ? ? boolean firstPoint = true;
? ? ? ? ? ? ? ? ? ? while (advanceToNextToken() == TOKEN_VALUE) {
? ? ? ? ? ? ? ? ? ? ? ? consumeAndTransformPoint(tempPoint1,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? relative && mCurrentPoint.x != Float.NaN);
? ? ? ? ? ? ? ? ? ? ? ? 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': {
? ? ? ? ? ? ? ? ? ? // c指令,相当于android 里的 cubicTo()
? ? ? ? ? ? ? ? ? ? if (mCurrentPoint.x == Float.NaN) {
? ? ? ? ? ? ? ? ? ? ? ? throw new ParseException("Relative commands require current point", mIndex);
? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? while (advanceToNextToken() == TOKEN_VALUE) {
? ? ? ? ? ? ? ? ? ? ? ? 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()进行画直线
? ? ? ? ? ? ? ? ? ? if (mCurrentPoint.x == Float.NaN) {
? ? ? ? ? ? ? ? ? ? ? ? throw new ParseException("Relative commands require current point", mIndex);
? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? while (advanceToNextToken() == TOKEN_VALUE) {
? ? ? ? ? ? ? ? ? ? ? ? consumeAndTransformPoint(tempPoint1, relative);
? ? ? ? ? ? ? ? ? ? ? ? p.lineTo(tempPoint1.x, tempPoint1.y);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? mCurrentPoint.set(tempPoint1);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? case 'H':
? ? ? ? ? ? ? ? case 'h': {
? ? ? ? ? ? ? ? ? ? // 画水平直线
? ? ? ? ? ? ? ? ? ? if (mCurrentPoint.x == Float.NaN) {
? ? ? ? ? ? ? ? ? ? ? ? throw new ParseException("Relative commands require current point", mIndex);
? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? while (advanceToNextToken() == TOKEN_VALUE) {
? ? ? ? ? ? ? ? ? ? ? ? float x = transformX(consumeva lue());
? ? ? ? ? ? ? ? ? ? ? ? if (relative) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? x += mCurrentPoint.x;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? p.lineTo(x, mCurrentPoint.y);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? mCurrentPoint.set(tempPoint1);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? case 'V':
? ? ? ? ? ? ? ? case 'v': {
? ? ? ? ? ? ? ? ? ? // 画竖直直线
? ? ? ? ? ? ? ? ? ? if (mCurrentPoint.x == Float.NaN) {
? ? ? ? ? ? ? ? ? ? ? ? throw new ParseException("Relative commands require current point", mIndex);
? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? while (advanceToNextToken() == TOKEN_VALUE) {
? ? ? ? ? ?

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android动效篇:一个绚丽的Loadin.. 下一篇Android注解支持(Support Annotat..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: