Android: animate a rectangle

I cut 5 corners of the internet without big pieces.

I am trying to create an animated rectangle to change its length. when he moves to the next length, I want him to animate the new length.

I draw a rectangle as follows:

    Paint rectanglePaint = new Paint();
    rectanglePaint.setARGB(255, 0, 0, 255);
    rectanglePaint.setStrokeWidth(2);
    rectanglePaint.setStyle(Style.FILL);

    Rect rectangle = new Rect(1, 1, 200, 20);
    canvas.drawRect(rectangle, rectanglePaint);

However, I'm not sure how to add ScaleAnimation to the above. I also want to generate only Java code.

Can anyone help?

+5
source share
1 answer

You must add a view with your rectangle to the layout.

Create a scale_anim.xml file in your code folder

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromXScale="1.0" android:toXScale="3.0"
   android:fromYScale="1.0" android:toYScale="3.0"
   android:pivotX="0"
   android:pivotY="0"
   android:interpolator="@android:anim/linear_interpolator"
   android:duration="700" android:fillAfter="true" />

And in your code you can set the animation using this code

Animation scaleAnimation = AnimationUtils.loadAnimation(this
        , R.anim.scale_anim.xml);
layout.startAnimation(scaleAnimation);
0
source

All Articles