Android 使用Vector XML文件创建矢量图片资源

参考链接:http://blog.csdn.net/klxh2009/article/details/51121034

<article>
    <h1 class="csdn_top">Android 使用Vector XML文件创建矢量图片资源</h1>
    <div class="article_bar clearfix">
        <div class="artical_tag">
            <span class="original">
            原创                </span>
            <span class="time">2016年04月11日 13:35:23</span>
        </div>

        <ul class="article_tags clearfix csdn-tracking-statistics tracking-click" data-mod="popu_377">
            <li class="tit">标签:</li>


  • android开发 /
  • 矢量图 /
  • svg /
  • vector /
  • gimp /





  • 转载请注明出处:http://blog.csdn.net/klxh2009/article/details/51121034 本文出自【付小华的博客】


    Vector:矢量的意思

    我们知道,在安卓开发过程中,经常使用到png格式的图片资源,这种图片需要有不同分辨率来做屏幕适配,当图片数量很大时,被打包的图片资源占据了app的绝大部分容量,使用Vector来创建图片,将大大减少png图片的使用,提高开发性能。废话不多说,可以先看下效果:


    这就是一个矢量图,下面我来讲讲怎么来实现它吧:

    所需工具:

    1、阿里巴巴矢量图库(http://www.iconfont.cn/)

    扫描二维码关注公众号,回复: 389376 查看本文章

    2、GIMP(GNU Image Manipulation Program)

    其中,GIMP我提供了两种下载方式:

    官网:http://www.gimp.org/

    百度网盘:http://pan.baidu.com/s/1sl9VnRZ

    3、Android Studio


    实现步骤:

    1、从阿里巴巴矢量图库下载需要的svg矢量图


    (注意这里的尺寸)

    2、使用已经安装好了的GIMP打开下载的这个矢量图


    3、选择Tool Options下的魔棒点击刚刚打开的五角星



    (这是选中之后的效果,出现蚂蚁线,,,会PS的同学应该对这个很明白的)

    4、然后再按图示操作:选择【Select】–>【To Path】,就可以看到右边的Brushes窗口下的Paths选项里多了个路径。



    5、导出路径


    导出时选择自己需要导出到的路劲,输入文件名,建议导出使用xml格式的文件,我这里为“star.xml”,然后点击save


    6、打开star.xml文件,可以看到好多数字,在path节点下的d里,这里的值就是我们需要的



    7、我们使用Android Studio来写代码,在drawable目录下新建一个star_path.xml文件,内容如下:

    其中需要注意这里的viewportHeight和viewportWidth值,设置太小就不能看到它的预览图了


    1. <?xml version=“1.0” encoding=“utf-8”?>  
    2. <vector xmlns:android=“http://schemas.android.com/apk/res/android”  
    3.     android:width=“200dp”  
    4.     android:height=“200dp”  
    5.     android:viewportHeight=“200”  
    6.     android:viewportWidth=“200”>  
    7.         <path  
    8.             android:fillColor=“#00BB9C”  
    9.             android:pathData=”M 120.25,62.00  
    10.            C 121.56,64.62 124.79,71.70 126.63,73.44  
    11.              129.39,76.04 138.05,76.58 142.00,77.13  
    12.              142.00,77.13 178.00,82.00 178.00,82.00  
    13.              174.13,90.18 158.14,103.06 150.99,110.04  
    14.              148.73,112.24 143.25,117.35 142.11,120.00  
    15.              140.83,122.98 142.80,131.54 143.42,135.00  
    16.              143.42,135.00 150.00,171.00 150.00,171.00  
    17.              142.14,169.53 124.31,158.91 116.00,154.75  
    18.              113.30,153.40 105.49,149.02 103.00,148.88  
    19.              99.54,148.69 91.24,153.75 88.00,155.58  
    20.              81.75,159.10 61.89,169.90 56.00,171.00  
    21.              56.00,171.00 61.80,136.00 61.80,136.00  
    22.              61.80,136.00 63.60,120.58 63.60,120.58  
    23.              63.60,120.58 53.00,109.00 53.00,109.00  
    24.              53.00,109.00 27.00,82.00 27.00,82.00  
    25.              27.00,82.00 63.00,77.13 63.00,77.13  
    26.              67.09,76.56 75.64,76.00 78.61,73.44  
    27.              81.01,71.37 84.64,63.21 86.25,60.00  
    28.              86.25,60.00 102.00,28.00 102.00,28.00  
    29.              106.72,32.72 116.58,54.66 120.25,62.00 Z  
    30.            M 91.25,75.00  
    31.            C 90.14,77.23 88.11,81.97 86.51,83.57  
    32.              83.98,86.09 79.42,86.24 76.00,86.73  
    33.              76.00,86.73 51.00,90.00 51.00,90.00  
    34.              51.00,90.00 68.00,108.00 68.00,108.00  
    35.              68.00,108.00 75.36,117.00 75.36,117.00  
    36.              75.36,117.00 74.41,128.00 74.41,128.00  
    37.              74.41,128.00 71.00,151.00 71.00,151.00  
    38.              75.21,149.77 88.51,142.63 93.00,140.14  
    39.              96.31,138.31 100.02,135.40 104.00,136.17  
    40.              104.00,136.17 135.00,151.00 135.00,151.00  
    41.              135.00,151.00 130.75,127.00 130.75,127.00  
    42.              130.75,127.00 130.11,116.00 130.11,116.00  
    43.              130.11,116.00 137.00,108.00 137.00,108.00  
    44.              137.00,108.00 155.00,90.00 155.00,90.00  
    45.              155.00,90.00 130.00,86.73 130.00,86.73  
    46.              130.00,86.73 119.39,84.15 119.39,84.15  
    47.              119.39,84.15 113.14,73.00 113.14,73.00  
    48.              113.14,73.00 103.00,53.00 103.00,53.00  
    49.              99.46,57.45 94.06,69.34 91.25,75.00 Z” />  
    50. </vector>  
    <?xml version="1.0" encoding="utf-8"?>
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="200dp"
        android:height="200dp"
        android:viewportHeight="200"
        android:viewportWidth="200">
            <path
                android:fillColor="#00BB9C"
                android:pathData="M 120.25,62.00
               C 121.56,64.62 124.79,71.70 126.63,73.44
                 129.39,76.04 138.05,76.58 142.00,77.13
                 142.00,77.13 178.00,82.00 178.00,82.00
                 174.13,90.18 158.14,103.06 150.99,110.04
                 148.73,112.24 143.25,117.35 142.11,120.00
                 140.83,122.98 142.80,131.54 143.42,135.00
                 143.42,135.00 150.00,171.00 150.00,171.00
                 142.14,169.53 124.31,158.91 116.00,154.75
                 113.30,153.40 105.49,149.02 103.00,148.88
                 99.54,148.69 91.24,153.75 88.00,155.58
                 81.75,159.10 61.89,169.90 56.00,171.00
                 56.00,171.00 61.80,136.00 61.80,136.00
                 61.80,136.00 63.60,120.58 63.60,120.58
                 63.60,120.58 53.00,109.00 53.00,109.00
                 53.00,109.00 27.00,82.00 27.00,82.00
                 27.00,82.00 63.00,77.13 63.00,77.13
                 67.09,76.56 75.64,76.00 78.61,73.44
                 81.01,71.37 84.64,63.21 86.25,60.00
                 86.25,60.00 102.00,28.00 102.00,28.00
                 106.72,32.72 116.58,54.66 120.25,62.00 Z
               M 91.25,75.00
               C 90.14,77.23 88.11,81.97 86.51,83.57
                 83.98,86.09 79.42,86.24 76.00,86.73
                 76.00,86.73 51.00,90.00 51.00,90.00
                 51.00,90.00 68.00,108.00 68.00,108.00
                 68.00,108.00 75.36,117.00 75.36,117.00
                 75.36,117.00 74.41,128.00 74.41,128.00
                 74.41,128.00 71.00,151.00 71.00,151.00
                 75.21,149.77 88.51,142.63 93.00,140.14
                 96.31,138.31 100.02,135.40 104.00,136.17
                 104.00,136.17 135.00,151.00 135.00,151.00
                 135.00,151.00 130.75,127.00 130.75,127.00
                 130.75,127.00 130.11,116.00 130.11,116.00
                 130.11,116.00 137.00,108.00 137.00,108.00
                 137.00,108.00 155.00,90.00 155.00,90.00
                 155.00,90.00 130.00,86.73 130.00,86.73
                 130.00,86.73 119.39,84.15 119.39,84.15
                 119.39,84.15 113.14,73.00 113.14,73.00
                 113.14,73.00 103.00,53.00 103.00,53.00
                 99.46,57.45 94.06,69.34 91.25,75.00 Z" />
    </vector>


    8、最后在布局文件里使用:

    1. <?xml version=“1.0” encoding=“utf-8”?>  
    2. <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android  
    3.     xmlns:tools=http://schemas.android.com/tools  
    4.     android:layout_width=“match_parent”  
    5.     android:layout_height=“match_parent”  
    6.     android:paddingBottom=“@dimen/activity_vertical_margin”  
    7.     android:paddingLeft=“@dimen/activity_horizontal_margin”  
    8.     android:paddingRight=“@dimen/activity_horizontal_margin”  
    9.     android:paddingTop=“@dimen/activity_vertical_margin”  
    10.     tools:context=“com.shenhua.vectordemo.MainActivity”>  
    11.   
    12.     <TextView  
    13.         android:layout_width=“wrap_content”  
    14.         android:layout_height=“wrap_content”  
    15.         android:text=“Hello World!” />  
    16.   
    17.     <ImageView  
    18.         android:layout_width=“wrap_content”  
    19.         android:layout_height=“wrap_content”  
    20.         android:layout_centerInParent=“true”  
    21.         android:src=“@drawable/star_path” />  
    22. </RelativeLayout>  
    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.shenhua.vectordemo.MainActivity">

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" /&gt;
    
    &lt;ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/star_path" /&gt;
    

    </RelativeLayout>
    目录结构:


    OK,就是如此简单。



    转载请注明出处:http://blog.csdn.net/klxh2009/article/details/51121034 本文出自【付小华的博客】


    Vector:矢量的意思

    我们知道,在安卓开发过程中,经常使用到png格式的图片资源,这种图片需要有不同分辨率来做屏幕适配,当图片数量很大时,被打包的图片资源占据了app的绝大部分容量,使用Vector来创建图片,将大大减少png图片的使用,提高开发性能。废话不多说,可以先看下效果:


    这就是一个矢量图,下面我来讲讲怎么来实现它吧:

    所需工具:

    1、阿里巴巴矢量图库(http://www.iconfont.cn/)

    2、GIMP(GNU Image Manipulation Program)

    其中,GIMP我提供了两种下载方式:

    官网:http://www.gimp.org/

    百度网盘:http://pan.baidu.com/s/1sl9VnRZ

    3、Android Studio


    实现步骤:

    1、从阿里巴巴矢量图库下载需要的svg矢量图


    (注意这里的尺寸)

    2、使用已经安装好了的GIMP打开下载的这个矢量图


    3、选择Tool Options下的魔棒点击刚刚打开的五角星



    (这是选中之后的效果,出现蚂蚁线,,,会PS的同学应该对这个很明白的)

    4、然后再按图示操作:选择【Select】–>【To Path】,就可以看到右边的Brushes窗口下的Paths选项里多了个路径。



    5、导出路径


    导出时选择自己需要导出到的路劲,输入文件名,建议导出使用xml格式的文件,我这里为“star.xml”,然后点击save


    6、打开star.xml文件,可以看到好多数字,在path节点下的d里,这里的值就是我们需要的



    7、我们使用Android Studio来写代码,在drawable目录下新建一个star_path.xml文件,内容如下:

    其中需要注意这里的viewportHeight和viewportWidth值,设置太小就不能看到它的预览图了


    1. <?xml version=“1.0” encoding=“utf-8”?>  
    2. <vector xmlns:android=“http://schemas.android.com/apk/res/android”  
    3.     android:width=“200dp”  
    4.     android:height=“200dp”  
    5.     android:viewportHeight=“200”  
    6.     android:viewportWidth=“200”>  
    7.         <path  
    8.             android:fillColor=“#00BB9C”  
    9.             android:pathData=”M 120.25,62.00  
    10.            C 121.56,64.62 124.79,71.70 126.63,73.44  
    11.              129.39,76.04 138.05,76.58 142.00,77.13  
    12.              142.00,77.13 178.00,82.00 178.00,82.00  
    13.              174.13,90.18 158.14,103.06 150.99,110.04  
    14.              148.73,112.24 143.25,117.35 142.11,120.00  
    15.              140.83,122.98 142.80,131.54 143.42,135.00  
    16.              143.42,135.00 150.00,171.00 150.00,171.00  
    17.              142.14,169.53 124.31,158.91 116.00,154.75  
    18.              113.30,153.40 105.49,149.02 103.00,148.88  
    19.              99.54,148.69 91.24,153.75 88.00,155.58  
    20.              81.75,159.10 61.89,169.90 56.00,171.00  
    21.              56.00,171.00 61.80,136.00 61.80,136.00  
    22.              61.80,136.00 63.60,120.58 63.60,120.58  
    23.              63.60,120.58 53.00,109.00 53.00,109.00  
    24.              53.00,109.00 27.00,82.00 27.00,82.00  
    25.              27.00,82.00 63.00,77.13 63.00,77.13  
    26.              67.09,76.56 75.64,76.00 78.61,73.44  
    27.              81.01,71.37 84.64,63.21 86.25,60.00  
    28.              86.25,60.00 102.00,28.00 102.00,28.00  
    29.              106.72,32.72 116.58,54.66 120.25,62.00 Z  
    30.            M 91.25,75.00  
    31.            C 90.14,77.23 88.11,81.97 86.51,83.57  
    32.              83.98,86.09 79.42,86.24 76.00,86.73  
    33.              76.00,86.73 51.00,90.00 51.00,90.00  
    34.              51.00,90.00 68.00,108.00 68.00,108.00  
    35.              68.00,108.00 75.36,117.00 75.36,117.00  
    36.              75.36,117.00 74.41,128.00 74.41,128.00  
    37.              74.41,128.00 71.00,151.00 71.00,151.00  
    38.              75.21,149.77 88.51,142.63 93.00,140.14  
    39.              96.31,138.31 100.02,135.40 104.00,136.17  
    40.              104.00,136.17 135.00,151.00 135.00,151.00  
    41.              135.00,151.00 130.75,127.00 130.75,127.00  
    42.              130.75,127.00 130.11,116.00 130.11,116.00  
    43.              130.11,116.00 137.00,108.00 137.00,108.00  
    44.              137.00,108.00 155.00,90.00 155.00,90.00  
    45.              155.00,90.00 130.00,86.73 130.00,86.73  
    46.              130.00,86.73 119.39,84.15 119.39,84.15  
    47.              119.39,84.15 113.14,73.00 113.14,73.00  
    48.              113.14,73.00 103.00,53.00 103.00,53.00  
    49.              99.46,57.45 94.06,69.34 91.25,75.00 Z” />  
    50. </vector>  
    <?xml version="1.0" encoding="utf-8"?>
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="200dp"
        android:height="200dp"
        android:viewportHeight="200"
        android:viewportWidth="200">
            <path
                android:fillColor="#00BB9C"
                android:pathData="M 120.25,62.00
               C 121.56,64.62 124.79,71.70 126.63,73.44
                 129.39,76.04 138.05,76.58 142.00,77.13
                 142.00,77.13 178.00,82.00 178.00,82.00
                 174.13,90.18 158.14,103.06 150.99,110.04
                 148.73,112.24 143.25,117.35 142.11,120.00
                 140.83,122.98 142.80,131.54 143.42,135.00
                 143.42,135.00 150.00,171.00 150.00,171.00
                 142.14,169.53 124.31,158.91 116.00,154.75
                 113.30,153.40 105.49,149.02 103.00,148.88
                 99.54,148.69 91.24,153.75 88.00,155.58
                 81.75,159.10 61.89,169.90 56.00,171.00
                 56.00,171.00 61.80,136.00 61.80,136.00
                 61.80,136.00 63.60,120.58 63.60,120.58
                 63.60,120.58 53.00,109.00 53.00,109.00
                 53.00,109.00 27.00,82.00 27.00,82.00
                 27.00,82.00 63.00,77.13 63.00,77.13
                 67.09,76.56 75.64,76.00 78.61,73.44
                 81.01,71.37 84.64,63.21 86.25,60.00
                 86.25,60.00 102.00,28.00 102.00,28.00
                 106.72,32.72 116.58,54.66 120.25,62.00 Z
               M 91.25,75.00
               C 90.14,77.23 88.11,81.97 86.51,83.57
                 83.98,86.09 79.42,86.24 76.00,86.73
                 76.00,86.73 51.00,90.00 51.00,90.00
                 51.00,90.00 68.00,108.00 68.00,108.00
                 68.00,108.00 75.36,117.00 75.36,117.00
                 75.36,117.00 74.41,128.00 74.41,128.00
                 74.41,128.00 71.00,151.00 71.00,151.00
                 75.21,149.77 88.51,142.63 93.00,140.14
                 96.31,138.31 100.02,135.40 104.00,136.17
                 104.00,136.17 135.00,151.00 135.00,151.00
                 135.00,151.00 130.75,127.00 130.75,127.00
                 130.75,127.00 130.11,116.00 130.11,116.00
                 130.11,116.00 137.00,108.00 137.00,108.00
                 137.00,108.00 155.00,90.00 155.00,90.00
                 155.00,90.00 130.00,86.73 130.00,86.73
                 130.00,86.73 119.39,84.15 119.39,84.15
                 119.39,84.15 113.14,73.00 113.14,73.00
                 113.14,73.00 103.00,53.00 103.00,53.00
                 99.46,57.45 94.06,69.34 91.25,75.00 Z" />
    </vector>


    8、最后在布局文件里使用:

    1. <?xml version=“1.0” encoding=“utf-8”?>  
    2. <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android  
    3.     xmlns:tools=http://schemas.android.com/tools  
    4.     android:layout_width=“match_parent”  
    5.     android:layout_height=“match_parent”  
    6.     android:paddingBottom=“@dimen/activity_vertical_margin”  
    7.     android:paddingLeft=“@dimen/activity_horizontal_margin”  
    8.     android:paddingRight=“@dimen/activity_horizontal_margin”  
    9.     android:paddingTop=“@dimen/activity_vertical_margin”  
    10.     tools:context=“com.shenhua.vectordemo.MainActivity”>  
    11.   
    12.     <TextView  
    13.         android:layout_width=“wrap_content”  
    14.         android:layout_height=“wrap_content”  
    15.         android:text=“Hello World!” />  
    16.   
    17.     <ImageView  
    18.         android:layout_width=“wrap_content”  
    19.         android:layout_height=“wrap_content”  
    20.         android:layout_centerInParent=“true”  
    21.         android:src=“@drawable/star_path” />  
    22. </RelativeLayout>  
    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.shenhua.vectordemo.MainActivity">

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" /&gt;
    
    &lt;ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/star_path" /&gt;
    

    </RelativeLayout>
    目录结构:


    OK,就是如此简单。


    猜你喜欢

    转载自blog.csdn.net/wzgbgz/article/details/79386211