补间动画
程序员文章站
2022-03-25 14:55:25
...
补间动画:指定动画资源(图片)开始位置中间位置和结束位置,控件仍停留在原来位置(实际位置没有改变)
例如:匀速旋转动画
(透明度、缩放、位移、旋转)
/**
* 透明度变化的动画
* @param view
*/
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为一张图片资源
}
/**
* 缩放动画
*
* @param view
*/
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);
}
/**
* 位移动画
*
* @param view
*/
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/**
* 旋转动画
*
* @param view
*/
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);
}
补间动画——xml形式
alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<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>
/**
* 透明度变化的动画
*
* @param view
*/
public void alpha(View view) {
Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha);
iv.startAnimation(aa);
}
scale.xml
<?xml version="1.0" encoding="utf-8"?>
<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" >
</scale>
/**
* 缩放动画
*
* @param view
*/
public void scale(View view) {
Animation sa = AnimationUtils.loadAnimation(this, R.anim.scale);
iv.startAnimation(sa);
}
trans.xml
<?xml version="1.0" encoding="utf-8"?>
<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" 停留在最后一个动画效果的位置,不回到原来的位置
/**
* 位移动画
*
* @param view
*/
public void Trans(View view) {
Animation ta = AnimationUtils.loadAnimation(this, R.anim.trans);
iv.startAnimation(ta);
}
rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<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%" >
</rotate>
/**
* 旋转动画
*
* @param view
*/
public void rotate(View view) {
Animation ra = AnimationUtils.loadAnimation(this, R.anim.rotate);
iv.startAnimation(ra);
}
set.xml
<?xml version="1.0" encoding="utf-8"?>
<set>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="2"
android:repeatMode="reverse"
android:toDegrees="360" >
</rotate>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="2"
android:repeatMode="reverse"
android:toXScale="2.0"
android:toYScale="2.0" >
</scale>
<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:toYDelta="50%" >
</translate>
</set>
public void set(View view){
Animation set = AnimationUtils.loadAnimation(this, R.anim.set);
iv.startAnimation(set);
}
例如:匀速旋转动画
/** 均匀旋转动画 */
private RotateAnimation refreshingAnimation;
refreshingAnimation = (RotateAnimation) AnimationUtils.loadAnimation(mContext, R.anim.rotating);
refreshingAnimation.setDuration(5000);
// 添加匀速转动动画
LinearInterpolator lir = new LinearInterpolator();
refreshingAnimation.setInterpolator(lir);
//设置动画(匀速旋转动画)
atdp_loading_iv.startAnimation(refreshingAnimation);
atdp_loading_iv.clearAnimation();
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:toDegrees="359" >
</rotate>
上一篇: Android动画系列之补间动画详解