繁体   English   中英

如何在Android中以不同的速度制作多个同时查看动画的动画?

[英]How to animate multiple simultaneous view animations with differing speeds in Android?

我有四个要应用于视图的动画,两个要缩放以填充活动,另外两个要缩小视图然后隐藏。 我需要使用两个动画,因为否则动画会缩放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>

进行一些更改,然后检查是否正常工作

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM