Android Animation(动画)介绍

来源:互联网 发布:知喻经济发展讲座 编辑:程序博客网 时间:2024/05/16 23:49

Android中的常用动画分为帧动画(Drawable Animation)、View动画(补间动画)、属性动画。


1 帧动画


1.1 首先我们介绍一下帧动画(Drawable Animation).根据Android官方文档中对于帧动画的介绍就是加载一系列的图片资源。

Drawable animation lets you load a series of Drawable resources one after another to create an animation.

1.2 那么我们从官方文档中同样也获得了如何实现一个帧动画效果步骤:

[1] 在Android 项目中的res/目录下,创建drawable目录.

[2] 将要显示的图片放到res/drawable/

[3] 创建一个包含 <animation-list>元素的xml文件

[4] 使用代码通过xml文件将图片显示出来


1.3 具体的代码实现如下:

为了演示效果,我随便在网上找了9张图片,动画的效果不是很强。并且在res/drawable/下创建drawanim.xml的文件。具体代码如下:


1.3.1 MainActivity.java

package com.animationdemo;import android.graphics.drawable.AnimationDrawable;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.MotionEvent;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    AnimationDrawable rocketAnimation;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //查找到我们的ImageView组件        ImageView rocketImage = (ImageView) findViewById(R.id.iv);        //设置背景资源        rocketImage.setBackgroundResource(R.drawable.drawanim);        //获取AnimationDrawable类型        rocketAnimation = (AnimationDrawable) rocketImage.getBackground();    }    public boolean onTouchEvent(MotionEvent event) {        if (event.getAction() == MotionEvent.ACTION_DOWN) {            //当触摸屏被点击之后,启动动画            rocketAnimation.start();            return true;        }        return super.onTouchEvent(event);    }    @Override    protected void onStop() {        super.onStop();        //停止动画效果        rocketAnimation.stop();    }}

1.3.2 drawanim.xml

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false">    <!--oneshot为false表示如下的9张图片不断地循环播放,如果为true,表示只显示一次-->    <!--duration表示每张图片显示的时间-->    <item android:drawable="@drawable/image_1" android:duration="1000" />    <item android:drawable="@drawable/image_2" android:duration="1000" />    <item android:drawable="@drawable/image_3" android:duration="1000" />    <item android:drawable="@drawable/image_4" android:duration="1000" />    <item android:drawable="@drawable/image_5" android:duration="1000" />    <item android:drawable="@drawable/image_6" android:duration="1000" />    <item android:drawable="@drawable/image_7" android:duration="1000" />    <item android:drawable="@drawable/image_8" android:duration="1000" />    <item android:drawable="@drawable/image_9" android:duration="1000" /></animation-list>

1.3.3 activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"    tools:context="com.animationdemo.MainActivity">    <ImageView        android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/></LinearLayout>

1.3.4 Demo代码

如果你不愿意下载,从我上面的代码拷贝一下,然后自己随便添加几张图片就可以使用了。不愿意麻烦的就Drawable Animation Demo下载


2 View动画(补间动画)

补间动画又叫view动画,主要有一下几个动画的操作:

动画的创建有两种方式,通过代码的方式和通过布局的方式。下面针对这两种方式来实现一下具体的例子.
动画效果不会改变控件的实际坐标,就一个效果而已。

2.1 代码方式

