我正在尝试实现跟随和编辑文本,当验证失败时,编辑文本行将颜色更改为红色,颜色更改从中心向边缘动画。
我将视图设置为,隐藏编辑文本底线,在编辑文本下方添加视图以显示该行
<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>
我尝试了多种方法,但仍然无法完全正确地进入。这是我到目前为止所尝试的内容
在设置背景之前使用缩放动画并开始动画。但在这种情况下,线条正在重新绘制,而不仅仅是改变颜色
我尝试过使用值动画,但我找不到适当的方法来应用自定义动画而不是淡入淡出行为。
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();
我真的很感激任何帮助,我可以为我尝试的内容添加更多代码,我不想让问题太长。 Thnanks
答案 0 :(得分: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();
}
}