补间动画就是只需要定义动画开始和结束的位置,动画中间的变化由系统去补齐。
补间动画由一下四种方式:
AplhaAnimation——透明度动画效果
ScaleAnimation ——缩放动画效果
TranslateAnimation——位移动画效果
RotateAnimation——旋转动画效果
AplhaAnimation的参数:
fromAlpha:动画开始时的透明度,0.0表示完全透明
toAlpha:动画结束时的透明度,1.0表示完全不透明
public class MainActivity extends Activity implements OnClickListener {
private ImageView iv;
private Button alpha;
private AlphaAnimation alphaAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
iv = (ImageView) findViewById(R.id.image);
alpha = (Button) findViewById(R.id.alpha);
alpha.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.alpha:
alphaAnimation = new AlphaAnimation(0.1f, 1);
alphaAnimation.setDuration(1000);
alphaAnimation.setRepeatCount(3);
alphaAnimation.setRepeatMode(Animation.REVERSE);
iv.startAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.d("TTTT", "动画开始");
}
@Override
public void onAnimationRepeat(Animation animation) {
Log.d("TTTT", "动画重复");
}
@Override
public void onAnimationEnd(Animation animation) {
Log.d("TTTT", "动画结束");
}
});
break;
}
}
}
setRepeatCount(int repeatCount)——设置重复次数
setFillAfter(boolean)——动画执行完后是否停留在执行完的状态
setStartOffset(long startOffset)——执行前的等待时间
ScakeAnimation的参数:
fromX:动画起始时X坐标上的伸缩尺寸
toX:动画结束时X坐标上的伸缩尺寸
fromY:动画起始时Y坐标上的伸缩尺寸
toY:动画结束时Y坐标上的伸缩尺寸
pivotXType:动画在X轴相对于物件位置类型
pivotXValue:动画相对于物件的X坐标的开始位置
pivotYType:动画在Y轴相对于物件位置类型
pivotYValue:动画相对于物件的Y坐标的开始位置
public class MainActivity extends Activity implements OnClickListener {
private ImageView iv;
private Button scale;
private ScaleAnimation scaleAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
iv = (ImageView) findViewById(R.id.image);
scale = (Button) findViewById(R.id.scale);
scale.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.scale:
scaleAnimation = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
iv.startAnimation(scaleAnimation);
break;
}
}
}
setRepeatCount(int repeatCount)——设置重复次数
setFillAfter(boolean)——动画执行完后是否停留在执行完的状态
setStartOffset(long startOffset)——执行前的等待时间
TranslateAnimation的参数
fromXDelta: 动画开始的点离当前View X坐标上的差值
toXDelta: 动画结束的点离当前View X坐标上的差值
fromYDelta: 动画开始的点离当前View Y坐标上的差值
toYDelta动画开始的点离当前View Y坐标上的差值
public class MainActivity extends Activity implements OnClickListener {
private ImageView iv;
private Button transla;
private TranslateAnimation translaAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
iv = (ImageView) findViewById(R.id.image);
transla = (Button) findViewById(R.id.transla);
transla.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.transla:
translaAnimation = new TranslateAnimation(0, 150, 0, 0);
translaAnimation.setDuration(1000);
iv.startAnimation(translaAnimation);
break;
}
}
}
animation.setDuration(long durationMillis)——设置动画持续时间
animation.setRepeatCount(int i)——设置重复次数
animation.setRepeatMode(Animation.REVERSE)——设置反方向执行
RotateAnimation的参数:
fromDegrees——旋转的开始角度。
toDegrees——旋转的结束角度。
pivotXType——X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
pivotXValue——X坐标的伸缩值。
pivotYType——Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
pivotYValue——Y坐标的伸缩值。
public class MainActivity extends Activity implements OnClickListener {
private ImageView iv;
private Button rotate;
private RotateAnimation rotateAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
iv = (ImageView) findViewById(R.id.image);
rotate = (Button) findViewById(R.id.rotate);
rotate.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rotate:
rotateAnimation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotateAnimation.setDuration(2000);
iv.startAnimation(rotateAnimation);
break;
}
}
}
animation.setRepeatCount(int repeatCount)——设置重复次数
animation.setFillAfter(boolean)——动画执行完后是否停留在执行完的状态
animation.setStartOffset(long startOffset)——执行前的等待时间