我在animation
fragments
之间滑动时实施ViewPager
。
当幻灯片转到新的fragment
时,我想scale
view
,并且开始时set of animation
包括alpha
和translate
animation
但是当开始set
问题时,scale animation
的结果没有保留(视图返回到原始大小)。
我试过用
scaleAnimation.setFillAfter(true);
scaleAnimation.setFillEnabled(true);
但是当我没有开始上面的animation
时,它就会起作用,当我开始set
时,view
会恢复原始大小。
任何人都可以给我一个解决方案吗?
修改
首先我要按imageView
缩放animation
:
ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 1.3f, 1f, 1.3f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1);
scaleAnimation.setDuration(1000);
scaleAnimation.setFillAfter(true);
scaleAnimation.setFillEnabled(true);
然后我希望在缩放后,ImageView
仍然保持新的缩放比例并开始另一个Animation
:
private void startSetAnimation() {
AnimationSet set = new AnimationSet(true);
set.setDuration(3000);
set.setFillAfter(true);
float alphaS = 1f;
AlphaAnimation alpha = new AlphaAnimation(currentAlpha, alphaS);
alpha.setDuration(3000);
alpha.setFillAfter(true);
float scale = 0.1f;
TranslateAnimation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, currentTrans,
Animation.RELATIVE_TO_SELF, scale,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF , 0f);
translate.setFillAfter(true);
translate.setDuration(3000);
set.addAnimation(translate);
set.addAnimation(alpha);
ivHorizontal.startAnimation(set);
}
所以我将下面的代码添加到scale animation
以使其成功但不成功。
scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
startSetAnimation();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
ivHorizontal.startAnimation(scaleAnimation);
答案 0 :(得分:0)
有一个onAnimationEnd监听器可以在其他动画完成时启动某些动画。我到达计算机时会发布代码
你的imageView在缩放后没有保留新尺寸的原因是因为动画不是永久性的,你需要做的就是这个
scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// change the actual width and height so it doesn't go back to what it was
LayoutParams layoutParams = (LayoutParmas) ivHorizontal.getLayoutParams();
layoutParams.width = ivHorizental.getWidth();
layoutParams.height= ivHorizental.getHeight();
ivHorizontal.setLayoutParams(layoutParams);
ivHorizontal.requestLayout();
startSetAnimation();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
ivHorizontal.startAnimation(scaleAnimation);