如何在Android中以不同的速度为多个同步视图动画制作动画?

时间:2015-01-28 02:21:27

标签: android animation

我有四个要应用于视图的动画,两个用于缩放它以填充活动,两个用于缩小视图然后隐藏它。我需要使用两个动画,否则动画会缩放X& Y因此它们同时完成,Y比X快得多。

我用来增加视图效果的两个动画,但缩小背景的第二组动画根本不起作用。

向上扩展

onsite_background.setVisibility(View.VISIBLE);

Animation scaleYAnim = AnimationUtils.loadAnimation(context, R.anim.fill_y);
Animation scaleXAnim = AnimationUtils.loadAnimation(context, R.anim.fill_x);
AnimationSet scaleAnim = new AnimationSet(false);
scaleAnim.addAnimation(scaleXAnim);
scaleAnim.addAnimation(scaleYAnim);
scaleAnim.setFillEnabled(true);
scaleAnim.setFillAfter(true);

onsite_background.startAnimation(scaleAnim);

scaleAnim.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation arg0) {
    }

    @Override
    public void onAnimationRepeat(Animation arg0) {
    }

    @Override
    public void onAnimationEnd(Animation arg0) {
        Log.v("onAnimationEnd", "scaleAnim");
        mSignOuthButton.setTextColor(Color.WHITE);
    }
});

扩展XML

//fill_y.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">
    <scale
        android:duration="450"
        android:fromYScale="1.0"
        android:fromXScale="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toYScale="110%p"
        android:toXScale="1.0" />
</set>
//fill_x.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">
    <scale
        android:duration="300"
        android:fromYScale="1.0"
        android:fromXScale="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toYScale="1.0"
        android:toXScale="110%p" />
</set>

缩小(破碎)

Animation shrinkYAnim = AnimationUtils.loadAnimation(context, R.anim.shrink_y);
Animation shrinkXAnim = AnimationUtils.loadAnimation(context, R.anim.shrink_x);
AnimationSet shrinkAnim = new AnimationSet(false);
shrinkAnim.addAnimation(shrinkXAnim);
shrinkAnim.addAnimation(shrinkYAnim);
shrinkAnim.setFillEnabled(true);
shrinkAnim.setFillAfter(true);

onsite_background.startAnimation(shrinkAnim);

shrinkAnim.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation arg0) {
        mSignOuthButton.setTextColor(getResources().getColor(R.color.rc_grey));
    }

    @Override
    public void onAnimationRepeat(Animation arg0) {
    }

    @Override
    public void onAnimationEnd(Animation arg0) {
        Log.v("onAnimationEnd", "shrinkAnim");
        onsite_background.clearAnimation();
        onsite_background.setVisibility(View.GONE);
    }
});

缩小XML

// shrink_y.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">
    <scale
        android:duration="350"
        android:fromXScale="110%p"
        android:fromYScale="110%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toYScale="34dp"
        android:toXScale="110%p"/>
</set>


// shrink_x.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">
    <scale
        android:duration="300"
        android:fromXScale="110%p"
        android:fromYScale="110%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toYScale="110%p"
        android:toXScale="139dp"/>
</set>

根据@Madhu请求的活动

//activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:nestedScrollingEnabled="false"
    tools:context="com...MainActivity">


    <ImageView
        android:id="@+id/onsite_background"
        android:layout_width="1px"
        android:layout_height="44dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/onsite_rect"
        android:visibility="gone" />

    <Button
        android:id="@+id/start_work_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:minHeight="45dp"
        android:minWidth="150dp"
        android:text="@string/main_work_button_inactive"
        android:textColor="#fff"
        android:textSize="19sp"
        android:textStyle="bold" />

    <ProgressBar
        android:id="@+id/start_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="gone" />


    <Button
        android:id="@+id/log_out_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"

        android:background="#0000"
        android:paddingBottom="@dimen/button_inset_vertical_material"
        android:paddingLeft="@dimen/button_inset_horizontal_material"
        android:paddingRight="@dimen/button_inset_horizontal_material"

        android:paddingTop="@dimen/button_inset_vertical_material"
        android:text="@string/log_out_string"
        android:textColor="@color/rc_grey"
        android:textSize="16sp"
        android:textStyle="bold" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

进行这些更改并检查是否有效

activity_main.xml

<ImageView
        android:id="@+id/onsite_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ad_banner2"
        android:visibility="invisible" />

fill_y.xml

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

fill_x.xml

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

在活动课中写下这个,

{



        Animation scaleYAnim = AnimationUtils.loadAnimation(context, R.anim.fill_y);
        Animation scaleXAnim = AnimationUtils.loadAnimation(context, R.anim.fill_x);
        AnimationSet scaleAnim = new AnimationSet(false);
        scaleAnim.addAnimation(scaleXAnim);
        scaleAnim.addAnimation(scaleYAnim);
        scaleAnim.setFillEnabled(true);
        scaleAnim.setFillAfter(true);

        onsite_background.startAnimation(scaleAnim);

        scaleAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation arg0) {
                onsite_background.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
            }

            @Override
            public void onAnimationEnd(Animation arg0) {
                Log.v("onAnimationEnd", "scaleAnim");
                mSignOuthButton.setTextColor(Color.WHITE);
            }
        });
    }