android svg矢量图,二变X,dome

 

 

1、先看看效果



  首先activity里面写一个ImageView

imageView1 = (ImageView)findViewById(R.id.image1);
        imageView1.setImageDrawable(getDrawable(R.drawable.svg_animated_vector));
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //启动动画效果
                final Drawable drawable = imageView1.getDrawable();
                if (drawable instanceof Animatable){
                    ((Animatable) drawable).start();
                }
            }
        });
        
<ImageView
        android:id="@+id/image1"
        android:layout_width="400dp"
        android:layout_height="400dp"/>

2、再看svg_animated_vector文件,写入drawable文件夹,此xml是连接svg图片和动画的
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/svg_vector">
    <target
        android:name="path1"
        android:animation="@animator/svg_objectanimator1" />
    <target
        android:name="path2"
        android:animation="@animator/svg_objectanimator2" />
    <target
        android:name="path3"
        android:animation="@animator/svg_objectanimator3" />
    <target
        android:name="path4"
        android:animation="@animator/svg_objectanimator4" />
</animated-vector>

3、svg_vector文件,仍然写在drawable文件夹
画了四条线,方便从中间折断的效果
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="200dp"
    android:height="200dp"
    android:viewportHeight="100"
    android:viewportWidth="100">
    <group>
        <path
            android:name="path1"
            android:fillColor="#0000"
            android:pathData="M20,20L50,20"//M20,20表示画笔移到坐标20,20处。L50,20表示从20,20处连线到50,20处
            android:strokeColor="@color/colorred"
            android:strokeLineCap="round"
            android:strokeWidth="5" />
        <path
            android:name="path2"
            android:fillColor="#0000"
            android:pathData="M50,20L80,20"
            android:strokeColor="@color/colorred"
            android:strokeLineCap="round"
            android:strokeWidth="5" />
        <path
            android:name="path3"
            android:fillColor="#0000"
            android:pathData="M20,80L50,80"
            android:strokeColor="@color/colorred"
            android:strokeLineCap="round"
            android:strokeWidth="5" />
        <path
            android:name="path4"
            android:fillColor="#0000"
            android:pathData="M50,80L80,80"
            android:strokeColor="@color/colorred"
            android:strokeLineCap="round"
            android:strokeWidth="5" />
    </group>
</vector>

4、再看动画效果,四根线段都要移动。
svg_objectanimator1文件,写在animator文件夹里面,有些res没有这个文件夹,自己创建一个。专门用来放属性动画的。
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="4000"
    android:propertyName="pathData"
    android:valueFrom="M20,20L50,20"//原始的坐标
    android:valueTo="M20,20L50,50"//移动后的坐标
    android:valueType="pathType">
</objectAnimator>

svg_objectanimator2文件
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="4000"
    android:propertyName="pathData"
    android:valueFrom="M50,20L80,20"
    android:valueTo="M50,50L80,20"
    android:valueType="pathType">
</objectAnimator>

svg_objectanimator3文件
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="4000"
    android:propertyName="pathData"
    android:valueFrom="M20,80L50,80"
    android:valueTo="M20,80L50,50"
    android:valueType="pathType">
</objectAnimator>

svg_objectanimator4文件
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="4000"
    android:propertyName="pathData"
    android:valueFrom="M50,80L80,80"
    android:valueTo="M50,50L80,80"
    android:valueType="pathType">
</objectAnimator>

5、到此为止,完成了这个svg图。点击ImageView会执行动画效果。
这只是很简单的一个动画,根据不同的形状可以绘制很多复杂的动画,有时间可以探索探索。。。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Vue中使用SVG矢量图,你可以按照以下步骤进行配置: 1. 首先,确保你的项目中有一个存放SVG图标的文件夹,比如`src/icons`。在这个文件夹中,你可以存放所有的SVG图标文件。 2. 在你的组件中,使用`<svg-icon>`组件来引用SVG图标。可以通过设置`icon-class`属性来指定SVG图标的名称,通过设置`className`属性来修改SVG图标的样式,比如大小和颜色。例如,`<svg-icon icon-class="tree" className="需要修改的样式" />`。 3. 配置`vue.config.js`文件,添加以下代码片段到`chainWebpack`方法中: ```javascript chainWebpack(config) { config.module .rule('svg') .exclude.add(resolve('src/icons')) // 注意svg的存储地址 .end() config.module .rule('icons') .test(/\.svg$/) .include.add(resolve('src/icons')) // 注意svg的存储地址 .end() .use('svg-sprite-loader') .loader('svg-sprite-loader') .options({ symbolId: 'icon-[name]' }) .end() } ``` 这段代码的作用是配置Webpack,使其能够正确地加载和处理SVG图标。其中,`exclude`和`include`方法用于指定SVG图标文件的存放地址,`svg-sprite-loader`用于加载和处理SVG图标文件,并通过`symbolId`选项设置图标的ID。 通过以上配置,你就可以在Vue项目中使用SVG矢量图了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [vue中如何使用svg](https://blog.csdn.net/weixin_47909202/article/details/123015710)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值