淡出动画作品,但相反的淡入动画不 [英] Fade out animation works but opposite fade in animation does not

查看:223
本文介绍了淡出动画作品,但相反的淡入动画不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        int imgSize = 30;

        final ShapeDrawable redDot = new ShapeDrawable(new OvalShape());
        redDot.getPaint().setColor(Color.RED);
        redDot.setIntrinsicHeight(imgSize);
        redDot.setIntrinsicWidth(imgSize);

        final Bitmap bitmap = Bitmap.createBitmap(imgSize, imgSize, Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(bitmap); 
        redDot.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        redDot.draw(canvas);

        ImageView imgAnimArea = new ImageView(getActivity());
        imgAnimArea.setImageBitmap(bitmap);
        imgAnimArea.setScaleType(ImageView.ScaleType.CENTER);

        animationsView.addView(imgAnimArea);

        final AnimationSet animSetRedDot = new AnimationSet(true);

//      animSetRedDot.setFillAfter(true);
//      animSetRedDot.setFillEnabled(true);

        Animation aniRepeatFadeIn = null;
        Animation aniRepatFadeOut = null;

        // fade out
        aniRepatFadeOut = new AlphaAnimation(1, 0);
        aniRepatFadeOut.setStartOffset(3000);
        aniRepatFadeOut.setDuration(300);
        animSetRedDot.addAnimation(aniRepatFadeOut);

        // fade out animation works only if remove this part
        aniRepeatFadeIn = new AlphaAnimation(0, 1);
        aniRepeatFadeIn.setStartOffset(6000);
        aniRepeatFadeIn.setDuration(300);
        animSetRedDot.addAnimation(aniRepeatFadeIn);


        imgAnimArea.startAnimation(animSetRedDot);

其简单的code(至少是部分动画),但很奇怪的行为。基本上动画之前它会创建一个形状(红色的小圆圈),将其转换为位图,并把它添加到 animationsView (的FrameLayout)为的ImageView 的源( imgAnimArea )。结果
所以,我的红点淡出,但从未出现回来了,它只是部分褪色的火灾比淡出甚至删除你淡出后的工作,以防万一。我试图还设置淡出.5f,而不是0。在这种情况下,它淡出知名度的一半。结果
我也曾经试图动画 animationsView ,但结果是相同的 - 唯一的淡出部分工作,如果添加的部分不褪色,如果添加淡入部分,那么整个动画不起作用在所有。结果
我可以看到没有动画在所有加入的形状。此外,我可以在任何情况下,动画完成后看到它。启用或禁用FillAfter没有任何作用的。结果
所以,问题是什么错在这里?为什么淡入动画不能正常工作?为什么整个动画没有工作,如果添加淡入动画?

Its simple code (at least the animation part) but has very strange behavior. Basically before animation it creates a shape (small red circle) converts it to a bitmap and adds it to animationsView(FrameLayout) as ImageView's source (imgAnimArea).
So my red dot fades out but never appears back and it works only in case the fade in part is removed even thou the fade in fires later than fade out. I was trying also to set fade out to .5f instead of 0. In this case it fades out a half of visibility.
Also I had tried to animate animationsView but result is the same - only fade out part works if no fade in part added and if fade in part added then whole animation doesn't work at all.
I could see the shape with no animation added at all. Also I could see it after animation finishes in any case. Enabling or disabling FillAfter has no effect at all.
So the question is whats wrong here? Why fade in animation does not work? Why the whole animation does not work if fade in animation added?

推荐答案

在这里你有两种方法如何做到这一点。

here you have two ways how to do it (see if statement inside onClick() method)

一个是preferred人们不,是你的选择。

one is preferred one not, the choice is yours

final TextView tv = new TextView(this);
tv.setText("click me");
tv.setTextSize(40);
tv.setTextColor(0xffeeeeee);
tv.setBackgroundColor(0xaa00ff00);
tv.setGravity(Gravity.CENTER);
OnClickListener l = new OnClickListener() {
    @Override
    public void onClick(View v) {
        Animation a;

        boolean preferred = true;
        if (preferred) {
            Log.d(TAG, "onClick using custom Interpolator, preferred way");
            // this is a preferred way: custom Interpolator
            a = new AlphaAnimation(0,  1);
            Interpolator i = new Interpolator() {
                @Override
                public float getInterpolation(float input) {
                    return (float) (1 - Math.sin(input * Math.PI));
                }
            };
            a.setInterpolator(i);
        } else {
            Log.d(TAG, "onClick using AnimationSet, NOT preferred way");
            AnimationSet set = new AnimationSet(true);

            AlphaAnimation alpha0 = new AlphaAnimation(1, 0);
            alpha0.setDuration(1000);
            alpha0.setFillEnabled(true);
            alpha0.setFillBefore(false);
            alpha0.setFillAfter(false);
            set.addAnimation(alpha0);

            AlphaAnimation alpha1 = new AlphaAnimation(0, 1);
            alpha1.setDuration(1000);
            alpha1.setFillEnabled(true);
            alpha1.setFillBefore(false);
            alpha1.setFillAfter(false);
            alpha1.setStartOffset(1000);
            set.addAnimation(alpha1);
            a = set;
        }
        a.setDuration(2000);
        tv.startAnimation(a);
    }
};
tv.setOnClickListener(l);
setContentView(tv);

这篇关于淡出动画作品,但相反的淡入动画不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