第一种使用方式。。可能习惯原来ViewGroup的 比较熟悉点。。这样其实也不容易出错。。。
RelativeLayout sceneBase = (RelativeLayout)findViewById(R.id.scene_base);
//Create a new layout for the second scene
ViewGroup scene2Group = (ViewGroup)getLayoutInflater().inflate(R.layout.scene_2, sceneBase, false);
//Create a scene using the root element from the initial scene
//plus the new group we just created
final Scene scene2 = new Scene(sceneBase, scene2Group);
//When the user clicks the button transition from scene1 to scene2
Button button = (Button)sceneBase.findViewById(R.id.press_me);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TransitionManager.go(scene2);
}
});
android 4.4 提供了一个新的,更简便地向ViewGroup塞东西的方式
container = (ViewGroup)findViewById(R.id.scene_base);
current = Scene.getSceneForLayout(container, R.layout.scene1, this);
another = Scene.getSceneForLayout(container, R.layout.scene2, this);
current.enter();
bt2 =(Button)findViewById(R.id.press_me);
bt2.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Scene tem = current;
current = another;
another = tem;
TransitionManager.go(current);
}
});
注意这个过程是不可逆的!!!!,除非你把改变他们的触发方式写到外面,也就是说这里的Button按钮也会改变的话,就无法再变回来了。。。因为它用的是 inflater 里面的用新的xml文件替换掉原来的父布局李敏啊的组件。。。。
下面是完整的代码
首先创建一个 名为scene1.xml的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scene_base"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/welcome_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This is the first scene!"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/press_me"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/welcome_text"
android:text="Press me!"
android:gravity="center_horizontal"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
再创建一个名为 scene2.xml的文件,没错这个就是替换后的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scene_base"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/welcome_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This is the second scene"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/second_line"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="It contains 2 views with the same IDs as scene_1"
android:layout_below="@id/welcome_text"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/third_line"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="But it also contains two new lines of text"
android:layout_below="@id/second_line"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/press_me"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="I'm done"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
最后是逻辑的部分。有两个版本。。我都附上
版本一,也是新的SDK中推荐的。。。但我个人觉得这样虽然简单了。。。但细节上不可控
package com.qingluan.testsome;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.transition.Scene;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.transition.TransitionManager;
public class MainActivity extends Activity {
private ViewGroup container;
private TextView thisis;
private Button bt2;
private Scene current;
private Scene another;
private Transition mytransition;
private Integer count;
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scene1); // 注意这里不是用的默认布局文件而是我们写的第一个文件
count = 0;
Kitkat();
bt2 =(Button)findViewById(R.id.press_me);
bt2.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Scene tem = current;
current = another;
another = tem;
TransitionManager.go(current);
}
});
}
@TargetApi(19)
public void Kitkat (){
container = (ViewGroup)findViewById(R.id.scene_base);
current = Scene.getSceneForLayout(container, R.layout.scene1, this);
another = Scene.getSceneForLayout(container, R.layout.scene2, this);
current.enter();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
第二个版本 ,用的是我们熟悉的 Inflater.inflate(int,root ,boolean) 工厂模式模式
package com.qingluan.testsome;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.transition.Scene;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.transition.TransitionManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set the layout to the initial scene
setContentView(R.layout.scene1);//这里不是默认xml
//这里将需要更改的父布局的框架先声明出来,等会需要改变现在的这个布局里面的东西
RelativeLayout sceneBase = (RelativeLayout)findViewById(R.id.scene_base);
//然后在这里把改变后的布局文件塞入我们的工厂中,特别注意的是‘inflate’ 的第三个变量必须是false,否则会立刻把里面的东西立刻塞入布局中,最后的结果是,还没等我们选择转换,就已经转换了,而这里的第一个参数就是改变后的布局,它会呗组装进入第二参数里面。。
ViewGroup scene2Group = (ViewGroup)getLayoutInflater().inflate(R.layout.scene2, sceneBase, false);
创建要转换的第二个屏幕(暂时这样翻译)其中第一个参数将变为第二个参数的父布局。
final Scene scene2 = new Scene(sceneBase, scene2Group);
//When the user clicks the button transition from scene1 to scene2
Button button = (Button)sceneBase.findViewById(R.id.press_me);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TransitionManager.go(scene2); //这个就是最重要的转换函数。。。而第二个参数是隐藏的,那个参数是为了自己定制自己的转换特效而设置的,这里就暂时不叙述了
}
});
}
有不对的地方希望指出。。。最后要小心。。这样转换有一个不方便的地方,就是,如果你使用了原来诸如 TextView.setText()之类的函数的话,只要转换就会初始化为XML文件中的内容。。,甚至你的setText直接失效,至于解决办法目前我还没测试出来,有会的朋友希望尽快联系我 最后 谢!
———— by 青鸾之旅
本文详细介绍如何在Android应用中实现场景之间的平滑过渡,包括使用Scene类进行布局切换的方法及注意事项。

3087

被折叠的 条评论
为什么被折叠?



