android vector矢量图的简单使用教程

一、需要准备工作来支持:

1.需要android-support-library-23.2以上的库支持。

这里我直接使用了25的。

 compile 'com.android.support:appcompat-v7:25.1.1'
 compile 'com.android.support:support-vector-drawable:25.1.1'

2.版本< API 21时需要支持的话:

 static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

这个我是放在自己的Application里面的,文档有说:

Please note: this only takes effect in Activities created after this call

二、代码

1.一个静态的vector:

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

    <group android:name="left">
        <path
            android:fillColor="#FF000000"
            android:pathData="M9.01,14L2,14v2h7.01v3L13,15l-3.99,-4v3"/>
    </group>

    <group android:name="right">
        <path
            android:fillColor="#FF000000"
            android:pathData="M14.99,13v-3L22,10L22,8h-7.01L14.99,5L11,9l3.99,4"/>
    </group>

</vector>

2.放在animator文件夹里面的两个动画文件:

anim_left:

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


anim_right:

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

3.测试的动画文件testanimatedvector.xml,放在drawable下面:

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

    <target
        android:name="left"
        android:animation="@animator/anim_left"/>

    <target
        android:name="right"
        android:animation="@animator/anim_right"/>

</animated-vector>

注意:animated-vector 会报错,但不影响编译。


3.xml中使用:

 <ImageView
            android:id="@+id/message"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/testanimatedvector"
            />

4.代码中启动动画:

  mImageView = (ImageView) findViewById(R.id.imageview);

        AnimatedVectorDrawableCompat animatedVectorDrawableCompat = AnimatedVectorDrawableCompat.create(
                this, R.drawable.testanimatedvector
        );

        if(animatedVectorDrawableCompat!=null) {
            mImageView.setImageDrawable(animatedVectorDrawableCompat);
            ((Animatable) mImageView.getDrawable()).start();
        }

转载注明:http://blog.csdn.net/u014614038/article/details/75014652
参考来源:http://www.jianshu.com/p/e3614e7abc03

另外:
svg在线编辑器:http://editor.method.ac/
svg转化为Android Vector Drawable在线工具:http://inloop.github.io/svg2android/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值