1. 前言
动画在安卓中,使用是很是常见的,好比网络请求时的loading,就是经过旋转实现的。 在安卓中,动画分为两大类, 分别是视图动画和属性动画。视图动画又分为帧动画和补间动画。这篇文章主要讲补间动画的使用。三种动画的使用文章地址以下:java
2. 介绍
补间动画指的是, 设置好动画的开始属性 和 结束属性。 系统会在咱们设置的动画时间内, 从开始时属性过渡到结束时候的属性。举个例子,一个控件向右平移100像素,其实改变的属性就是控件x轴的坐标。系统提供了四种补间动画,分别为:android
3. 使用
3.1 平移动画
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:startOffset="1000"
android:fillBefore="true"
android:fillAfter="false"
android:fillEnabled="true"
android:repeatMode="restart"
android:repeatCount="3"
android:fromXDelta="0"
android:toXDelta="500"
android:fromYDelta="0"
android:toYDelta="500"
>
</translate>
复制代码
解释:
第一部分是动画的公共属性,也就是其余三种动画也有这些属性:git
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
iv.startAnimation(animation);
复制代码
至此动画就已经定义完成并能够播放了。 第一步中的xml方式也能够经过java代码来实现,代码方式以下:github
Animation translateAnimation = new TranslateAnimation(0,500,0,500);
mation
translateAnimation.setDuration(3000);
// 固定属性的设置都是在其属性前加“set”,如setDuration()
iv.startAnimation(translateAnimation);
复制代码
解释:
bash
3.2 缩放动画
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:repeatCount="2"
android:fillBefore="true"
android:repeatMode="restart"
android:fromXScale="0"
android:toXScale="2"
android:fromYScale="0"
android:toYScale="2"
android:pivotX="50%"
android:pivotY="50%"
>
</scale>
复制代码
解释:
网络
Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
iv.startAnimation(animation);
复制代码
3.3 旋转动画
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:repeatCount="2"
android:repeatMode="restart"
android:fillBefore="true"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
>
</rotate>
复制代码
解释:
dom
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
iv.startAnimation(animation);
复制代码
3.4 透明动画
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:startOffset="500"
android:fillBefore="true"
android:fromAlpha="1.0"
android:toAlpha="0.0"
>
</alpha>
复制代码
解释:
ide
Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
iv.startAnimation(animation);
复制代码
3.5 组合动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fillBefore="true"
android:repeatCount="0"
android:repeatMode="restart"
android:shareInterpolator="true"
>
<translate
android:fromXDelta="0"
android:toXDelta="250"
android:fromYDelta="0"
android:toYDelta="-250"
android:duration="3000"
>
</translate>
<rotate
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
android:fromDegrees="0"
android:toDegrees="30"
>
</rotate>
<!--<alpha-->
<!--android:duration="3000"-->
<!--android:fromAlpha="1.0"-->
<!--android:toAlpha="0.5"-->
<!--/>-->
</set>
复制代码
解释:
函数
特别注意:
布局
Animation animation = AnimationUtils.loadAnimation(this, R.anim.combination);
iv.startAnimation(animation);
复制代码
4. 实例
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_water"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@mipmap/water"
android:layout_centerInParent="true"
android:layout_above="@id/iv_shumiao"
android:visibility="gone"
/>
<ImageView
android:id="@+id/iv_shumiao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/shumiao"
android:layout_centerInParent="true"
/>
<ImageView
android:id="@+id/iv_shuihu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/shuihu"
android:layout_below="@id/iv_shumiao"
/>
</RelativeLayout>
复制代码
package com.domain.study.animation.tween;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import com.domain.study.animation.R;
import com.domain.study.animation.base.BaseActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class TweenCombinationActivity extends BaseActivity {
@BindView(R.id.iv_shumiao)
ImageView ivShumiao;
@BindView(R.id.iv_shuihu)
ImageView ivShuihu;
@BindView(R.id.iv_water)
ImageView ivWater;
int shuiHuX = 0;
int shuiHuY = 0;
int shuMiaoX = 0;
int shuMiaoY = 0;
int shuMiaoWidth = 0;
int shuMiaoHeight = 0;
int shuiHuWidth = 0;
int shuiHuHeight = 0;
int waterHeight = 0;
@Override
protected int setLayoutId() {
return R.layout.activity_combination;
}
@Override
protected void initView() {
ivShuihu.post(new Runnable() {
@Override
public void run() {
int[] location = new int[2];
ivShuihu.getLocationOnScreen(location);
shuiHuX = location[0]; // view距离 屏幕左边的距离(即x轴方向)
shuiHuY = location[1]; // view距离 屏幕顶边的距离(即y轴方向)
shuiHuWidth = ivShuihu.getWidth();
shuiHuHeight = ivShuihu.getHeight();
}
});
ivShumiao.post(new Runnable() {
@Override
public void run() {
int[] location = new int[2];
ivShumiao.getLocationOnScreen(location);
shuMiaoX = location[0]; // view距离 屏幕左边的距离(即x轴方向)
shuMiaoY = location[1]; // view距离 屏幕顶边的距离(即y轴方向)
shuMiaoWidth=ivShumiao.getWidth(); // 获取宽度
shuMiaoHeight =ivShumiao.getHeight(); // 获取高度
}
});
ivWater.post(new Runnable() {
@Override
public void run() {
waterHeight = ivWater.getHeight();
}
});
}
@OnClick(R.id.iv_shuihu)
public void onViewClicked() {
start();
}
private void start(){
AnimationSet setAnimation = new AnimationSet(true);
// 子动画1:旋转动画
Animation rotate = new RotateAnimation(0,10,0.5f,0.5f);
rotate.setDuration(1000);
rotate.setStartOffset(3000);
// 子动画2:平移动画
Animation translate = new TranslateAnimation(
0,
shuMiaoX-shuiHuWidth,
0
,-(shuiHuHeight+shuMiaoHeight+waterHeight+100));
translate.setDuration(3000);
//等待水滴落下动画
Animation wait = new TranslateAnimation(
0,
0,
0
,0);
wait.setStartOffset(4000);
wait.setDuration(2000);
// 将建立的子动画添加到组合动画里
setAnimation.addAnimation(translate);
setAnimation.addAnimation(rotate);
setAnimation.addAnimation(wait);
ivShuihu.startAnimation(setAnimation);
rotate.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
ivWater.setVisibility(View.VISIBLE);
startWaterAnimation();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
private void startWaterAnimation(){
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(0,0,0,50);
translateAnimation.setDuration(2000);
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(2000);
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(alphaAnimation);
ivWater.startAnimation(animationSet);
animationSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
ivWater.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
复制代码
5.总结
6. 完整demo地址
7. 参考文章
8. 历史文章目录