Fade out animation works but opposite fade in animation does not
Asked Answered
H

1

1
        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);

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?

Hundredth answered 6/10, 2013 at 5:51 Comment(2)
the code here looks good. it should start the fade-in animation 3 seconds after the fade out completed. Could you attach more code please? Where is this code being called from?Cyprian
Actually there is nothing to show. The code above is 99% of the method gets called. The only one and very 1st code line is missed - animationsViewWrapper.setVisibility(View.VISIBLE);Hundredth
W
2

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

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);
Wivinah answered 6/10, 2013 at 6:54 Comment(2)
thanx! the unpreferred way works, so the solution is to set Fill settings according to your answer. But whats more interesting - if I set it to animationSet it has no effect. Also it doesn't work with interpolator. My task is to 10 times show and hide the dot so Im using AnimationSet.Hundredth
If you want to show/hide ten times either use interpolator cos(input*pi*5)^2 or use 5 fadeout and 5 fadein animationsWivinah

© 2022 - 2024 — McMap. All rights reserved.