一、缩放动画介绍
Android缩放动画,是改变控件大小的一种动画效果。通常会配合其他动画效果,例如位移动画、旋转动画等等,来实现更加生动的界面效果。
下面是一个简单的缩放动画示例:
mAnimateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
animationSet.addAnimation(new ScaleAnimation(1, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f));
animationSet.setDuration(1000);
animationSet.setFillAfter(true);
mImageView.startAnimation(animationSet);
}
});
以上代码实现了当用户点击按钮时,图片控件会从原大小缩放到1.5倍的大小。
二、缩放动画属性
缩放动画的实现依靠了ScaleAnimation类。以下是ScaleAnimation类提供的属性:
- fromX:动画起始时 X轴的伸缩尺寸
- toX:动画结束时 X轴的伸缩尺寸
- fromY:动画起始时Y轴的伸缩尺寸
- toY:动画结束时Y轴的伸缩尺寸
- pivotXType:缩放中心点的X轴坐标的类型
- pivotXValue:缩放中心点的X轴坐标的相对位置
- pivotYType:缩放中心点的Y轴坐标的类型
- pivotYValue:缩放中心点的Y轴坐标的相对位置
其中,伸缩尺寸的值必须在0~1之间,且0表示完全收缩,1表示完全伸展。缩放中心点的相对位置是以控件的大小为基准的,取值范围在0~1之间。
三、缩放动画示例
1. 点击时缩放
实现效果:当用户点击按钮时,按钮控件会从原大小缩放到1.2倍的大小再缩放回原大小。
mScaleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
animationSet.addAnimation(new ScaleAnimation(1, 1.2f, 1, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f));
animationSet.setDuration(300);
animationSet.addAnimation(new ScaleAnimation(1.2f, 1f, 1.2f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f));
animationSet.setDuration(300);
mScaleBtn.startAnimation(animationSet);
}
});
2. 逐渐缩小
实现效果:控件开始时大小为填充父布局,逐渐减小到原大小。
mScaleLayout.post(new Runnable() {
@Override
public void run() {
float fromX = (float) mScaleLayout.getWidth() / mScaleLayout.getParent().getWidth();
float fromY = (float) mScaleLayout.getHeight() / mScaleLayout.getParent().getHeight();
float toX = 1.0f;
float toY = 1.0f;
ScaleAnimation scaleAnimation = new ScaleAnimation(fromX, toX, fromY, toY, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(500);
mScaleLayout.startAnimation(scaleAnimation);
}
});
3. 逐渐放大
实现效果:控件开始时大小为原大小,逐渐增大到填充父布局。
mScaleLayout.post(new Runnable() {
@Override
public void run() {
float fromX = 1.0f;
float fromY = 1.0f;
float toX = (float) mScaleLayout.getWidth() / mScaleLayout.getParent().getWidth();
float toY = (float) mScaleLayout.getHeight() / mScaleLayout.getParent().getHeight();
ScaleAnimation scaleAnimation = new ScaleAnimation(fromX, toX, fromY, toY, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(500);
mScaleLayout.startAnimation(scaleAnimation);
}
});
四、总结
缩放动画是一种常见的动画效果,可以让界面变得更加生动。android缩放动画可以很方便地通过ScaleAnimation类来实现,并且可以根据需求进行自定义。在实际开发应用中,需要根据界面效果的要求来选择不同的动画实现方法,以达到最佳的用户交互效果。
原创文章,作者:ZMYGH,如若转载,请注明出处:https://www.506064.com/n/331423.html
微信扫一扫
支付宝扫一扫