android中SVG 的使用姿势

SVG(Scalable Vector Graphics,可伸缩矢量图形 )是W3C推出的一种开放标准的文本式矢量图形描述语言,它是基于XML、专门为网络而设计的图像格式,SVG是一种采用XML来描述二维图形语言,所以它可以直接打开xml文件来修改和编辑

在 Android 中如何使用 svg 呢,下面我们来看一下:

SVG 静态图

自定义 svg 图

1,创建 svg 格式的图

​ 制作地址:https://svg.wxeditor.com/

​ 在上面这个网站中可以在线创建 svg 图片,最后 ctrl + s 进行保存即可。

在这里插入图片描述

2,将 svg 转换为 vector Drawable

​ 工具连接:https://pan.baidu.com/s/1bp9HANH

​ 使用转换工具,将上面创建好的图片导入到转换工具,然后进行格式转换。

在这里插入图片描述

​ 点击圈起来的地方进行导入 svg 格式文件,然后就会自动生成代码如下:
在这里插入图片描述

3,使用

然后复制这些代码,打开 as,在 drawable 文件夹下创建一个 xml 文件,并粘贴这些代码

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="580dp"
    android:height="400dp"
    android:viewportWidth="580"
    android:viewportHeight="400">

    <path
        android:fillColor="#fff"
        android:pathData="M -1 -1 H 581 V 401 H -1 V -1 Z" />
    <path
        android:fillColor="#ff0000"
        android:fillAlpha="0.5"
        android:strokeColor="#bf0000"
        android:strokeAlpha="0.5"
        android:strokeWidth="1.5"       		        android:pathData="M126,150.584091l113.826691,0l35.173309,-108.134872l35.17334,108.134872l113.82666,0l-92.087524,66.830261l35.17511,108.134872l-92.087585,-66.832062l-92.08757,66.832062l35.175156,-108.134872l-92.087578,-66.830261z" />
</vector>

解释一下:

​ fillColor:填充颜色

​ strokeColor:线颜色

​ fillAlpha:填充透明度

​ strokeAlpha:线透明度

​ StorkeWidth:线宽度

​ PathData:路径值,根据值来画图片

标识方法作用
Mmoveto(M X,Y)将画笔移动到指定的位置
Llineto(L X ,Y)画直线到指定的坐标
Hhorizeontal lineTo(H X)画水平线到指定的 X 坐标
Vvertical lineto(V Y)画垂直线到指定的 Y 坐标
Ccurveto(C X1,Y1,X2,Y2,ENDEX,ENDY)三次贝塞尔曲线
Ssmooth curveto(S X2,Y2,ENDEX,ENDY)
Qquadratic Belazier curve(Q X,Y ENDX,ENDY)二次贝塞尔曲线
Tsmooth quadratic Belzier curveto(T ENDX,ENDY)映射
Aelliptical Arc(A RX,RY XROTATION,FLAGL,FAG2,X,Y)弧线
ZclosePath()关闭路径

即可使用,如下

在这里插入图片描述

​ svg 是自适应的

注意,android 5.0 一下好像不支持 svg

使用 as 自带的 svg 图片

​ 在drawable文件夹上右键->new->Vector Asset

​ 选择要使用的图片,可以设置大小 和 透明度

​ 创建成功后就可以再 drawable 文件夹中看到了。

svg 动画效果

​ 1,用到了 SVG 静态图

​ 2,用到了动画

下面看一下简单使用:

  • 首先看一下 svg 图

    <?xml version="1.0" encoding="utf-8"?>
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="580dp"
        android:height="400dp"
        android:viewportWidth="580"
        android:viewportHeight="400">
    
        <path
            android:name="star_back"
            android:fillColor="#000000"
            android:pathData="M -1 -1 H 581 V 401 H -1 V -1 Z" />
        <path
            android:name="star"
            android:fillColor="#ff0000"
            android:fillAlpha="0.5"
            android:strokeColor="#bf0000"
            android:strokeAlpha="0.5"
            android:strokeWidth="1.5"
            android:pathData="M126,150.584091l113.826691,0l35.173309,-108.134872l35.17334,108.134872l113.82666,0l-92.087524,66.830261l35.17511,108.134872l-92.087585,-66.832062l-92.08757,66.832062l35.175156,-108.134872l-92.087578,-66.830261z" />
    </vector>
    
  • 然后是 animated-vector

    <animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/star">
    <!-- animated-vector 相当于一个桥梁,静态的 svg 和 动画之间的桥梁-->
    
    <target
        android:animation="@animator/star_in"
        android:name="star"/>
    
    </animated-vector>
    

    接着看一下 target。注意他的 name 和 svg 中 path 的 name 是相同的。

    然后 animation 值的是动画 如下:

    <?xml version="1.0" encoding="utf-8"?>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="trimPathEnd"
        android:valueFrom="1"
        android:valueTo="0"
        android:valueType="floatType">
    
    </objectAnimator>
    
  • 使用:

    <ImageView
           android:id="@+id/image_main"
           android:layout_width="200dp"
           android:layout_height="200dp"
           android:src="@drawable/star"/>
    
    final AnimatedVectorDrawable drawableIn = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.star_d_in);
            final AnimatedVectorDrawable drawableOut = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.star_d_out);
            final ImageView imageView = (ImageView) findViewById(R.id.image_main);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (flag) {
                        imageView.setImageDrawable(drawableIn);
                        drawableIn.start();
                        flag = false;
                    } else {
                        imageView.setImageDrawable(drawableOut);
                        drawableOut.start();
                        flag = true;
                    }
                }
            });
    
        imageView.setImageDrawable(drawableOut);
                    drawableOut.start();
                    flag = true;
                }
            }
        });
    
    
    
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android SVG (Scalable Vector Graphics) 是一种用于在 应用程序显示可缩放矢量图形的格式。SVG 图像使用 XML 语法进行描述,可以无损地缩放和放大,而不会失去清晰度和质量。 Android 提供了一些方法来在应用程序加载和显示 SVG 图像。最常用的方法是使用第三方库,例如 AndroidSVGSvg-androidAndroidSVG Library。这些库提供了用于解析和渲染 SVG 图像的类和方法。 要在 Android 应用程序使用 SVG 图像,首先需要将 SVG 文件保存在项目的资源文件夹。然后,可以使用相应的库来加载和显示 SVG 图像。通常,需要将 SVG 图像转换为 Android 可以理解和显示的格式,例如 Bitmap 或 Drawable。 以下是一个简单的示例代码,演示如何使用 AndroidSVG 库加载和显示 SVG 图像: ```java import com.caverock.androidsvg.SVG; import com.caverock.androidsvg.SVGParseException; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Picture; import android.graphics.drawable.PictureDrawable; import android.widget.ImageView; public void loadSvgImage(Context context, ImageView imageView, int svgResId) { try { SVG svg = SVG.getFromResource(context, svgResId); Picture picture = svg.renderToPicture(); PictureDrawable drawable = new PictureDrawable(picture); imageView.setImageDrawable(drawable); } catch (SVGParseException e) { e.printStackTrace(); } } ``` 在上面的示例,`loadSvgImage` 方法接收一个上下文对象、一个 ImageView 和一个指向 SVG 文件的资源 ID。它使用 AndroidSVG 库从资源加载 SVG 图像,并将其渲染为 PictureDrawable,然后将其设置为 ImageView 的图像。 这只是一个基本示例,你可以根据自己的需求进行更多的定制和扩展。希望这能帮助你开始在 Android 应用程序使用 SVG 图像!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tʀᴜsᴛ³⁴⁵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值