我如何在 Android 中制作动态翻转屏幕(如 iPhone 的屏幕)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2666966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i make a dynamic flipping screen(like that of iPhone) in Android
提问by Muhammad Maqsoodur Rehman
I am parsing data through the web service. I want the flipping horizontally rather than vertically. Here is a tutorial where ViewFlipper is used but it is for static data.
我正在通过网络服务解析数据。我想要水平翻转而不是垂直翻转。这是一个使用 ViewFlipper 的教程,但它用于静态数据。
Here is our code where we need flipping between 2 activities:
这是我们需要在 2 个活动之间切换的代码:
Splash.java
飞溅.java
public class Splash extends Activity{
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startActivity(new Intent(Splash.this, MainMenu.class));
Splash.this.finish();
}
}
Splash.xml
飞溅.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/splash">
</AbsoluteLayout>
Menu.java
菜单.java
public class Menu extends Activity{
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
}
}
Menu.xml
菜单文件
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/menu">
</AbsoluteLayout>
回答by stealthcopter
You can add pages to a ViewFlipper dynamically using addView.
您可以使用 addView 动态地将页面添加到 ViewFlipper。
flipper= (ViewFlipper) findViewById(R.id.flipper1);
flipper.addView(myView,myViewIndex);
Where myView is the view you wish to add, and myViewIndex is the index in the viewflipper you want to add this new view.
其中 myView 是您要添加的视图,而 myViewIndex 是您要添加此新视图的 viewflipper 中的索引。
You can then set the animations to preform on changing of views:
然后,您可以将动画设置为在更改视图时执行:
flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));
Then to flip to this page you can use:
然后翻到这个页面,你可以使用:
flipper.setDisplayedChild(myViewIndex);
Where left_in.xml is defined as
其中 left_in.xml 定义为
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:interpolator="@android:anim/decelerate_interpolator"
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="300"
/>
</set>
and left_out.xml is:
和 left_out.xml 是:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
>
<translate
android:interpolator="@android:anim/decelerate_interpolator"
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="300"
/>
</set>
回答by stealthcopter
Just had a quick look for you because I was sure I've seen it around before, I found thison developer.android.com:
刚刚快速找了找你,因为我确定我以前见过它,我在 developer.android.com 上找到了这个:
public void overridePendingTransition (int enterAnim, int exitAnim)
Since: API Level 5
Call immediately after one of the flavors of startActivity(Intent) or finish() to specify an explicit transition animation to perform next.
Parameters
enterAnim A resource ID of the animation resource to use for the incoming activity. Use 0 for no animation.
exitAnim A resource ID of the animation resource to use for the outgoing activity. Use 0 for no animation.
So you can add this extra parameter to your intent which will define how the activity you call will animate on entrance of new activity and exit of old activity.
因此,您可以将此额外参数添加到您的意图中,这将定义您调用的活动在新活动进入和旧活动退出时将如何动画化。
回答by Blundell
If you want to navigate between two activities and not use a view flipper you can use normal intents. Wrote a tutorial so that you can animate your activity's in and out,
如果您想在两个活动之间导航而不使用视图翻转器,您可以使用普通意图。写了一个教程,这样你就可以动画你的活动进出,
Enjoy:
享受:
回答by snctln
Do you just want the screen to rotate based off of the position it is being held in (as determined by the accelerometer sensors)?
您是否只希望屏幕根据其保持的位置(由加速度计传感器确定)旋转?
If so just add
如果是这样,只需添加
android:screenOrientation="sensor"
to the Activity node in your AndroidManifest.xml file
到您的 AndroidManifest.xml 文件中的 Activity 节点