Android 中的矢量图形

发布于 2024-09-29 03:26:11 字数 322 浏览 3 评论 0 原文

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

问题是,Android中有没有内置的方法来显示矢量图形?格式并不重要——我们可以转换。我们当前考虑的格式是 PDF,但鉴于没有原生 PDF 支持,我必须做一些相当复杂的事情才能使其正常工作(例如,集成 poppler 通过 NDK 进入我的应用程序)。另一种方法是将矢量图形转换为更简单的格式(JPG、GIF),但我宁愿避免这样做。

I'm working on an application that displays someone else's database of images. The images they have are all vector graphics and can be converted to any format, however keeping them in a vector format is good because users will probably want to zoom in closely.

The question is, is there a built-in way to display a vector graphic in Android? The format doesn't matter - we can convert. The current format we're considering is PDF, but given that there's no native PDF support, I'd have to do something pretty complex just to get it working (for example, integrating poppler into my app via the NDK). The alternative is to just convert the vector graphics into a simpler format (JPG, GIF) but I'd rather avoid that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情深已缘浅 2024-10-06 03:26:11

查看 svg-android - 这是一个相对较新的库,仅支持 SVG Basic,但它是用于绘制 Androidify 的库。主页上有一些关于如何从 SVG 中获取 Drawable 的示例,这似乎正是您正在寻找的内容。

Check out svg-android - it's a relatively new library and it only supports SVG Basic, but it's the library used to draw Androidify. There are examples on the homepage on how to get a Drawable from an SVG which sems to be what you're looking for.

晨曦慕雪 2024-10-06 03:26:11

创建矢量绘图。


我知道这个问题已经很旧了,但我遇到了同样的要求,我很高兴得知 Android 5.0 现在支持矢量绘图。您可以使用 标签和路径数据来创建矢量绘图,它在 API-21 上就像一个魅力。这是一个生成心形矢量图像的示例。

<!-- 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>

到目前为止,我遇到的唯一问题是,它没有包含在支持库中,因此您无法在较低的 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>

此处了解有关矢量可绘制动画的更多信息。

Creating Vector Drawables.


I know this question is old, but I came across this same requirement and I'm happy to learn that Android 5.0 supports vector drawables now. You can use <vector> tag and path data to create vector drawables and it works like a charm on API-21. Here is an example that produces a vector image in the shape of a heart.

<!-- 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>

The only problem I faced so far is that, it is not included in the support libs, and so you cannot make use of them in lower APIs. The wonderful thing is that, you can even animate vector drawables now. Here is an example.

<!-- 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>

Read more about vector drawable animation here.

暖树树初阳… 2024-10-06 03:26:11

TinyLive SVG 提供适用于 Android 的 SVG 查看器。我没试过,所以不知道好不好。

阅读此错误请求看来 SVG 可能< /em> 可以在浏览器中启用 - 因此大概也可以在 Gingerbread 中启用 WebView。但由于这是未来版本中可能出现的功能,因此现在可能对您没有太大帮助。

TinyLive SVG provides an SVG viewer for Android. I haven't tried it, so I've no idea if it's any good or not.

Reading this bug request it seems that SVG might be enabled in the Browser - and so presumably WebView too - in Gingerbread. But since this is a possible feature in future release it probably isn't much help to you now.

暗喜 2024-10-06 03:26:11

查看 BetterVectorDrawable 库以及 SVG 到 VectorDrawable 转换器

BetterVectorDrawable 是 Android 4.0+ 的 VectorDrawable 实现,在 Android 5.0+ 上具有可配置的回退行为。

SVG to VectorDrawable Converter 是将 SVG 图像批量转换为 Android VectorDrawable XML 资源文件。在线版本位于此处

链接指向自述文件,其中提供了有关如何使用库和转换器的足够信息。

Look at the BetterVectorDrawable lib together with the SVG to VectorDrawable Converter.

BetterVectorDrawable is the VectorDrawable implementation for Android 4.0+ with configurable fall-back behavior on Android 5.0+.

SVG to VectorDrawable Converter is the batch converter of SVG images to Android VectorDrawable XML resource files. Online version is here.

Links point to readmes, which provide enough information on how to use the lib and the converter.

梦初启 2024-10-06 03:26:11

一个无耻的自我插件,但也许你会对 ribbon-vg?它是基本的、纯 Java 的并且速度快;依靠尖端技术减轻渲染过程中CPU的负担。

A shameless self-plug, but maybe you'd be interested in ribbon-vg? It's basic, pure Java and fast; relying on cutting-edge techniques to unburden the CPU in the rendering process.

江南烟雨〆相思醉 2024-10-06 03:26:11

这个问题很老了。但希望我的回答对未来的游客有所帮助。

我们将 VectorDrawable 文件放在可绘制文件夹中。 AnimatedVectorDrawable 也是一个可绘制对象。因此,我们也将这些文件放在可绘制文件夹中。下面是这两个示例:

vd_omega.xml

    <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>

您可以在同一文件中为各种属性设置更多动画器。

在布局文件中的

activity_main.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中编写以下代码:

import ...
导入...

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 中放置可绘制对象,例如 setImageDrawable("d") 等。

您还可以参考 Alex Lockwood 先生的“图标动画技术简介”:

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

是很好的例子。

希望我给你有用的答案。

我上面给出的所有示例代码都有效。它也简单、直接。

This question is old. But hope my answer helps future visitors here.

We place VectorDrawable files in drawable folder. AnimatedVectorDrawable also is a drawable . Hence, We place these files also in drawable folder. Below are examples of the two :

vd_omega.xml

    <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>

Then you can animate using an animator file like following:

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>

You can have more animators in same file for various properties.

In your layout file

activity_main.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>

In your activity write the following code in the onCreate:

import ...
import ...

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;
}

and see the fun.

Just as I used getDrawable above you can also use other methods to place drawables in the ImageView like setImageDrawable("d") etc.

You can also refer to "Introduction to Icon animation techniques" of Mr. Alex Lockwood at:

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

for good examples.

Hope I gave you helpful answer.

All the example code which I gave above works. It is also simple and straight.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文