android属性动画--场景过过渡动画

当从一个布局切换到另一个布局时的动画,先看效果:


当从scene1切换到其他scenc时候,可以设置动画,代码:

/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.apis.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.transition.Scene;
import android.transition.TransitionInflater;
import android.transition.TransitionManager;
import com.example.android.apis.R;

/**
 * This application demonstrates some of the capabilities and uses of the
 * {@link android.transition transitions} APIs. Scenes and a TransitionManager
 * are loaded from resource files and transitions are run between those scenes
 * as well as a dynamically-configured scene.
 */
public class Transitions extends Activity {//控制过度效果

    Scene mScene1, mScene2, mScene3;
    ViewGroup mSceneRoot;
    TransitionManager mTransitionManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.transition);

        mSceneRoot = (ViewGroup) findViewById(R.id.sceneRoot);

        TransitionInflater inflater = TransitionInflater.from(this);

        // Note that this is not the only way to create a Scene object, but that
        // loading them from layout resources cooperates with the
        // TransitionManager that we are also loading from resources, and which
        // uses the same layout resource files to determine the scenes to transition
        // from/to.
        mScene1 = Scene.getSceneForLayout(mSceneRoot, R.layout.transition_scene1, this);
        mScene2 = Scene.getSceneForLayout(mSceneRoot, R.layout.transition_scene2, this);
        mScene3 = Scene.getSceneForLayout(mSceneRoot, R.layout.transition_scene3, this);
        mTransitionManager = inflater.inflateTransitionManager(R.anim.transitions_mgr, //过渡的动画在transitions_mgr中
                mSceneRoot);
    }

    public void selectScene(View view) {
        switch (view.getId()) {
            case R.id.scene1:
                mTransitionManager.transitionTo(mScene1);//切换到scenc1
                break;
            case R.id.scene2:
                mTransitionManager.transitionTo(mScene2);//切换到scenc2
                break;
            case R.id.scene3:
                mTransitionManager.transitionTo(mScene3);//切换到scenc4
                break;
            case R.id.scene4://切换到scenc4
                // scene4 is not an actual 'Scene', but rather a dynamic change in the UI,
                // transitioned to using beginDelayedTransition() to tell the TransitionManager
                // to get ready to run a transition at the next frame
                TransitionManager.beginDelayedTransition(mSceneRoot);
                setNewSize(R.id.view1, 150, 25);
                setNewSize(R.id.view2, 150, 25);
                setNewSize(R.id.view3, 150, 25);
                setNewSize(R.id.view4, 150, 25);
                break;
        }
    }

    private void setNewSize(int id, int width, int height) {
        View view = findViewById(id);
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }
}

相应的布局文件transition.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2013 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

        <RadioButton android:id="@+id/scene1"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:checked="true"
                     android:onClick="selectScene"
                     android:text="Scene 1" />

        <RadioButton android:id="@+id/scene2"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:onClick="selectScene"
                     android:text="Scene 2" />

        <RadioButton android:id="@+id/scene3"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:onClick="selectScene"
                     android:text="Scene 3" />

        <RadioButton android:id="@+id/scene4"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:onClick="selectScene"
                     android:text="Scene 4" />

    </RadioGroup>

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/sceneRoot">

        <include layout="@layout/transition_scene1"/>

    </LinearLayout>
</LinearLayout>


场景文件transition_scene1.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2013 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/container">

    <View
            android:layout_width="100dip"
            android:layout_height="50dip"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:background="#f00"
            android:id="@+id/view1"/>

    <View
            android:layout_width="100dip"
            android:layout_height="50dip"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:background="#0f0"
            android:id="@+id/view2"/>

    <View
            android:layout_width="100dip"
            android:layout_height="50dip"
            android:background="#00f"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:id="@+id/view3"/>

    <View
            android:layout_width="100dip"
            android:layout_height="50dip"
            android:background="#0ff"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:id="@+id/view4"/>

</RelativeLayout>

transition_scene3.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2013 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/container">

    <View
            android:layout_width="100dip"
            android:layout_height="50dip"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:background="#f00"
            android:id="@+id/view1"/>

    <View
            android:layout_width="100dip"
            android:layout_height="50dip"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:background="#0f0"
            android:id="@+id/view2"/>

    <View
            android:layout_width="100dip"
            android:layout_height="50dip"
            android:background="#00f"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:id="@+id/view3"/>

    <View
            android:layout_width="100dip"
            android:layout_height="50dip"
            android:background="#0ff"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:id="@+id/view4"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:orientation="vertical"
        android:id="@+id/grayscaleContainer">

        <View
                android:layout_width="match_parent"
                android:layout_height="10dip"
                android:background="#000"
                android:id="@+id/gray1"/>
        <View
                android:layout_width="match_parent"
                android:layout_height="10dip"
                android:background="#222"
                android:id="@+id/gray2"/>
        <View
                android:layout_width="match_parent"
                android:layout_height="10dip"
                android:background="#444"
                android:id="@+id/gray3"/>
        <View
                android:layout_width="match_parent"
                android:layout_height="10dip"
                android:background="#666"
                android:id="@+id/gray4"/>
        <View
                android:layout_width="match_parent"
                android:layout_height="10dip"
                android:background="#888"
                android:id="@+id/gray5"/>
        <View
                android:layout_width="match_parent"
                android:layout_height="10dip"
                android:background="#aaa"
                android:id="@+id/gray6"/>
        <View
                android:layout_width="match_parent"
                android:layout_height="10dip"
                android:background="#ccc"
                android:id="@+id/gray7"/>
        <View
                android:layout_width="match_parent"
                android:layout_height="10dip"
                android:background="#fff"
                android:id="@+id/gray8"/>

    </LinearLayout>

</RelativeLayout>

过渡动画管理transitions_mgr.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2013 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<!-- BEGIN_INCLUDE(TransitionManager) -->
<transitionManager xmlns:android="http://schemas.android.com/apk/res/android">
    <transition android:fromScene="@layout/transition_scene1"
                android:toScene="@layout/transition_scene2"
                android:transition="@anim/changebounds"/>
    <transition android:fromScene="@layout/transition_scene2"
                android:toScene="@layout/transition_scene1"
                android:transition="@anim/changebounds"/>
    <transition android:toScene="@layout/transition_scene3"
                android:transition="@anim/changebounds_fadein_together"/>
    <transition android:fromScene="@layout/transition_scene3"
                android:toScene="@layout/transition_scene1"
                android:transition="@anim/changebounds_fadeout_sequential"/>
    <transition android:fromScene="@layout/transition_scene3"
                android:toScene="@layout/transition_scene2"
                android:transition="@anim/changebounds_fadeout_sequential"/>
</transitionManager>
<!-- END_INCLUDE(TransitionManager) -->
上面列出了从某某场景切换到另一个场景时的动画



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值