淡入Android启动活动

时间:2015-03-12 14:45:43

标签: android animation

寻找帮助淡出我的发布活动。我发现很多帖子关于使用意图启动的活动之间的淡出,但没有关于如何淡化您的启动活动。我试过overridePendingTransition(int animationIn, int animationOut),但这对我没用。我已将其放在onCreate(..)方法和onStart(..)方法中,但未成功。任何帮助或指示将不胜感激。

感谢。

2 个答案:

答案 0 :(得分:5)

我为你做了一些事情,但它排除了动作栏。 我为一个简单的例子修改了hello world演示:

  1. 为您的启动器活动设置半透明主题,我称之为Translucy:

    /res/values/style.xml

    <style name="Theme.Translucy" parent="android:style/Theme.Translucent">
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@android:color/transparent</item>
    </style>
    

    AndroidManifest.xml:

    <activity
        android:name=".MainActivity"
        android:theme="@style/Theme.Translucy"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
  2. 为主xml布局创建一个id,我称之为rlayout_main, 并将其设置为不可见:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
    
        android:id="@+id/rlayout_main"
        android:visibility="invisible"
        android:background="@android:color/holo_blue_dark"
    
        tools:context=".MainActivity">
    
          <TextView
              android:textSize="50sp"
              android:text="@string/hello_world"
              android:layout_centerInParent="true"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" />
    
    </RelativeLayout>
    
  3. 创建AlphaAnimation并为主父布局设置动画:

     public class MainActivity extends Activity {
    
         @Override
         protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
    
         RelativeLayout relativeLayoutMain = (RelativeLayout) findViewById(R.id.rlayout_main);
         AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
         alphaAnimation.setDuration(3000);
         alphaAnimation.setFillAfter(true);
         relativeLayoutMain.startAnimation(alphaAnimation);
         }
     }
    

    (编辑): 要保存一些代码,您还可以使用ViewPropertyAnimator为布局设置动画,而不是创建AlphaAnimation:

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        RelativeLayout relativeLayoutMain = (RelativeLayout)  findViewById(R.id.rlayout_main);
        relativeLayoutMain.animate().alpha(1).setDuration(3000);
        }
    }
    
  4. 希望这会有所帮助,就像我说它不完美,因为它排除了ActionBar,但它可能是开始的。

答案 1 :(得分:0)

Haven尝试过但只是一招;

创建一个空活动并使其成为启动器。在onCreate方法中,从该活动开始一个意图,这是您要淡入的主要活动。现在您可以应用第一个空活动中的任何动画。并且第一个活动不会影响任何内容,因为只要它被创建,它就会启动第二个(主要)活动。