Sushanth
Sushanth

Reputation: 1551

Animating Border radius

I'm trying to implement one of this animation where the border radius of the button should change on time. I'm confused with value animator concept.here is my code.

shape.xml file

<?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 in my view

<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" />

Upvotes: 5

Views: 5186

Answers (1)

user10053723
user10053723

Reputation:

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();
        }
    };
}

Upvotes: 9

Related Questions