Android drawing an animated line

前端 未结 4 526
时光说笑
时光说笑 2020-12-23 10:24

I\'m currently working with graphics and paths, and I can successufully display whatever I want.

But instead of drawing a line directly on my SurfaceView, I\'d like

4条回答
  •  执念已碎
    2020-12-23 11:03

    You will have to add this view to the layout, setting height to 1 and width to match parent. The line will be animated from left to right. The later line will be placed over the first one.

                    public class AnimatorLineView extends RelativeLayout {
    
                        private View animatorLineView;
                        private View simpleLineView;
                        View animatorLine;
                        private int colorBeforeAnimation;
                        private int colorAfterAnimation;
                        private int colorForErrorLine;
    
                        public AnimatorLineView(Context context) {
                            super(context);
                            init();
                            startAnimation();
                        }
    
                        public AnimatorLineView(Context context, AttributeSet attrs) {
                            super(context, attrs);
                            init();
                            initAttributes(context, attrs);
                            setColors();
                            startAnimation();
                        }
    
                        public AnimatorLineView(Context context, AttributeSet attrs, int defStyleAttr) {
                            super(context, attrs, defStyleAttr);
                            init();
                            initAttributes(context, attrs);
                            setColors();
                            startAnimation();
                        }
    
    
                        private void setColors() {
                            simpleLineView.setBackgroundColor(colorBeforeAnimation);
                            animatorLine.setBackgroundColor(colorAfterAnimation);
                        }
    
                        public void init() {
                            animatorLineView = inflate(getContext(), R.layout.ainimator_line_view, this);
                            animatorLine = findViewById(R.id.simple_line);
                            simpleLineView = findViewById(R.id.animator_line);
    
                        }
    
                        public void setColor(int color) {
                            animatorLine.setBackgroundColor(color);
                        }
    
                        public void startAnimation() {
                            animatorLine.setVisibility(View.VISIBLE);
                            Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.enter_animation_underline);
                            animatorLine.startAnimation(animation);
                        }
    
                        public void showErrorLine(){
                            animatorLine.setBackgroundColor(colorForErrorLine);
                            animatorLine.setVisibility(View.VISIBLE);
                            Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.enter_animation_underline);
                            animatorLine.startAnimation(animation);
                        }
    
                        public void hideErrorLine(){
                            animatorLine.setBackgroundColor(colorAfterAnimation);
                            animatorLine.setVisibility(View.VISIBLE);
                            Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.enter_animation_underline);
                            animatorLine.startAnimation(animation);
                        }
    
                        private void initAttributes(Context context, AttributeSet attributeSet) {
                            TypedArray attr = getTypedArray(context, attributeSet, R.styleable.ProgressButton);
                            if (attr == null) {
                                return;
                            }
                            try {
                                colorBeforeAnimation = attr.getColor(R.styleable.AnimatorLineView_al_color_after_animation,ContextCompat.getColor(getContext(), R.color.animation_line_text_color));
                                colorAfterAnimation = attr.getColor(R.styleable.ProgressButton_pb_text_color, ContextCompat.getColor(getContext(), R.color.black_color));
                                colorForErrorLine = attr.getColor(R.styleable.ProgressButton_pb_text_color, ContextCompat.getColor(getContext(), R.color.error_msgs_text_color));
                            } finally {
                                attr.recycle();
                            }
                        }
    
                        protected TypedArray getTypedArray(Context context, AttributeSet attributeSet, int[] attr) {
                            return context.obtainStyledAttributes(attributeSet, attr, 0, 0);
                        }
    
                        public void resetColor(){
                            animatorLine.setBackgroundColor(colorAfterAnimation);
                            animatorLine.setVisibility(View.GONE);
                        }
                    }
    
                
    
                
    
                    
    
                    
    
                
    
            
    
            
            
                
            
        ---- styles------
          
    
            
                
                
                
            
    
    -------- to be include in the xml
     
    

提交回复
热议问题