, . , - , , , , , . , . onCreate(), , .
"ScreenTransitionLab" "android.example" , , , , , . , .
, , :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#0000FF"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btnForwards"
android:text="Forwards" />
</LinearLayout>
</LinearLayout>
, , :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#0000FF"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btnForwards"
android:text="Forwards" />
</LinearLayout>
</LinearLayout>
ScreenTransitionsLab, , "" NewScreen:
package android.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class ScreenTransitionLab extends Activity {
protected LinearLayout mainLayout;
public static Button btnForwards = null;
public static Activity currentActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentActivity = this;
mainLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main,
null);
btnForwards = (Button) mainLayout.findViewById(R.id.btnForwards);
btnForwards.setOnClickListener(forwardsOnClickListener);
UIHelper.setSlideDirection(mainLayout, UIHelper.bottom);
setContentView(mainLayout);
}
public View.OnClickListener forwardsOnClickListener = new View.OnClickListener() {
public void onClick(View v) {
Activity currentActivity = (Activity) v.getContext();
Intent i = new Intent(currentActivity, NewScreen.class);
currentActivity.startActivity(i);
currentActivity.finish();
}
};
}
NewScreen, , "" ScreenTransitionsLab:
package android.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class NewScreen extends Activity {
protected LinearLayout mainLayout;
public static Button btnBackwards = null;
public static Activity currentActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentActivity = this;
mainLayout = (LinearLayout) getLayoutInflater().inflate(
R.layout.new_screen, null);
btnBackwards = (Button) mainLayout.findViewById(R.id.btnBackwards);
btnBackwards.setOnClickListener(backwardsOnClickListener);
UIHelper.setSlideDirection(mainLayout, UIHelper.top);
setContentView(mainLayout);
}
public View.OnClickListener backwardsOnClickListener = new View.OnClickListener() {
public void onClick(View v) {
Activity currentActivity = (Activity) v.getContext();
Intent i = new Intent(currentActivity, ScreenTransitionLab.class);
currentActivity.startActivity(i);
currentActivity.finish();
}
};
}
UIHelper, :
package android.example;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
public class UIHelper {
public static final int top = 1;
public static final int bottom = 2;
public static final int left = 3;
public static final int right = 4;
public static void setSlideDirection(ViewGroup panel, int fromDirection) {
float fromX = 0;
float toX = 0;
float fromY = 0;
float toY = 0;
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(100);
set.addAnimation(animation);
switch (fromDirection) {
case top:
fromX = 0.00f;
toX = 0.00f;
fromY = -1.00f;
toY = 0.00f;
break;
case bottom:
fromX = 0.00f;
toX = 0.00f;
fromY = 1.00f;
toY = 0.00f;
break;
case left:
fromX = -1.00f;
toX = 0.00f;
fromY = 0.00f;
toY = 0.00f;
break;
default:
fromX = 1.00f;
toX = 0.00f;
fromY = 0.00f;
toY = 0.00f;
break;
}
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, fromX,
Animation.RELATIVE_TO_SELF, toX, Animation.RELATIVE_TO_SELF,
fromY, Animation.RELATIVE_TO_SELF, toY);
animation.setDuration(200);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(
set, 0.25f);
panel.setLayoutAnimation(controller);
}
}