Android 当片段显示在活动的布局中时,如何开始转换是最佳实践吗?
我有一个活动,其中只包含用于渲染内部片段的FrameLayout StartActivity.javaAndroid 当片段显示在活动的布局中时,如何开始转换是最佳实践吗?,android,animation,android-fragments,transition,Android,Animation,Android Fragments,Transition,我有一个活动,其中只包含用于渲染内部片段的FrameLayout StartActivity.java public class StartActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start);
public class StartActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
final ViewGroup group = (ViewGroup)findViewById(R.id.fragment_container);
IntroFragment fragment = new IntroFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_container, fragment);
ft.commit();}
activity_start.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/fragment_container">
ChangeBounds转换的问题是,当片段被渲染时,OnGlobalYoutListener调用,TransitionManager将场景从scene1更改为scene2。除了ChangeBounds外,淡入淡出转换效果非常好。ChangeBounds根本不起作用,就像我没有这个代码一样
ChangeBounds bounds = new ChangeBounds();
set.addTransition(fadeIn);
为什么会发生
哦,对不起,下面两个场景
scene_one.xml
<RelativeLayout
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:visibility="invisible"
android:layout_marginTop="40dp"
android:id="@+id/circle_logo"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/circle_logo"/>
<TextView
android:visibility="invisible"
android:id="@+id/title_logo"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="25dp"
android:text="Booking" />
</RelativeLayout>
scene_two.xml
<RelativeLayout
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/circle_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/circle_logo" />
<TextView
android:id="@+id/title_logo"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/circle_logo"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="25dp"
android:text="Booking" />
</RelativeLayout>
这个代码帮助了我
public class StartActivity extends Activity {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
final ViewGroup group = (ViewGroup)findViewById(R.id.fragment_container);
scheduleStartPostponedTransition(group);
IntroFragment fragment = new IntroFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_container, fragment);
ft.commit();
}
private void scheduleStartPostponedTransition(final View sharedElement) {
sharedElement.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
sharedElement.getViewTreeObserver().removeOnPreDrawListener(this);
startPostponedEnterTransition();
return true;
}
});
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public void startPostponedEnterTransition(){
ViewGroup root = (ViewGroup) findViewById(R.id.scene_root);
final Scene scene2 = Scene.getSceneForLayout(root, R.layout.intro_scene_two, getApplicationContext());
final TransitionSet set = new TransitionSet();
set.setDuration(10000);
set.setOrdering(TransitionSet.ORDERING_TOGETHER);
set.addTarget(R.id.circle_logo);
set.addTarget(R.id.title_logo);
Fade fadeIn = new Fade(Fade.IN);
ChangeBounds bounds = new ChangeBounds();
set.addTransition(fadeIn);
set.addTransition(bounds);
TransitionManager.go(scene2, set);
}
我从本文中获得了想法和实现
public class StartActivity extends Activity {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
final ViewGroup group = (ViewGroup)findViewById(R.id.fragment_container);
scheduleStartPostponedTransition(group);
IntroFragment fragment = new IntroFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_container, fragment);
ft.commit();
}
private void scheduleStartPostponedTransition(final View sharedElement) {
sharedElement.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
sharedElement.getViewTreeObserver().removeOnPreDrawListener(this);
startPostponedEnterTransition();
return true;
}
});
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public void startPostponedEnterTransition(){
ViewGroup root = (ViewGroup) findViewById(R.id.scene_root);
final Scene scene2 = Scene.getSceneForLayout(root, R.layout.intro_scene_two, getApplicationContext());
final TransitionSet set = new TransitionSet();
set.setDuration(10000);
set.setOrdering(TransitionSet.ORDERING_TOGETHER);
set.addTarget(R.id.circle_logo);
set.addTarget(R.id.title_logo);
Fade fadeIn = new Fade(Fade.IN);
ChangeBounds bounds = new ChangeBounds();
set.addTransition(fadeIn);
set.addTransition(bounds);
TransitionManager.go(scene2, set);
}