2015-12-15 22 views
0

我试图实现以下和编辑文本,当验证失败时,编辑文本行将颜色更改为红色,颜色更改从中心向边缘生成动画。如何将动画应用于Android中的视图的setBackground颜色属性?

我认为设置为,隐藏编辑文本的底线,下面添加编辑文本视图来显示行

<android.support.design.widget.TextInputLayout 
    android:id="@+id/fpet_text_input_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="left" 
    android:orientation="horizontal" 
    app:errorEnabled="false" 
    app:hintAnimationEnabled="true" 
    android:theme="@style/TextLabel"> 

    <EditText 
     android:id="@+id/fpet_edit_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColorHighlight="@color/blue" 
     android:background="@null" 
     android:textColorLink="@color/blue" 
     android:paddingTop="5dp" 
     android:textSize="18dp" 
     /> 


    </android.support.design.widget.TextInputLayout> 


    <ImageView 
    android:id="@+id/fpet_line" 
    android:layout_width="match_parent" 
    android:layout_height="2dp" 
    android:layout_marginTop="5dp" 
    android:layout_below="@id/fpet_text_input_layout" 
    android:background="@color/grey"></ImageView> 

我已经尝试了多种方法,但仍无法在完全正确拿到。以下是我迄今

  1. 使用规模的动画尝试和做设定背景前开始动画。但在这种情况下,线正在重绘,而不是只是改变颜色

  2. 我试过使用值动画,但我找不到适当的方式来应用自定义动画,而不是淡入淡出的行为。

    Integer colorFrom = getResources().getColor(R.color.grey); 
    Integer colorTo = getResources().getColor(color); 
    ValueAnimator colorAnimator = ValueAnimator.ofObject(new ArgbEvaluator(),  colorFrom, colorTo); 
    colorAnimator.setDuration(1000); 
    colorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()  { 
    
    @Override 
    public void onAnimationUpdate(ValueAnimator animator) { 
        underLine.setBackgroundColor((Integer)animator.getAnimatedValue()); 
    } 
    
    }); 
    colorAnimator.start(); 
    

enter image description here

我真的很感激任何帮助,我能为我曾尝试添加更多的代码,我不想做的问题太长。 Thanks

回答

2

我试图达到你想要的效果。

public class AnimatedEditText extends EditText { 
    private Paint paint; 
    private Rect rect; 

    public AnimatedEditText(Context context) { 
     super(context); 
    } 

    public AnimatedEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public AnimatedEditText(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public AnimatedEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    protected void onFinishInflate() { 
     super.onFinishInflate(); 
     paint = new Paint(); 
     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP)); 
     paint.setColor(Color.RED); 
     rect = new Rect(); 
     setLayerType(LAYER_TYPE_SOFTWARE, null); 
    } 

    public void playAnimation() { 
     rect.top = getMeasuredHeight() - getResources().getDimensionPixelSize(R.dimen.line_offset); 
     rect.bottom = getMeasuredHeight(); 
     rect.left = getMeasuredWidth()/2; 
     rect.right = getMeasuredWidth()/2; 
     ValueAnimator animator = ValueAnimator.ofPropertyValuesHolder(PropertyValuesHolder.ofInt("left", rect.left, 0), PropertyValuesHolder.ofInt("right", rect.right, getMeasuredWidth())); 
     animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
      @Override 
      public void onAnimationUpdate(ValueAnimator animation) { 
       rect.left = (int) animation.getAnimatedValue("left"); 
       rect.right = (int) animation.getAnimatedValue("right"); 
      } 
     }); 
     animator.setDuration(500); 
     animator.start(); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     canvas.drawRect(rect, paint); 
     invalidate(); 
    } 
} 
+0

工程就像魅力!非常感谢你,这非常有帮助! –

相关问题