矢量图VectorDrawable

配置

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.ha.testvector"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary=true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

searchbar.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="150dp"
        android:height="24dp"
        android:viewportHeight="24"
        android:viewportWidth="150">

    <path
        android:name="search"
        android:pathData="M141,17 A9,9 0 1,1 142,16 L149,23"
        android:strokeAlpha="0.8"
        android:strokeColor="#000000"
        android:strokeLineCap="round"
        android:strokeWidth="2"/>

    <path
        android:name="bar"
        android:pathData="M0,23 L149,23"
        android:strokeAlpha="0.8"
        android:strokeColor="#000000"
        android:strokeLineCap="square"
        android:strokeWidth="2"/>
</vector>

bar动画文件

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
    xmlns:androd="http://schemas.android.com/apk/res/android"
    androd:duration="1000"
    androd:propertyName="trimPathStart"
    androd:repeatCount="infinite"
    androd:repeatMode="reverse"
    androd:valueFrom="0"
    androd:valueTo="1"
    androd:valueType="floatType">

</objectAnimator>

search动画文件

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
    xmlns:androd="http://schemas.android.com/apk/res/android"
    androd:duration="1000"
    androd:propertyName="trimPathEnd"
    androd:repeatCount="infinite"
    androd:repeatMode="reverse"
    androd:valueFrom="0"
    androd:valueTo="1"
    androd:valueType="floatType">

</objectAnimator>

动画粘合剂文件

<?xml version="1.0" encoding="utf-8"?>
<animated-vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/searchbar">

    <target
        android:animation="@animator/anim_search"
        android:name="search"/>

    <target
        android:animation="@animator/anim_bar"
        android:name="bar"/>

</animated-vector>

布局文件

        <ImageView
            android:onClick="anim"
            app:srcCompat="@drawable/searchbar_anim"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 如果您要在 ImageView 中显示矢量图,可以使用 VectorDrawableCompat 类。VectorDrawableCompat 允许您在 Android 2.1(API 7) 及更高版本上显示矢量图。以下是一个示例: 1. 首先,在您的项目中添加以下依赖项: ``` implementation 'com.android.support:support-vector-drawable:28.0.0' ``` 2. 在您的布局文件中,将 ImageView 的 src 属性设置为一个矢量图的资源 ID。例如: ``` <ImageView android:id="@+id/my_image_view" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/my_vector_drawable" /> ``` 3. 在您的 Java 代码中,使用 VectorDrawableCompat 类来获取 Drawable 并将其设置为 ImageView 的 src。例如: ``` ImageView imageView = findViewById(R.id.my_image_view); Drawable drawable = VectorDrawableCompat.create(getResources(), R.drawable.my_vector_drawable, null); imageView.setImageDrawable(drawable); ``` 这样就可以在 ImageView 中显示矢量图,并兼容 Android 2.1 及更高版本。 ### 回答2: ImageView对矢量图的兼容性是相对较好的。矢量图是基于数学算法来描述图形的,不依赖于具体的像素点,因此具有无限的可伸缩性和精确性。而传统的位图则是由像素点组成的,放大时容易失真。因此,对于需要放大或缩小的图像,使用矢量图更加合适。 在Android开发中,我们可以使用VectorDrawable类来表示矢量图形。ImageView可以加载并显示VectorDrawable对象。在支持矢量图的Android版本中,可以直接在ImageView中使用VectorDrawable,无需做任何兼容处理。 然而,对于一些低版本的Android系统(例如API级别低于21),直接加载VectorDrawable可能会导致崩溃或显示异常。为了兼容这些低版本系统,可以使用AppCompat库中的VectorDrawableCompat类来替代VectorDrawableVectorDrawableCompat类提供了对矢量图兼容处理的支持。我们可以使用AppCompatImageView来加载VectorDrawableCompat对象,确保在低版本Android系统上也能正常显示矢量图。同时,还可以使用VectorDrawableCompat类提供的兼容方法来设置矢量图的颜色、透明度等属性。 总结来说,ImageView对矢量图的兼容性较好。可以直接使用VectorDrawable来显示矢量图,对于低版本的Android系统,可以使用VectorDrawableCompat来进行兼容处理,确保矢量图在各个版本的Android系统上都能正常显示。 ### 回答3: ImageView是Android中常用的控件之一,用于显示图像。矢量图是一种使用数学公式描述图像的图形格式,相对于位图,它具有无限的可缩放性和较小的文件大小。在Android中,原生的ImageView并不直接支持显示矢量图,但可以通过一些方式使其兼容。 一种常见的方式是使用支持矢量图显示的第三方库,例如使用Android中开源的矢量图VectorDrawableCompat。该库可以将矢量图转换为兼容的位图进行显示,并且在不同版本的Android系统上表现一致。 另一种方式是将矢量图转换为位图进行显示。可以使用Android Studio中的“Vector Asset Studio”工具将矢量图转换为位图,然后将生成的位图设置给ImageView进行显示。这种方式的缺点是需要提前将矢量图转换为位图,且无法充分利用矢量图的可缩放性。 除了上述方式,还可以使用开源的矢量图库如SVG库,将矢量图文件转换为SVG格式,然后在ImageView中显示。不过,这种方式需要引入额外的库,并且可能需要一些额外的代码来实现。 总的来说,要实现ImageView的矢量图兼容,可以通过使用第三方库、转换矢量图为位图或使用SVG库等方式来实现。具体选择哪种方式视需求和项目情况而定。在选择方式时,需要考虑到文件大小、兼容性以及开发和维护的便利性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值