一、view动画
* 帧动画 FrameAnimation
类似放电影,图片一张接一张的播放
* 属性动画
* 补间动画 TweenAnimation
(1)平移动画 translate
步骤:
(1)在res文件夹下新建文件夹anim
(2)在amin下新建动画布局文件xml
* android:toXDelta="?"
> ?中的值,可以写3中类型,如果直接写数字,代表的是绝对值的移动
> 写成0.5,代表0.5个像素
> 如果写50% ,代表的是自己的宽或高的1/2
> 如果写50%p ,代表的是屏幕的1/2
三、建议使用XML文件来定义动画
> xml属性动画的核心:使用充气球的方式,把资源冲进去加载,所以是它本身在移动.
> View动画的核心:图像还是在原来的位置,x轴,y轴没有变动,只是把图像切换出来,执行了一连串的动作.
四、属性动画
补间动画的核心是,图像还是在原来的位置,x轴,y轴没有变动,只是把图像切换出来,执行了一连串的动作。
属性动画可以改变图画属性的值。
4.1 属性动画
步骤:
(1)在res文件夹下新建文件夹animator
(2)在amin下新建动画布局文件xml
* 帧动画 FrameAnimation
类似放电影,图片一张接一张的播放
* 属性动画
* 补间动画 TweenAnimation
(1)平移动画 translate
public void translate(View v) {
// 定义一个平移动画
/*
* TranslateAnimation animation = new TranslateAnimation( 0, 200, 0, 0);
*/
/*
* RELATIVE_TO_PARENT :相对屏幕的宽高来说 倍数
* RELATIVE_TO_SELF :相对自己控件的宽高来说 倍数
* Animation.ABSOLUTE :绝对值 指的是像素
*/
TranslateAnimation animation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT,
0.5f, Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0);
// 定义动画时长
animation.setDuration(2000);
// 无限循环播放
animation.setRepeatCount(TranslateAnimation.INFINITE);
// 指定重复播放的模式
animation.setRepeatMode(TranslateAnimation.RESTART);
// 让图片播放这个动画
iv.startAnimation(animation);
}
(2)旋转动画 rotate
public void rotate(View v) {
RotateAnimation animation = new RotateAnimation(0,
360,// 从什么角度旋转到什么角度
Animation.RELATIVE_TO_SELF, 0.2f,
Animation.RELATIVE_TO_SELF,
0.5f);
// 定义动画时长
animation.setDuration(2000);
// 无限循环播放
animation.setRepeatCount(TranslateAnimation.INFINITE);
// 指定重复播放的模式
animation.setRepeatMode(TranslateAnimation.REVERSE);
// 让图片播放这个动画
iv.startAnimation(animation);
}
(3)缩放动画
scale
public void scale(View v) {
ScaleAnimation animation = new ScaleAnimation(3, 1, // x方向,从什么倍数到什么倍数
3, 1,// y方向,从什么倍数到什么倍数
Animation.RELATIVE_TO_SELF, 0.5f, // 放大的中心点 x方向
Animation.RELATIVE_TO_SELF, 0.5f); // 放大的中心店 y方向
// 定义动画时长
animation.setDuration(2000);
// 无限循环播放
animation.setRepeatCount(TranslateAnimation.INFINITE);
// 指定重复播放的模式
animation.setRepeatMode(TranslateAnimation.REVERSE);
// 让图片播放这个动画
iv.startAnimation(animation);
}
(4)透明度渐变动画 alpha
public void alpha(View v) {
AlphaAnimation animation = new AlphaAnimation(0.5f, 1);
// 定义动画时长
animation.setDuration(2000);
// 无限循环播放
animation.setRepeatCount(TranslateAnimation.INFINITE);
// 指定重复播放的模式
animation.setRepeatMode(TranslateAnimation.REVERSE);
// 让图片播放这个动画
iv.startAnimation(animation);
}
(5)组合
public void set(View v){
//包含 平移动画 、 旋转动画
// Interpolator : 插入器 false : 每个动画使用自己的插入器, true: 大家都使用集合里面的插入器,使用同一
AnimationSet set = new AnimationSet(false);
/*//定义平移动画
TranslateAnimation animation = new TranslateAnimation(
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 200,
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 0);*/
TranslateAnimation animation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT,
0.5f, Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0.5f);
// 定义动画时长
animation.setDuration(2000);
// 无限循环播放
animation.setRepeatCount(TranslateAnimation.INFINITE);
// 指定重复播放的模式
animation.setRepeatMode(TranslateAnimation.REVERSE);
/**///定义旋转动画
RotateAnimation ranimation = new RotateAnimation(0,
360,// 从什么角度旋转到什么角度
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
// 定义动画时长
ranimation.setDuration(2000);
// 无限循环播放
ranimation.setRepeatCount(TranslateAnimation.INFINITE);
// 指定重复播放的模式
ranimation.setRepeatMode(TranslateAnimation.REVERSE);
ScaleAnimation sanimation = new ScaleAnimation(1, 2, // x方向,从什么倍数到什么倍数
1, 2,// y方向,从什么倍数到什么倍数
Animation.RELATIVE_TO_SELF, 0.5f, // 放大的中心点 x方向
Animation.RELATIVE_TO_SELF, 0.5f); // 放大的中心店 y方向
// 定义动画时长
sanimation.setDuration(2000);
// 无限循环播放
sanimation.setRepeatCount(TranslateAnimation.INFINITE);
// 指定重复播放的模式
sanimation.setRepeatMode(TranslateAnimation.REVERSE);
AlphaAnimation aanimation = new AlphaAnimation(0, 1);
// 定义动画时长
aanimation.setDuration(2000);
// 无限循环播放
aanimation.setRepeatCount(TranslateAnimation.INFINITE);
// 指定重复播放的模式
aanimation.setRepeatMode(TranslateAnimation.REVERSE);
set.addAnimation(animation);
set.addAnimation(ranimation);
// set.addAnimation(aanimation);
//播放集合动画
iv.startAnimation(set);
}
二、使用XML文件定义动画
步骤:
(1)在res文件夹下新建文件夹anim
(2)在amin下新建动画布局文件xml
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="50%p"
android:duration="2000"
android:repeatCount="infinite"
android:repeatMode="reverse">
</translate>
(3)在activity文件中
//从xml文件---动画对象
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate_demo);
iv.startAnimation(animation);
注意:* android:toXDelta="?"
> ?中的值,可以写3中类型,如果直接写数字,代表的是绝对值的移动
> 写成0.5,代表0.5个像素
> 如果写50% ,代表的是自己的宽或高的1/2
> 如果写50%p ,代表的是屏幕的1/2
三、建议使用XML文件来定义动画
> xml属性动画的核心:使用充气球的方式,把资源冲进去加载,所以是它本身在移动.
> View动画的核心:图像还是在原来的位置,x轴,y轴没有变动,只是把图像切换出来,执行了一连串的动作.
四、属性动画
补间动画的核心是,图像还是在原来的位置,x轴,y轴没有变动,只是把图像切换出来,执行了一连串的动作。
属性动画可以改变图画属性的值。
4.1 属性动画
public class MainActivity extends Activity {
ImageView iv ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "aaaaaaaaaaa", 0).show();
}
});
}
public void translate(View v) {
// iv.setTranslationX()
ObjectAnimator animator = ObjectAnimator.ofFloat(
iv, //谁将要执行这个动画 一般放控件的对象
"translationX", //水平平移的动画
new float[]{0 ,10,20,30, 100}); //从0位置,移动到100位置
animator.setDuration(2000);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setRepeatMode(ObjectAnimator.REVERSE);
animator.start();
}
public void rotate(View v) {
// iv.setRotation(rotation)
// iv.setRotationY(rotationY)
ObjectAnimator animator = ObjectAnimator.ofFloat(
iv, //谁将要执行这个动画 一般放控件的对象
"rotationX", //以x方向为中轴线旋转的动画
new float[]{0 ,360}); //从0位置,旋转到100位置
animator.setDuration(2000);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setRepeatMode(ObjectAnimator.REVERSE);
animator.start();
}
public void scale(View v) {
// iv.setScaleX(scaleX)
ObjectAnimator animator = ObjectAnimator.ofFloat(
iv, //谁将要执行这个动画 一般放控件的对象
"scaleX", //以x方向为中轴线旋转的动画
new float[]{1 ,3}); //从0位置,旋转到100位置
animator.setDuration(2000);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setRepeatMode(ObjectAnimator.REVERSE);
animator.start();
}
public void alpha(View v) {
// iv.setAlpha(alpha)
ObjectAnimator animator = ObjectAnimator.ofFloat(
iv, //谁将要执行这个动画 一般放控件的对象
"alpha", //透明度动画
new float[]{0 ,1}); //从完全透明到完全不透明
animator.setDuration(2000);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setRepeatMode(ObjectAnimator.REVERSE);
animator.start();
}
}
4.2 属性组合动画
//属性动画集合
public void set(View v){
// AnimationSet View动画的集合
AnimatorSet set = new AnimatorSet();
ObjectAnimator animator = ObjectAnimator.ofFloat(
iv, //谁将要执行这个动画 一般放控件的对象
"translationX", //水平平移的动画
new float[]{0 , 100}); //从0位置,移动到100位置
animator.setDuration(2000);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setRepeatMode(ObjectAnimator.REVERSE);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(
iv, //谁将要执行这个动画 一般放控件的对象
"translationY", //水平平移的动画
new float[]{0 , 100}); //从0位置,移动到100位置
animator2.setDuration(2000);
animator2.setRepeatCount(ObjectAnimator.INFINITE);
animator2.setRepeatMode(ObjectAnimator.REVERSE);
// set.playTogether(animator , animator2); //同时播放两个动画
set.playSequentially(animator , animator2); //按顺序播放
set.start(); //播放动画
}
4.3 用xml实现属性动画
步骤:
(1)在res文件夹下新建文件夹animator
(2)在amin下新建动画布局文件xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="translationX"
android:valueFrom="0"
android:valueTo="150"
android:duration="2000"
android:repeatCount="infinite"
android:repeatMode="reverse"
>
</objectAnimator>
(3)在activity文件中
public void translate(View v) {
Xml.newSerializer().setOutput(os, encoding);
//从xml文件导入动画资源
Animator anim = AnimatorInflater.loadAnimator(this, R.animator.traslate_demo);
//指定哪一个控件去播放这个动作
anim.setTarget(iv);
anim.start();
}