android:如何在放大动画时停止重复

时间:2017-07-14 05:06:10

标签: android android-layout android-animation

以下是我的zoom_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" >
    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromXScale="0"
        android:fromYScale="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1"
        android:toYScale="1" >
    </scale>
</set>

视图放大三次。我怎么能只做一次

PS:android:repeatCount="1"无效。

编辑1:我的动画工具包有加载动画如下

public static Animation loadAnimation(Context context, @AnimRes int id)
        throws NotFoundException {

    XmlResourceParser parser = null;
    try {
        parser = context.getResources().getAnimation(id);
        return createAnimationFromXml(context, parser);
    } catch (XmlPullParserException ex) {
        NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +
                Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } catch (IOException ex) {
        NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +
                Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } finally {
        if (parser != null) parser.close();
    }

现在我将其称为

zoomIn = loadAnimation(getContext(),
            R.anim.zoom_in);
view.startAnimation(zoomIn);

2 个答案:

答案 0 :(得分:1)

将重复次数设为0

zoomIn = loadAnimation(getContext(), R.anim.zoom_in);
zoomIn.setRepeatCount(0);

AnimationListener添加到zoomIn,如下所示

zoomIn.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // clear animation here
        view.clearAnimation();
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});

希望这会有所帮助。

答案 1 :(得分:0)

使用&#34;设置&#34;试试&#34; objectAnimator&#34; :

 <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="0"
    android:fromYScale="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1"
    android:toYScale="1"
    android:repeatCount="1" />