前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发之View动画

Android开发之View动画

作者头像
YungFan
发布2018-04-24 15:06:03
9320
发布2018-04-24 15:06:03
举报
文章被收录于专栏:学海无涯学海无涯
Android动画主要分为3种
  • View动画
  • 帧动画
  • 属性动画
何为View动画?

View动画主要是对View对象进行变换所达到的动画效果,如平移、缩放、旋转和透明度等,下面写个简单案例。

动画文件

首先在res目录下新建一个anim文件夹,然后新建4个动画文件,如下:

动画文件.PNG

然后在Activity布局中放入一张图片:

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/football"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/football" />
</RelativeLayout>
Activity
代码语言:javascript
复制
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = (ImageView) findViewById(R.id.football);

    }
1、平移动画
代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="3000" 
        android:fromXDelta="0"  //x的起始值
        android:fromYDelta="0"  //y的起始值
        android:toXDelta="400" //x的结束值
        android:toYDelta="400" /> //y的结束值

</set>

android:fromXDelta:x的起始值 android:toXDelta:x的结束值 android:fromYDelta:y的起始值 android:toYDelta:y的结束值

Activity代码

代码语言:javascript
复制
private void translateAnim() {
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translateanim);
        img.startAnimation(animation);
    }

测试运行

translate.gif

2、缩放动画
代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:duration="3000"
        android:fromXScale="0.5"
        android:fromYScale="0.5"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>

android:fromXScale:水平方向缩放的起始值 android:toXScale:水平方向缩放的结束值 android:fromYScale:垂直方向缩放的起始值 android:toYScale:垂直方向缩放的结束值

Activity代码

代码语言:javascript
复制
private void scaleAnim() {
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scaleanim);
        img.startAnimation(animation);
    }

测试运行

scale.gif

3、旋转动画
代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <rotate
        android:duration="3000"
        android:fromDegrees="0"
        android:toDegrees="360" />


</set>

android:fromDegrees:旋转开始的角度 android:toDegrees:旋转结束的角度

Activity代码

代码语言:javascript
复制
private void roteteAnim() {
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotateanim);
        img.startAnimation(animation);
    }

测试运行

rotate.gif

4、透明度动画
代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha
        android:duration="3000"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />

</set>

android:fromAlpha:起始透明度 android:toAlpha:结束透明度

Activity代码

代码语言:javascript
复制
private void alphaAnim() {
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alphaanim);
        img.startAnimation(animation);
    }

测试运行

alpha.gif

注意点

1、动画集合 <set xmlns:android="http://schemas.android.com/apk/res/android"> 中可以设置一些属性值,重要属性说明:

  • android:interpolator:动画集合插值器,主要影响动画的速度,默认为加速减速插值器,还有线性插值器、减速插值器等等
  • android:shareInterpolator:动画集合中的动画是否与几何共享同一个插值器
  • android:duration:动画集合执行时间
  • android:fillAfter:动画结束以后View是否停在结束位置,默认是false不停留,但是该属性需要设置在动画集合中才有效果,设在单独的动画中是无效的

2、View动画并没有真正改变View的位置,也就是说就算你看到了动画最终停留在了某个位置,它的真身还是在原来的位置,有点像神话小说的元神出窍,所以使用的时候要特别注意,如给Button设置点击事件,就会发现新位置的Button并不会出发click事件,原始位置却能响应,不知道原因的同学肯定入坑~~

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.04.11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Android动画主要分为3种
  • 何为View动画?
  • 动画文件
  • Activity
  • 1、平移动画
  • 2、缩放动画
  • 3、旋转动画
  • 4、透明度动画
  • 注意点
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档