Android uses SVG resource files (1) and androidsvg

Source: Internet
Author: User

Android uses SVG resource files (1) and androidsvg

I will not introduce the background here. Let's talk about how to use SVG resources in Android!
Procedure:
1. source file of the SVG Resource
To use SVG resources, we must first have svg resources. Where can we find them? There are many tools that can generate SVG resources, such as the omnigraffle graphic editor. Here I have generated an image of SVG resources.

Open it in a browser and you will see its code.

What we need is the svg code after path, which is copied to our code.

M 6.783868 184 C 6.783868 184 7.349524 160.57912 14.131572 C 157.31086 19.783036 155.677 29.389906 150.7756 31.085964 C 149.68606 32.78275 148.59634 32.78093 143.14954 32.78093 C 143.69422 32.78093 144.23854 20.34669 156.22168 C 11.870586 156.22168 29.9572 140.971 C 27.130012 136.06888 27.130012 136.06888 C 27.130012 136.06888 12.434786 147.50698 9.04267 146.41762 C 24.302642 132.80062 26.565084 126.26464 26.565084 C 126.26464 26.565084 126.26464 7.349524 0 136.06888 C 132.25594 17.523324 127.35418 22.044022 88.13767 22.044022 C 88.13767 22.044022 88.13767 25.998882 52.18906 C 28.82607 45.6529 29.9572-39.66178 41.26122 4.80244 C 141.86809 11.88328 134.52148 75.06544 134.52148 C 76.69959 134.52148 78.333574 135.65042 76.15482 135.65042 C 81.056866 135.65042 85.95886 133.3898 88.13767 133.3898 C 88.13767 133.3898 88.13767 135.65133 93.584493 135.65133 C 102.84378 133.955276 101.20983 133.3898 98.486374 C 132.259764 100.12038 131.69538 106.656376 130.0006 105.022426 126.6083 117.54976 C 126.041916 124.0861 133.955276 128.98804 136.78028 C 128.98804 131.12918 131.16676 140.1713 132.80062 140.1713 C 132.80062 140.1713 132.80062 159.38686 135.52402 166.17091 C 139.88164 172.95351 144.23908 181.43398 169.29364 181.99854 C 182.91064 182.56402 183.99982 6.783868 184 Z
By now, our svg resources are ready, and the next step is to use them in the code.

2. Compile the vector drawable resource file.

<?xml version="1.0" encoding="utf-8"?><vector xmlns:android="http://schemas.android.com/apk/res/android"    android:width="600dp"    android:height="600dp"    android:viewportHeight="512"    android:viewportWidth="512">    <path android:fillColor="#410a0a0a"        android:pathData="@string/pretty_girl"/></vector>

These attributes are explained here;
Android: width = "600dp" the actual display width is 600dp
Android: height = "600dp" the actual height displayed is 600dp.
Android: viewportHeight = "512" the width of the vector is 512, which has no unit;
Android: viewportWidth = "512" vector height: 512
In fact, the above two attributes are the width and height of the svg image.

Android: pathData = "@ string/pretty_girl", that is, the path of the image content. The value in the path generally does not exceed the value of viewportHeight and viewportWidth. In this case, it will not exceed 512.

3. Use it. Like other drawable resource files, you can directly set the View background or the src resource of ImageView.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/id_img"        android:layout_centerInParent="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/pretty_girl"/></RelativeLayout>

After running:

This is a success! However, this effect is only supported by Android5.0 and later versions. Therefore, it does not make much sense to study it. At present, it does not make much sense! So I was wondering, can a lower version of the system support SVG vector images? The answer is yes. Next Android uses SVG resource files (II) to explain how to use SVG files in earlier versions. If you are interested in SVG, you can continue to pay attention to them.

No code is pasted here, because it can only be run in systems above android.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.