首先我们先来了解一下Android的动画分为三种:
1,补间动画:提供开始帧和结束帧,中间的过程由计算机自己完成。
2,帧动画:逐帧动画的工作原理很简单,其实就是将一个完整的动画拆分成一张张单独的图片,然后再将它们连贯起来进行播放,类似于动画片的工作原理。
3,属性动画:通过改变属性实现的动画。
今天我们就来重点搞一下属性动画
属性动画
特点:动画效果会改变控件的位置.且开启动画的是动画对象,而不是控件对象.
只有旋转的属性动画是经常用的,注意参数.
注意:这些方法都是安卓在3.0以后出现的新特性,所以要把AndroidManifest.xml里的 android:minSdkVersion值修改为11以上
接下来 直接上代码:
这里面我注释写的挺详细的,想必大家看一遍都能明白
MainActivity
package com.animator.animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
/**
* 属性动画
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_alpha;
private Button btn_rotationY;
private Button btn_scaleX;
private Button btn_translationY;
private ImageView img;
private Button btn_gather;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化资源id
initView();
}
//获取相应的控件资源id
private void initView() {
btn_alpha = (Button) findViewById(R.id.btn_alpha);
btn_rotationY = (Button) findViewById(R.id.btn_rotationY);
btn_scaleX = (Button) findViewById(R.id.btn_scaleX);
btn_translationY = (Button) findViewById(R.id.btn_translationY);
img = (ImageView) findViewById(R.id.img);
btn_alpha.setOnClickListener(this);
btn_rotationY.setOnClickListener(this);
btn_scaleX.setOnClickListener(this);
btn_translationY.setOnClickListener(this);
btn_gather = (Button) findViewById(R.id.btn_gather);
btn_gather.setOnClickListener(this);
}
//动画的点击事件
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_alpha: //透明度动画
/**
* 参数1:View,代表你要修改那个控件的属性.
* 参数2:propertyName代表实现什么样子的动画:"alpha",String类型.
* 参数3:float... values,控件修改的参数,new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f}
*/
ObjectAnimator alpha = ObjectAnimator.ofFloat(img, "alpha", new float[]{0.0f, 02f, 0.4f, 0.6f, 0.8f, 1.0f});
//设置动画执行时间
alpha.setDuration(4000);
//设置动画的执行模式
alpha.setRepeatMode(ObjectAnimator.RESTART);
//设置动画的执行次数
alpha.setRepeatCount(1);
//使用ObjectAnimator对象开启动画
alpha.start();
break;
case R.id.btn_rotationY://旋转动画 rotationY , rotationX
ObjectAnimator rotationY = ObjectAnimator.ofFloat(img, "rotationY", new float[]{90f, 180f, 270f, 360f});
//设置动画执行时间
rotationY.setDuration(4000);
//设置动画的执行模式
rotationY.setRepeatMode(ObjectAnimator.RESTART);
//设置动画的执行次数
rotationY.setRepeatCount(1);
//使用ObjectAnimator对象开启动画
rotationY.start();
break;
case R.id.btn_scaleX://缩放动画 scaleX,scaleY
ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "scaleX", new float[]{1f, 2f, 3f, 4f, 1f});
//设置动画执行时间
scaleX.setDuration(4000);
//设置动画的执行模式
scaleX.setRepeatMode(ObjectAnimator.RESTART);
//设置动画的执行次数
scaleX.setRepeatCount(1);
//使用ObjectAnimator对象开启动画
scaleX.start();
break;
case R.id.btn_translationY://平移动画 translationY,translationX
//此刻translationY 代表的是垂直方向的平移 如果 new float[]{10f,20f,30f,40f} 为正数 则依据手机的X,Y轴坐标,Y轴是越往下
//数值越大 所以是向屏幕下方平移 如果想往上方移动 那么只需将数值改为负数即可new float[]{-10f,-20f,-30f,-40f}
// 同理,我们也可以设置水平方向的平移, 将动画属性修改为translationX即可 , 如果 new float[]{10f,20f,30f,40f} 为正数 则依据手机的X,Y轴坐标,X轴是越往右
//数值越大 所以是向屏幕右方平移,如果想往左方移动 那么只需将数值改为负数即可new float[]{-10f,-20f,-30f,-40f}
ObjectAnimator translationY = ObjectAnimator.ofFloat(img, "translationY", new float[]{10f, 20f, 30f, 40f});
//设置动画执行时间
translationY.setDuration(4000);
//设置动画的执行模式
translationY.setRepeatMode(ObjectAnimator.RESTART);
//设置动画的执行次数
translationY.setRepeatCount(1);
//使用ObjectAnimator对象开启动画
translationY.start();
break;
case R.id.btn_gather://动画集合
//创建集合
AnimatorSet set = new AnimatorSet();
//透明度动画
ObjectAnimator al = ObjectAnimator.ofFloat(img, "alpha", new float[]{0.0f, 02f, 0.4f, 0.6f, 0.8f, 1.0f});
//设置动画时长
al.setDuration(4000);
//平移动画
ObjectAnimator tran = ObjectAnimator.ofFloat(img, "translationY", new float[]{10f, 20f, 30f, 40f});
//设置动画时长
tran.setDuration(4000);
//在这里我们需要对playTogether和playSequentially有一个充足的了解,这样我们才能更好的应用到我们是项目上,
//使用playTogether:那么我们所设置的动画集合会同步进行
//使用playSequentially:那么我们所设置的动画集合会根据设置的先后顺序进行执行
//大家可以根据项目需要选择相应的效果,在这里我选的是同步进行
//同步进行
set.playTogether(al,tran);
//分别执行
//set.playSequentially(al,tran);
set.start();
break;
}
}
}
看一下布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明动画(alpha)"/>
<Button
android:id="@+id/btn_rotationY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转动画(rotationY)"/>
<Button
android:id="@+id/btn_scaleX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放动画(scaleX)"/>
<Button
android:id="@+id/btn_translationY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="平移动画(translationY)"/>
<Button
android:id="@+id/btn_gather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动画集合(gather)"/>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round"/>
</LinearLayout>
好了,属性动画的一些代码就这么多,只要你逻辑捋清楚之后,代码特别简单,希望能给大家带来帮助