Frage

        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?

War es hilfreich?

Lösung

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);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top