Rotating image. Animation list or animated rotate? (Android)

前端 未结 6 796
甜味超标
甜味超标 2020-11-30 21:17

I want to create a rotating progress image, and wonder what\'s the best way to proceed. I can make it work with an animation list with for example 12 images changing every 1

6条回答
  •  萌比男神i
    2020-11-30 22:19

    I found vokilam's answer to be the best one to create a nice stepped/staggered animation. I went for his final suggestion and made a custom widget, the only problem I encountered was that setting visibility wouldn't work because it was animated and thus would always be visible...

    I adjusted his code (ProgressView.java which I renamed StaggeredProgress.java) like this:

    public class StaggeredProgress extends ImageView {
    
    private Animation staggered;
    
    public StaggeredProgress(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setAnimation(attrs);
    }
    
    public StaggeredProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        setAnimation(attrs);
    }
    
    public StaggeredProgress(Context context) {
        super(context);
    }
    
    private void setAnimation(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.StaggeredProgress);
        int frameCount = a.getInt(R.styleable.StaggeredProgress_frameCount, 12);  
        int duration = a.getInt(R.styleable.StaggeredProgress_duration, 1000);
        a.recycle();
    
        setAnimation(frameCount, duration);
    }
    
    public void setAnimation(final int frameCount, final int duration) {
        Animation a = AnimationUtils.loadAnimation(getContext(), R.anim.progress_anim);
        a.setDuration(duration);
        a.setInterpolator(new Interpolator() {
    
            @Override
            public float getInterpolation(float input) {
                return (float)Math.floor(input*frameCount)/frameCount;
            }
        });
        staggered = a;
        //startAnimation(a);
    }
    
    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if( visibility == View.VISIBLE )
            startAnimation(staggered);
        else
            clearAnimation();
    
    }
    
    
    }
    

    This way setting the view's visibility starts and stops the animation as required...Many thanks again to vokilam!

提交回复
热议问题