创建项目MyViewAnimDemo,然后展现一下代码:
MainActivity.java
package com.myviewanimdemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    private Button btn_alpha;    private Button btn_romate;    private Button btn_scale;    private Button btn_translate;    private Button btn_all;    private ImageView iv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 找到关心的控件        btn_alpha = (Button) findViewById(R.id.btn_alpha);//透明        btn_romate = (Button) findViewById(R.id.btn_rotate);//旋转        btn_scale = (Button) findViewById(R.id.btn_scale);// 缩放        btn_translate = (Button) findViewById(R.id.btn_translate); // 平移        btn_all = (Button) findViewById(R.id.btn_all); // 所有效果        iv = (ImageView) findViewById(R.id.iv); //查找到图片        // 初始化控件        initView();    }    private void initView() {        btn_alpha.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 透明图片 1.0不透明,0.0透明                AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);                // 设置动画执行的时间                alphaAnimation.setDuration(2000);                // 设置重复的次数1+X,X就是此处设置的                alphaAnimation.setRepeatCount(2);                // 设置动画模式                alphaAnimation.setRepeatMode(Animation.REVERSE);                // 开始动画                iv.startAnimation(alphaAnimation);            }        });        btn_romate.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 按照默认的位置旋转                // RotateAnimation rotateAnimation = new RotateAnimation(0, 360);                // 旋转图片,按照中心旋转 0.5就是按照图片的宽或者高的1/2的位置                RotateAnimation rotateAnimation = new RotateAnimation(0, 360,                        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);                rotateAnimation.setDuration(2000);// 设置动画执行的时间                rotateAnimation.setRepeatCount(1);// 设置重复的次数1+X,X就是此处设置的                rotateAnimation.setRepeatMode(Animation.REVERSE);// 设置动画模式                //开启动画                iv.startAnimation(rotateAnimation);            }        });        btn_scale.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 缩放图片,按照自己的中心点,x、y轴各放大原来的两倍。                // 1.0f表示原大小。2.0f表示原来的两倍                ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f,                        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);                scaleAnimation.setDuration(2000);//设置执行的时间                // 设置缩放的模式                scaleAnimation.setRepeatMode(Animation.REVERSE);                scaleAnimation.setRepeatCount(3);// 设置缩放的次数                // 启动动画                iv.startAnimation(scaleAnimation);            }        });        btn_translate.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 平移图片,按照父窗体的长和宽的长度0.2倍的距离移动。                TranslateAnimation translateAnimation = new TranslateAnimation(                        Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT,0.2f,                        Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT,0.2f);                translateAnimation.setDuration(2000);// 设置执行时间                translateAnimation.setRepeatCount(4);// 设置重复的次数                // 设置重复模式                translateAnimation.setRepeatMode(Animation.REVERSE);                // 启动动画                iv.startAnimation(translateAnimation);            }        });        btn_all.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 1 所有效果一起执行                AnimationSet animationSet = new AnimationSet(true);                // 2 将之前所有的图片效果增加                // 2.1 透明图片 1.0不透明,0.0透明                AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);                // 设置动画执行的时间                alphaAnimation.setDuration(2000);                // 设置重复的次数1+X,X就是此处设置的                alphaAnimation.setRepeatCount(2);                // 设置动画模式                alphaAnimation.setRepeatMode(Animation.REVERSE);                // 2.2 旋转图片,按照中心旋转 0.5就是按照图片的宽或者高的1/2的位置                RotateAnimation rotateAnimation = new RotateAnimation(0, 360,                        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);                rotateAnimation.setDuration(2000);// 设置动画执行的时间                rotateAnimation.setRepeatCount(1);// 设置重复的次数1+X,X就是此处设置的                rotateAnimation.setRepeatMode(Animation.REVERSE);// 设置动画模式                // 2.3 缩放图片,按照自己的中心点,x、y轴各放大原来的两倍。                // 1.0f表示原大小。2.0f表示原来的两倍                ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f,                        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);                scaleAnimation.setDuration(2000);//设置执行的时间                // 设置缩放的模式                scaleAnimation.setRepeatMode(Animation.REVERSE);                scaleAnimation.setRepeatCount(3);// 设置缩放的次数                // 2.4 平移图片,按照父窗体的长和宽的长度0.2倍的距离移动。                TranslateAnimation translateAnimation = new TranslateAnimation(                        Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT,0.2f,                        Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT,0.2f);                translateAnimation.setDuration(2000);// 设置执行时间                translateAnimation.setRepeatCount(4);// 设置重复的次数                // 设置重复模式                translateAnimation.setRepeatMode(Animation.REVERSE);                // 3 添加动画合集                animationSet.addAnimation(scaleAnimation);                animationSet.addAnimation(rotateAnimation);                animationSet.addAnimation(translateAnimation);                animationSet.addAnimation(alphaAnimation);                // 4 启动动画                iv.startAnimation(animationSet);            }        });    }}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><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"    tools:context="com.myviewanimdemo.MainActivity">    <!--添加5个按钮和一张图片-->    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/btn_alpha"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="透明" />        <Button            android:id="@+id/btn_rotate"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="旋转" />        <Button            android:id="@+id/btn_scale"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="缩放" />        <Button            android:id="@+id/btn_translate"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="平移" />        <Button            android:id="@+id/btn_all"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="一起" />    </LinearLayout>    <ImageView        android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:src="@mipmap/ic_launcher" /></RelativeLayout>

2.2 使用xml设置动画效果

