代码之家  ›  专栏  ›  技术社区  ›  Dan Lew

Android中的矢量图形

  •  33
  • Dan Lew  · 技术社区  · 14 年前

    我正在开发一个应用程序,显示其他人的图像数据库。它们所拥有的图像都是矢量图形,可以转换成任何格式,但是保持矢量格式是很好的,因为用户可能希望放大。

    问题是,Android中是否有一种内置的方式来显示矢量图形?格式无关紧要-我们可以转换。我们考虑的当前格式是PDF,但是考虑到没有本地的PDF支持,我必须做一些非常复杂的事情来让它工作(例如,集成 poppler 通过NDK进入我的应用程序)。另一种方法是将矢量图形转换为更简单的格式(JPG,GIF),但我宁愿避免这种情况。

    6 回复  |  直到 14 年前
        1
  •  21
  •   rekire    12 年前

    退房 svg-android -这是一个相对较新的库,它只支持SVG Basic,但它是用来绘制Androidify的库。主页上有一些例子说明了如何从SVG中获取一个Drawable,sems就是您要寻找的对象。

        2
  •  7
  •   C--    10 年前

    Creating Vector Drawables.


    我知道这个问题已经过时了,但是我遇到了同样的需求,我很高兴地了解到Android 5.0现在支持向量可抽取。你可以用 <vector>

    <!-- res/drawable/heart.xml -->
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:height="256dp"
        android:width="256dp"
        android:viewportWidth="32"
        android:viewportHeight="32">
    
        <!-- draw a path -->
        <path android:fillColor="#8fff"
            android:pathData="M20.5,9.5
                        c-1.955,0,-3.83,1.268,-4.5,3
                        c-0.67,-1.732,-2.547,-3,-4.5,-3
                        C8.957,9.5,7,11.432,7,14
                        c0,3.53,3.793,6.257,9,11.5
                        c5.207,-5.242,9,-7.97,9,-11.5
                        C25,11.432,23.043,9.5,20.5,9.5z" />
    </vector>
    

    到目前为止,我面临的唯一问题是,它没有包含在支持libs中,因此您不能在较低的api中使用它们。奇妙的是,你甚至可以动画矢量绘图现在。这是一个例子。

    <!-- res/drawable/vectordrawable.xml -->
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:height="64dp"
        android:width="64dp"
        android:viewportHeight="600"
        android:viewportWidth="600">
        <group
            android:name="rotationGroup"
            android:pivotX="300.0"
            android:pivotY="300.0"
            android:rotation="45.0" >
            <path
                android:name="v"
                android:fillColor="#000000"
                android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
        </group>
    </vector>
    

    阅读有关矢量可绘制动画的详细信息 here

        3
  •  4
  •   David Webb    14 年前

    TinyLive SVG 为Android提供SVG查看器。我还没试过,所以不知道有没有用。

    Reading this bug request 似乎SVG 可以 在浏览器中启用-因此推测 WebView 姜饼里也有。但由于这是未来版本中的一个可能特性,所以现在可能对您没有太大帮助。

        4
  •  3
  •   A-student    6 年前

    看看 BetterVectorDrawable SVG to VectorDrawable Converter

    更好的可拉伸性 是Android 4.0+的VectorDrawable实现,在Android 5.0+上具有可配置的回退行为。

    是SVG图像到AndroidVectorDrawable XML资源文件的批处理转换器。联机版本是 here .

    链接指向readmes,它提供了关于如何使用lib和转换器的足够信息。

        5
  •  0
  •   Mapsy    8 年前

    无耻的自我封闭,但也许你会感兴趣 ribbon-vg

        6
  •  0
  •   Vempati Satya Suryanarayana    7 年前

    这个问题太老了。但希望我的回答能帮助未来的游客。

    我们将矢量可绘制文件放在可绘制文件夹中。AnimatedVectorDrawable也是一个drawable。因此,我们也将这些文件放在可提取文件夹中。下面是这两个例子:

        <vector 
    
            xmlns:android="http://schemas.android.com/apk/res/android"
                    android:width="1536dp"
                    android:height="864dp"
                    android:viewportWidth="1536.0"
                    android:viewportHeight="864.0">
    
                <path
                    android:name="my_vector"
                    android:pathData="M  162   8
                    q    -07    00   -41    26
                    q    -34    27   -50    64
                    q    -25    59   -19   117
                    q     07    70    53   121
                    q     57    63   151    62
                    q     87   -01   140   -66
                    q     46   -55    48  -142
                    q     01   -56   -34  -105
                    q    -38   -52   -77   -70
                    l    -29   -11
                    q     16   -01    31   -02
                    q     59   -01   119   -02 "
    
                    android:strokeLineCap="round"
                    android:strokeColor="#f00f"
                    android:fillColor="#00000000"
                    android:strokeWidth="32"
                    android:trimPathEnd="0"/>
    
    
    
            </vector>
    

    avd_omega_animator.xml格式

    <animated-vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt"
    android:drawable="@drawable/vd_number8">
    
        <target
            android:name="my_vector"
            android:animation="@animator/trimpath_animator"/>       
    
    
    
    </animated-vector>
    

    然后,可以使用如下动画制作程序文件制作动画:

    trimpath_animator.xml

    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
    <objectAnimator
    android:propertyName="trimPathEnd"
    android:duration="1000"
    android:valueFrom="0"
    android:valueTo="1"
    android:repeatCount="0"
    android:repeatMode="reverse"/>
    </set>
    

    在布局文件中

    活动\主.xml

    <RelativeLayout
        xmlna:android="http://schemasandroid.com/apk/res/android"
        xmlna:app="http://schemasandroid.com/apk/res-auto"
        xmls:tools="http:schemas.android.com/tools"
        android:id="@+some_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MainActivity">
    
        <ImageView
        android:id="@+id/my_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/avd_omega_animator"/>
    
    </RelativeLayout>
    

    在活动中,在onCreate中编写以下代码:

    导入。。。

    public class MyActivity extends Activity{
    
    ImageView myImage;
    
    
    ...
    ...
    setContentView(R.layout.activity_main);
    myImage = (ImageView)findViewById(R.id.my_image);
    Drawable d = myImage.getDrawable():
    if(d instance of Animatable){
    d.start;
    }
    

    看看好玩的地方。

    正如我在上面使用的getDrawable一样,您也可以使用其他方法在ImageView中放置drawables,比如setImageDrawable(“d”)等。

    https://www.androiddesignpatterns.com/2016/11/introduction-to-icon-animation-techniques.html

    好的例子。

    希望我给了你有用的答案。