动画边框半径

时间:2018-07-14 10:13:16

标签: android android-layout android-animation

我正在尝试实现其中一个动画,其中按钮的边框半径应及时更改。我对价值动画师的概念感到困惑。这是我的代码。

shape.xml文件

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@color/colorPrimary"/>
        <corners android:radius="5dp"/>
    </shape>
我视图中的

按钮

<Button
    android:id="@+id/button"
    style="@android:style/Widget.Material.Button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="12dp"
    android:background="@drawable/shape"
    android:onClick="onclick"
    android:text="@string/click_me"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintCircleRadius="0dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:targetApi="lollipop" />

1 个答案:

答案 0 :(得分:2)

public class ExampleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);

        findViewById(R.id.button).setOnClickListener(listener);

    }

    private final View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final float fromRadius = getResources().getDimension(R.dimen.start_corner_radius);
            final float toRadius = v.getHeight() / 2;
            final GradientDrawable gd = (GradientDrawable) v.getBackground();
            final ValueAnimator animator = ValueAnimator.ofFloat(fromRadius, toRadius);
            animator.setDuration(2000)
                    .addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            float value = (float) animation.getAnimatedValue();
                            gd.setCornerRadius(value);
                        }
                    });
            animator.start();
        }
    };
}