创建项目MyViewAnimByXmlDemo,展示代码如下.
MainActivity.java:
package com.myviewanimbyxmldemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    private Button btn_alpha;    private Button btn_romate;    private Button btn_scale;    private Button btn_translate;    private Button btn_all;    private ImageView iv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 找到关心的控件        btn_alpha = (Button) findViewById(R.id.btn_alpha);//透明        btn_romate = (Button) findViewById(R.id.btn_rotate);//旋转        btn_scale = (Button) findViewById(R.id.btn_scale);// 缩放        btn_translate = (Button) findViewById(R.id.btn_translate); // 平移        btn_all = (Button) findViewById(R.id.btn_all); // 所有效果        iv = (ImageView) findViewById(R.id.iv); //查找到图片        // 初始化控件        initView();    }    private void initView() {        btn_alpha.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 透明图片 1.0不透明,0.0透明                Animation alphaAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);                // 开始动画                iv.startAnimation(alphaAnimation);            }        });        btn_romate.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 旋转图片,按照中心旋转 0.5就是按照图片的宽或者高的1/2的位置                Animation rotateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);                //开启动画                iv.startAnimation(rotateAnimation);            }        });        btn_scale.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 缩放图片,按照自己的中心点,x、y轴各放大原来的两倍。                // 1.0f表示原大小。2.0f表示原来的两倍                Animation scaleAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale);                // 启动动画                iv.startAnimation(scaleAnimation);            }        });        btn_translate.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 平移图片,按照父窗体的长和宽的长度0.2倍的距离移动。                Animation translateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate);                // 启动动画                iv.startAnimation(translateAnimation);            }        });        btn_all.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 所有效果一起执行                Animation animationSet = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.set);                // 启动动画                iv.startAnimation(animationSet);            }        });    }}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><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"    tools:context="com.myviewanimdemo.MainActivity">    <!--添加5个按钮和一张图片-->    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/btn_alpha"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="透明" />        <Button            android:id="@+id/btn_rotate"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="旋转" />        <Button            android:id="@+id/btn_scale"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="缩放" />        <Button            android:id="@+id/btn_translate"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="平移" />        <Button            android:id="@+id/btn_all"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="一起" />    </LinearLayout>    <ImageView        android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:src="@mipmap/ic_launcher" /></RelativeLayout>
alpha.xml
<?xml version="1.0" encoding="utf-8"?><alpha    android:fromAlpha="1.0"    android:toAlpha="0.0"    android:duration="2000"    android:repeatCount="2"    android:repeatMode="reverse"    xmlns:android="http://schemas.android.com/apk/res/android"></alpha>

rotate.xml
<?xml version="1.0" encoding="utf-8"?><rotate    android:fromDegrees="0"    android:toDegrees="360"    android:pivotX="50%"    android:pivotY="50%"    android:duration="2000"    android:repeatCount="2"    android:repeatMode="reverse"    xmlns:android="http://schemas.android.com/apk/res/android"></rotate>

scale.xml
<?xml version="1.0" encoding="utf-8"?><scale    android:fromXScale="1.0"    android:toXScale="2.0"    android:fromYScale="1.0"    android:toYScale="2.0"    android:pivotX="50%"    android:pivotY="50%"    android:duration="2000"    android:repeatCount="2"    android:repeatMode="reverse"    xmlns:android="http://schemas.android.com/apk/res/android"></scale>

translate.xml
<?xml version="1.0" encoding="utf-8"?><translate    android:fromXDelta="0%p"    android:toXDelta="20%p"    android:fromYDelta="0%p"    android:toYDelta="20%p"    android:duration="2000"    android:repeatCount="2"    android:repeatMode="reverse"    xmlns:android="http://schemas.android.com/apk/res/android"></translate>

set.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:fromXDelta="0%p"        android:toXDelta="20%p"        android:fromYDelta="0%p"        android:toYDelta="20%p"        android:duration="2000"        android:repeatCount="2"        android:repeatMode="reverse"        xmlns:android="http://schemas.android.com/apk/res/android">    </translate>    <scale        android:fromXScale="1.0"        android:toXScale="2.0"        android:fromYScale="1.0"        android:toYScale="2.0"        android:pivotX="50%"        android:pivotY="50%"        android:duration="2000"        android:repeatCount="2"        android:repeatMode="reverse"        xmlns:android="http://schemas.android.com/apk/res/android">    </scale>    <rotate        android:fromDegrees="0"        android:toDegrees="360"        android:pivotX="50%"        android:pivotY="50%"        android:duration="2000"        android:repeatCount="2"        android:repeatMode="reverse"        xmlns:android="http://schemas.android.com/apk/res/android">    </rotate>    <alpha        android:fromAlpha="1.0"        android:toAlpha="0.0"        android:duration="2000"        android:repeatCount="2"        android:repeatMode="reverse"        xmlns:android="http://schemas.android.com/apk/res/android">    </alpha></set>

3 属性动画

