Android学习——动画与图形(补间动画与帧动画)
动画与图形——补间动画与帧动画
参考博客:https://blog.****.net/zhangqunshuai/article/details/81098062
Animations介绍
Android提供了几种动画类型: View Animation、Drawable Animation、Property Animation。View Animation相当简单, 分别是Tween动画,和Frame动画。Tween通过场景里的对象不断的进行图片的变换,比如平移、渐变、缩放、旋转等来产生动画效果;Frame动画叫做顺序播放实现做好的图像和电影类似。另外加上gif动画, 因为如果直接使用Bitmap或其他方法直接调用gif图片资源的话,显示的是静态的,如果要做成动态的, 就需要一些其他的方法来实现。
Tween动画(补间动画)
首先来谈谈Tween动画,它有四种:
1、Alpha:渐变透明度动画
2、Scale:渐 变R寸伸缩动画
3、Translate:画 面转换位置移动动画
4、Rotate:画面转移旋转动画
这些动画的执行步骤差不多:先定义Animation 动画对象,然后设置动画的一些属性,最后通过startAnimation()方法开始动画。
setDuration(long durationMillis);
功能:设置动画显示的时间
参数:durationMillis为动画显示时间的长短,以毫秒为单位
startAnimation(Animation animation)
功能: animation为要播放的动画
参数: animation 为要播放的动画
- Alpha渐变动画
通过XML来创建动画alpha_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true">
<!--fillAfter:控件动画结束时是否保持动画最后的状态-->
<!--fillEnabled:控件动画结束时是否还原到开始动画前的状态(同android:fillBefore)-->
<alpha android:fromAlpha="0.1" android:toAlpha="1.0"
android:duration="3000"/>
</set>
或直接在程序中创建动画
开始动画
public void click(View view){
alpha = AnimationUtils.loadAnimation(this,
R.anim.alpha_anim);
imageView.startAnimation(alpha);
}
- rotate旋转动画
anim_rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true">
<!--插值器accelerate_interpolator:从缓慢到加速-->
<rotate
android:duration="2000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="720"/>
<!--startOffset:调用start函数之后等待开始运行的时间,单位为毫秒(用于顺序执行多个动画)-->
<rotate
android:duration="2000"
android:fromDegrees="360"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="2000"
android:toDegrees="0"/>
</set>
public void rotateClick(View view){
alpha = AnimationUtils.loadAnimation(this,
R.anim.anim_rotate);
imageView.startAnimation(alpha);
}
- scale缩放动画
anim_scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true">
<scale
android:duration="2000"
android:fillAfter="true"
android:fromXScale="1"
android:fromYScale="1"
android:interpolator="@android:anim/decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="1"
android:repeatMode="reverse"
android:toXScale="2.0"
android:toYScale="2.0" />
</set>
public void scaleClick(View view){
alpha = AnimationUtils.loadAnimation(this,
R.anim.anim_scale);
imageView.startAnimation(alpha);
}
- translate平移动画
anim_translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true">
<translate
android:duration="2000"
android:fillAfter="true"
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toXDelta="860"
android:toYDelta="0"></translate>
</set>
public void translateClick(View view){
alpha = AnimationUtils.loadAnimation(this,
R.anim.anim_translate);
imageView.startAnimation(alpha);
}
Frame动画(帧动画)
定义帧动画的资源文件
anim_frame.xml
注意,Android Studio中anim_frame.xml如果放置在res/anim文件夹下的话,会报错。因此要把它放置在res/drawable下。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/a" android:duration="500" />
<item android:drawable="@drawable/b" android:duration="500" />
<item android:drawable="@drawable/c" android:duration="500" />
<item android:drawable="@drawable/d" android:duration="500" />
<item android:drawable="@drawable/d" android:duration="500" />
<item android:drawable="@drawable/c" android:duration="500" />
<item android:drawable="@drawable/b" android:duration="500" />
<item android:drawable="@drawable/a" android:duration="500" />
</animation-list>
//开始帧动画
public void startClick(View view){
if (anim == null) {
imageView_frame.setBackgroundResource(R.drawable.anim_frame);
anim = (AnimationDrawable) imageView_frame.getBackground();
}
anim.start();
}
//停止帧动画
public void stopClick(View view){
if (anim != null && anim.isRunning()) { //如果正在运行,就停止
anim.stop();
}
}