view动画
补间动画 Tween Animation
补间动画:指定动画资源开始位置到结束位置,控件仍停留在原来位置(实际位置没有改变)可有(透明度、缩放、平移、旋转、闪烁、切换效果)
以下是动态创建
//透明度变化的动画
public void alpha(View view) {
AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);// 完全透明0.0f ---> 完全不透明1.0f
aa.setDuration(2000);// 动画播放2秒
aa.setRepeatCount(2);//重复播放次数
aa.setRepeatMode(Animation.REVERSE);//重复模式 倒序播
iv.startAnimation(aa);//iv为一张图片资源
}
//缩放动画
public void scale(View view) {
//fromX,toX,fromY,toY,pivotXType,pivotXValue,pivotYType,pivotYValue
//X,Y方向放大2倍,围绕(0,0)坐标缩放 [(0.5f,0.5f)中心点]
ScaleAnimation sa = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
sa.setDuration(2000);
sa.setRepeatCount(2);
sa.setRepeatMode(Animation.REVERSE);
iv.startAnimation(sa);
}
//平移动画
public void Trans(View view) {
//pivotXType,pivotXValue,toXType,toXValue,pivotYType,pivotYValue,toYType,toYValue
//X和Y方向移动位移(相对自身)
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0.5f);//还可以相对于父类RELATIVE_TO_PARENT
ta.setDuration(2000);
ta.setRepeatCount(2);
ta.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ta);
ta.setRepeatCount(Animation.INFINITE);//不停止,一直播放 值为-1
//旋转动画
public void rotate(View view) {
//fromDegrees,toDegrees,pivotXType,pivotXValue,pivotYType,pivotYValue
//围绕中心点旋转360度
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
ra.setDuration(2000);
ra.setRepeatCount(2);
ra.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ra);
}
//动画合集
public void set(View view){
AnimationSet set = new AnimationSet(false);
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
ta.setDuration(2000);
ta.setRepeatCount(2);
ta.setRepeatMode(Animation.REVERSE);
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
ra.setDuration(2000);
ra.setRepeatCount(2);
ra.setRepeatMode(Animation.REVERSE);
ScaleAnimation sa = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
sa.setDuration(2000);
sa.setRepeatCount(2);
sa.setRepeatMode(Animation.REVERSE);
set.addAnimation(sa);
set.addAnimation(ta);
set.addAnimation(ra);
iv.startAnimation(set);
}
以下是静态创建
可以在res新建一个包anim,用来存放静态创建所用到的xml文件
alpha.xml(即透明度的变化)
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1.0"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="reverse" >
</alpha>
在监听事件中
public void alpha(View view) {
Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha);
iv.startAnimation(aa);
}
scale.xml(即动画的缩放)
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:pivotX="50%" //50%p//相对于PARENT的50%
android:pivotY="50%"
android:repeatCount="2"
android:repeatMode="reverse"
android:toXScale="2.0"
android:toYScale="2.0" >
public void scale(View view) {
Animation sa = AnimationUtils.loadAnimation(this, R.anim.scale);
iv.startAnimation(sa);
}
trans.xml(即动画的移动)
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXDelta="-50%"
android:fromYDelta="-50%"
android:repeatCount="2"
android:repeatMode="reverse"
android:toXDelta="50%"
android:fillAfter="true"
android:toYDelta="50%" >
</translate>
//android:fillAfter="true" 停留在最后一个动画效果的位置,不回到原来的位置
public void Trans(View view) {
Animation ta = AnimationUtils.loadAnimation(this, R.anim.trans);
iv.startAnimation(ta);
rotate.xml(即动画的旋转)
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="reverse"
android:pivotX="50%"
android:pivotY="50%" >
public void rotate(View view) {
Animation ra = AnimationUtils.loadAnimation(this, R.anim.rotate);
iv.startAnimation(ra);
}
set.xml(动画的集合包括选择,平移等等)
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fromXDelta="0"
android:toXDelta="100"
android:fromYDelta="0"
android:startOffset="3000"
android:toYDelta="500">
</translate>
<scale
android:duration="3000"
android:fromXScale="0.0"
android:toXScale="2"
android:fromYScale="0.0"
android:toYScale="0.5"
android:pivotX="50"
android:pivotY="50">
</scale>
<rotate
android:fromDegrees="0"
android:toDegrees="-360"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%"/>
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.5"/>
</set>
public void set(View view){
Animation set = AnimationUtils.loadAnimation(this, R.anim.set);
iv.startAnimation(set);
}
页面切换效果
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<scale
android:duration="1000"
android:fromXScale="0.1"
android:fromYScale="0.1"
android:pivotX="50%p"
android:pivotY="50%p"
android:toXScale="1.0"
android:toYScale="1.0"/>
<alpha
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1.0" />
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<scale
android:duration="@android:integer/config_mediumAnimTime"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%p"
android:pivotY="50%p"
android:toXScale="0.1"
android:toYScale="0.1"/>
<alpha
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1.0" />
</set>
Intent intent = new Intent(TweenAnimationActivity.this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.zoom_in, R.anim.zoom_out);
逐帧动画
逐帧动画(Frame Animation),是通过将一系列图片按照一定的顺序展示实现的动画。比补间动画要简单很多。
可以在res下的drawable包添加一系列图片
在res下的drawable包建立一个frame_anim.xml文件
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/wait0" android:duration="200"/>
<item android:drawable="@drawable/wait1" android:duration="200"/>
<item android:drawable="@drawable/wait2" android:duration="200"/>
<item android:drawable="@drawable/wait3" android:duration="200"/>
<item android:drawable="@drawable/wait4" android:duration="200"/>
<item android:drawable="@drawable/wait5" android:duration="200"/>
<item android:drawable="@drawable/wait6" android:duration="200"/>
<item android:drawable="@drawable/wait7" android:duration="200"/>
</animation-list>
将它引入到你的布局中
<ImageView
android:id="@+id/iv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/frame_anim"/>
然后在监听事件中
// 获取AnimationDrawable对象
AnimationDrawable animation = (AnimationDrawable) imageView.getBackground();
// 启动动画
animation.start();
属性动画(Property Animation)
弥补了效果单一和动画局限性的缺点,修改原本对象的某个值来改变属性,动画效果非常丰富。
普通展示
值动画
private void startValueAnimation() {
// 1. 设置动画属性的初始值、结束值
// 第1 种方式:动态设置
ValueAnimator animator = ValueAnimator.ofFloat(0.0f, 1.0f, 0.5f);
// 第2 种方式:加载animator目录下的xml文件
// ValueAnimator animator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.set_value_animator);
// 2. 设置动画的播放时长
animator.setDuration(3000);
// 3. 增加动画属性值的更新监听器
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// 3.1 获取属性值
float value = (float) animation.getAnimatedValue();
// 3.2 给控件对象赋值
textView.setAlpha(value);
}
});
// 4. 启动动画
animator.start();
}
属性动画
ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "alpha", 0.0f, 1.0f);
// ObjectAnimator animator = (ObjectAnimator)
animator.setTarget(textView);
animator.setDuration(3000);
animator.start();
组合动画
private void startAnimationSet() {
// 动画效果:透明度, 常规 - 全透明 - 常规
ObjectAnimator alpha = ObjectAnimator.ofFloat(textView, "alpha", 1f, 0f, 1f);
alpha.setDuration(5000);
alpha.start();
// 旋转
ObjectAnimator rotation = ObjectAnimator.ofFloat(textView, "rotation", 0f, 360f);
// 平移
float currentX = textView.getX();
ObjectAnimator translationX = ObjectAnimator.ofFloat(textView, "translationX", currentX, currentX + 300, currentX);
// 缩放
ObjectAnimator scaleX = ObjectAnimator.ofFloat(textView, "scaleX", 1f, 3f, 1f);
// 动画组合
AnimatorSet set = new AnimatorSet();
set.play(translationX).with(rotation).before(scaleX);
set.start();
// 加载animator目录的动画组合
// AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.set_animator_set);
// set.setTarget(textView);
// set.setDuration(5000);
// set.start();
}
自定义估值器
private void startCustomAnimation() {
ObjectAnimator animator = ObjectAnimator.ofObject(textView, "x", new TranslateXEvalutor(), 0, 300);
animator.setDuration(3000);
animator.start();
}