Java 如何为android创建矢量绘图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35401968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create vector drawables for android?
提问by theUturn
I am new to VectorDrawables
.
我是新手VectorDrawables
。
I can see that the default vector drawables
provided with android studio like ic_menu_gallery
, ic_menu_camera
, etc. are working great. So I tried to create my own vector drawables
by converting my png images
to svg
first and using the path and fill values to make vector drawables i.e replaced the android:pathData
for d and android:fillColor
for fill tag in svg files. It somehow gave vector drawables but distorted or currputed looking.
我可以看到,默认vector drawables
提供了像Android这样的工作室ic_menu_gallery
,ic_menu_camera
等都是伟大的工作。因此,我尝试vector drawables
通过将 my 转换png images
为svg
第一个并使用路径和填充值来制作矢量可绘制对象来创建自己的对象,即替换svg 文件中的android:pathData
for d 和android:fillColor
for fill 标签。它以某种方式提供了矢量可绘制对象,但看起来扭曲或粗糙。
If I am not taking the right approach please suggest me.
如果我没有采取正确的方法,请建议我。
采纳答案by Ahmad Aghazadeh
You can use Android Studio.
Convert png to svg file online tools PNG to SVG
您可以使用 Android Studio。将png转svg文件在线工具PNG转SVG
For SVG color can use :
对于 SVG 颜色可以使用:
<ImageButton
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/button"
android:src="@drawable/ic_more_vert_24dp"
android:tint="@color/primary" />
Or
或者
imageView.setColorFilter(getResources().getColor(android.R.color.black), PorterDuff.Mode.SRC_IN);
Can convert SVG to PNG online tool: coolutils
可以将SVG转PNG的在线工具: coolutils
Convert SVG to Android drawable: inloop
将 SVG 转换为 Android drawable: inloop
回答by Cowgirl
you can create your own svg image or icon using photoshop/illustrator and can import vector asset (available in android studio 2.+). some times importing it results distorted image if svg is complex or has many elements then you can use it as single single element as vector assets by overlapping ImageViews.
您可以使用 photoshop/illustrator 创建自己的 svg 图像或图标,并且可以导入矢量资源(在 android studio 2.+ 中可用)。有时,如果 svg 很复杂或有很多元素,导入它会导致图像失真,那么您可以通过重叠 ImageViews 将其用作单个元素作为矢量资产。
回答by suku
Here is the best method to convert any png/jpg into vector drawable:
这是将任何 png/jpg 转换为可绘制矢量的最佳方法:
Download the software InkScape
Open your png in it and follow the procedure shown in the videoto convert it to svg
The use the SVG to Vector Drawable toolto convert the svg to vector drawable. The other tool svg2android does not always work as sometimes the svg gets improperly converted to a vector drawable
Copy the code into a new drawable resource file. Now this can be used as a regular drawable.
下载软件InkScape
在其中打开您的 png 并按照视频中显示的步骤将其转换为 svg
使用SVG to Vector Drawable 工具将 svg 转换为矢量可绘制对象。另一个工具 svg2android 并不总是有效,因为有时 svg 会被错误地转换为可绘制的矢量
将代码复制到一个新的可绘制资源文件中。现在这可以用作常规可绘制对象。
If you already have a svg then start straightaway from step 3.
如果您已经有了 svg,则直接从第 3 步开始。
回答by Annemarie Rinyu
I had this problem too and found this very useful website:
我也有这个问题,发现这个非常有用的网站:
https://materialdesignicons.com/
https://materialdesignicons.com/
There are many icons and with the "advanced export" you can edit the settings easily. For me it was the fastest and easiest way to create vector drawable, if you don't want to download the file, you can just view the code and copy it, not only as a vector drawable but even as an SVG or XAML. Oh and it's free:)
有许多图标,通过“高级导出”,您可以轻松编辑设置。对我来说,这是创建可绘制矢量的最快和最简单的方法,如果您不想下载文件,您可以查看代码并复制它,不仅可以作为矢量可绘制,甚至可以作为 SVG 或 XAML。哦,它是免费的:)
- Pick an icon
- Click on Advanced Export
- Edit the settings
- Click on "View Vector Drawable" or download it and put it in your project:)
- 选择一个图标
- 点击高级导出
- 编辑设置
- 单击“View Vector Drawable”或下载它并将其放入您的项目中:)
回答by live-love
In Android Studio 3.1:
在 Android Studio 3.1 中:
File -> New -> Vector Asset
文件 -> 新建 -> 矢量资源
Asset Type: Select Local File
资产类型:选择本地文件
Click on the path to choose a .svg or a .psd file
单击路径以选择 .svg 或 .psd 文件
If you don't have an image, go to Google Images, Advanced Image Search. Use file Type: SVG files
如果您没有图片,请转至 Google 图片、高级图片搜索。使用文件类型:SVG 文件
回答by kunal
Step 1The first step is to convert it into SVG format - For black and white SVG convertor there are many tools but there are very few good tools for coloured conversion. https://www.autotracer.org/is one of the best and it worked for me. Convert and download the SVG file. Step 2- In the ' Android Studio ' Go to drawables folder and right click to select New - Vector Asset
步骤1第一步是将其转换为SVG格式 - 对于黑白SVG转换器,有很多工具,但很少有用于彩色转换的好工具。https://www.autotracer.org/是最好的之一,它对我有用。转换并下载 SVG 文件。 第 2 步- 在“Android Studio”中,转到 drawables 文件夹并右键单击以选择 New - Vector Asset
Step 3- Choose the downloaded SVG file from Step 1. The android Studio
第 3 步- 从第 1 步中选择下载的 SVG 文件。Android Studio
Step 4- Dont forget to set navigationView.setItemIconTintList(null); as null in case the coloured icons are being used in a navigation drawer/view.
第 4 步- 不要忘记设置 navigationView.setItemIconTintList(null); 如果在导航抽屉/视图中使用彩色图标,则为 null。
Cheers!!
干杯!!