开发者

Android: animate a rectangle

I've scowered the 5 corners of the internet without much of a nibble.

I'm trying to produce a animated rectangle to change its length. when it moves to the next length I want it to animate to the new length..

I'm drawing the 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, rectan开发者_如何转开发glePaint);

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

Can anyone help?


You should add the view with your rectangle to the layout.

Create a scale_anim.xml file in your anim folder with the code

<?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 with this code

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