手机版
你好,游客 登录 注册
背景:
阅读新闻

Android实现炫酷SVG动画效果

[日期:2015-08-20] 来源:Linux社区  作者:crazy__chen [字体:大 中 小]

整个过程有两个类,一个是SVG解析工具类:

/**
 * Util class to init and get paths from svg.
 */
public class SvgUtils {
    /**
    * It is for logging purposes.
    */
    private static final String LOG_TAG = "SVGUtils";
    /**
    * All the paths with their attributes from the svg.
    */
    private final List<SvgPath> mPaths = new ArrayList<SvgPath>();
    /**
    * The paint provided from the view.
    */
    private final Paint mSourcePaint;
    /**
    * The init svg.
    */
    private SVG mSvg;

    /**
    * Init the SVGUtils with a paint for coloring.
    *
    * @param sourcePaint - the paint for the coloring.
    */
    public SvgUtils(final Paint sourcePaint) {
        mSourcePaint = sourcePaint;
    }

    /**
    * Loading the svg from the resources.
    *
    * @param context    Context object to get the resources.
    * @param svgResource int resource id of the svg.
    */
    public void load(Context context, int svgResource) {
        if (mSvg != null) return;
        try {
            mSvg = SVG.getFromResource(context, svgResource);
            mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
        } catch (SVGParseException e) {
            Log.e(LOG_TAG, "Could not load specified SVG resource", e);
        }
    }

    /**
    * Draw the svg to the canvas.
    *
    * @param canvas The canvas to be drawn.
    * @param width  The width of the canvas.
    * @param height The height of the canvas.
    */
    public void drawSvgAfter(final Canvas canvas, final int width, final int height) {
        final float strokeWidth = mSourcePaint.getStrokeWidth();
        rescaleCanvas(width, height, strokeWidth, canvas);
    }

    /**
    * Render the svg to canvas and catch all the paths while rendering.
    *
    * @param width  - the width to scale down the view to,
    * @param height - the height to scale down the view to,
    * @return All the paths from the svg.
    */
    public List<SvgPath> getPathsForViewport(final int width, final int height) {
        final float strokeWidth = mSourcePaint.getStrokeWidth();
        Canvas canvas = new Canvas() {
            private final Matrix mMatrix = new Matrix();

            @Override
            public int getWidth() {
                return width;
            }

            @Override
            public int getHeight() {
                return height;
            }

            @Override
            public void drawPath(Path path, Paint paint) {
                Path dst = new Path();

                //noinspection deprecation
                getMatrix(mMatrix);
                path.transform(mMatrix, dst);
                paint.setAntiAlias(true);
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(strokeWidth);
                mPaths.add(new SvgPath(dst, paint));
            }
        };

        rescaleCanvas(width, height, strokeWidth, canvas);

        return mPaths;
    }

    /**
    * Rescale the canvas with specific width and height.
    *
    * @param width      The width of the canvas.
    * @param height      The height of the canvas.
    * @param strokeWidth Width of the path to add to scaling.
    * @param canvas      The canvas to be drawn.
    */
    private void rescaleCanvas(int width, int height, float strokeWidth, Canvas canvas) {
        final RectF viewBox = mSvg.getDocumentViewBox();

        final float scale = Math.min(width
                        / (viewBox.width() + strokeWidth),
                height / (viewBox.height() + strokeWidth));

        canvas.translate((width - viewBox.width() * scale) / 2.0f,
                (height - viewBox.height() * scale) / 2.0f);
        canvas.scale(scale, scale);

        mSvg.renderToCanvas(canvas);
    }

    /**
    * Path with bounds for scalling , length and paint.
    */
    public static class SvgPath {
        /**
        * Region of the path.
        */
        private static final Region REGION = new Region();
        /**
        * This is done for clipping the bounds of the path.
        */
        private static final Region MAX_CLIP =
                new Region(Integer.MIN_VALUE, Integer.MIN_VALUE,
                        Integer.MAX_VALUE, Integer.MAX_VALUE);
        /**
        * The path itself.
        */
        final Path path;
        /**
        * The paint to be drawn later.
        */
        final Paint paint;
        /**
        * The length of the path.
        */
        final float length;
        /**
        * The bounds of the path.
        */
        final Rect bounds;
        /**
        * The measure of the path, we can use it later to get segment of it.
        */
        final PathMeasure measure;

        /**
        * Constructor to add the path and the paint.
        *
        * @param path  The path that comes from the rendered svg.
        * @param paint The result paint.
        */
        SvgPath(Path path, Paint paint) {
            this.path = path;
            this.paint = paint;

            measure = new PathMeasure(path, false);
            this.length = measure.getLength();

            REGION.setPath(path, MAX_CLIP);
            bounds = REGION.getBounds();
        }
    }
}

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-08/121989.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款