Android:如何在动画后保留动画

时间:2015-06-23 17:07:40

标签: android animation

我在animation fragments之间滑动时实施ViewPager。 当幻灯片转到新的fragment时,我想scale view,并且开始时set of animation包括alphatranslate 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);

1 个答案:

答案 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);