会改变属性控件的真实坐标
实际开发中用的比较少。主要是用的ObjectAnimator类实现。同样的属性动画也有两种方式实现。代码和布局实现。

3.1 代码的方式
创建项目MyAttributesAnimDemo,代码如下:
MainActivity.java

package com.myattributesanimdemo;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    private Button btn_alpha;    private Button btn_romate;    private Button btn_scale;    private Button btn_translate;    private Button btn_all;    private ImageView iv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 找到关心的控件        btn_alpha = (Button) findViewById(R.id.btn_alpha);//透明        btn_romate = (Button) findViewById(R.id.btn_rotate);//旋转        btn_scale = (Button) findViewById(R.id.btn_scale);// 缩放        btn_translate = (Button) findViewById(R.id.btn_translate); // 平移        btn_all = (Button) findViewById(R.id.btn_all); // 所有效果        iv = (ImageView) findViewById(R.id.iv); //查找到图片        // 初始化控件        initView();    }    private void initView() {        btn_alpha.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 透明图片 1.0不透明,0.0透明                ObjectAnimator alpha = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.5f, 0, 1, 0, 1);                // 设置执行时间                alpha.setDuration(2000);                // 开始动画                alpha.start();            }        });        btn_romate.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 旋转图片                ObjectAnimator rotation = ObjectAnimator.ofFloat(iv, "rotation", 0, 180, 90, 360);                rotation.setDuration(2000);// 设置执行时间                rotation.start(); // 开始启动            }        });        btn_scale.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 缩放图片                ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv, "scaleY", 0.1f, 2, 1, 0.5f);                scaleY.setDuration(2000);//设置执行时间                scaleY.start();            }        });        btn_translate.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 平移图片从X轴的10走到90再回到30再走到100                ObjectAnimator translationX = ObjectAnimator.ofFloat(iv, "translationX", 10, 90, 30, 100);                translationX.setDuration(2000);// 设置执行时间                // 启动动画                translationX.start();            }        });        btn_all.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 所有效果一起执行                AnimatorSet animatorSet = new AnimatorSet();                ObjectAnimator translationX = ObjectAnimator.ofFloat(iv, "translationX", 10, 90, 30, 100);                ObjectAnimator rotation = ObjectAnimator.ofFloat(iv, "rotation", 0, 180, 90, 360);                ObjectAnimator alpha = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.5f, 0, 1, 0, 1);                ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv, "scaleY", 0.1f, 2, 1, 0.5f);                // 设置执行时间                animatorSet.setDuration(3000);                animatorSet.setTarget(iv);                // 往集合中添加对象                // 挨个执行//                animatorSet.playSequentially(translationX, rotation, alpha, scaleY);                // 一起飞                animatorSet.playTogether(translationX, rotation, alpha, scaleY);                // 一起执行                animatorSet.start();            }        });    }}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?><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"    tools:context="com.myattributesanimdemo.MainActivity">    <!--添加5个按钮和一张图片-->    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/btn_alpha"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="透明" />        <Button            android:id="@+id/btn_rotate"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="旋转" />        <Button            android:id="@+id/btn_scale"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="缩放" />        <Button            android:id="@+id/btn_translate"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="平移" />        <Button            android:id="@+id/btn_all"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:layout_marginTop="20dp"            android:layout_weight="1"            android:text="一起" />    </LinearLayout>    <ImageView        android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:src="@mipmap/ic_launcher" /></RelativeLayout>

3.2 使用XML加载

由于属性动画不是很常用,在此我就举个例子就可以了
MainActivity.java
package com.myattributesanimxmldemo;import android.animation.Animator;import android.animation.AnimatorInflater;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    private ImageView iv;    private Button btn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 找到关心的控件        btn = (Button) findViewById(R.id.btn);        iv = (ImageView) findViewById(R.id.iv);        btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 加载动画                Animator anim = AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.animator);                anim.setTarget(iv);                anim.start();            }        });    }}

animator.xml
<?xml version="1.0" encoding="utf-8"?><objectAnimator    android:duration="2000"    android:propertyName="translateX"    android:valueFrom="10"    android:valueTo="100"    xmlns:android="http://schemas.android.com/apk/res/android"></objectAnimator>

activity_main.xml
<?xml version="1.0" encoding="utf-8"?><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"    tools:context="com.myattributesanimxmldemo.MainActivity">    <Button        android:id="@+id/btn"        android:text="旋转动画"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <ImageView        android:id="@+id/iv"        android:layout_centerInParent="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@mipmap/ic_launcher" /></RelativeLayout>


阅读全文
0 0
原创粉丝点击