Fade In Android Launch Activity

2019-02-19 01:10发布

Looking for help fading in my launch activity. I've found LOTS of posts about fading between activities that are being launched with intents, but nothing about how to fade in your launch activity. I've tried overridePendingTransition(int animationIn, int animationOut) but that hasn't worked for me. I've placed it in the onCreate(..) method and the onStart(..) method but have been unsuccessful. Any help or direction would be appreciated.

Thanks.

2条回答
我只想做你的唯一
2楼-- · 2019-02-19 01:56

Haven't tried but just a trick;

Create an empty activity ana make it launcher. In the onCreate method, start an intent from that activity, which is the main activity you want to fade in. Now the you can apply any animation from the first empty activity. And the first activity will not affect anything since it starts the second (main) activity as long as it is created.

查看更多
乱世女痞
3楼-- · 2019-02-19 02:04

I've made something up for you, it excludes the action bar though. I've modified the hello world demo a little for a simple example :

  1. Set a translucent theme to your launcher activity, I called it 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. Create an id for your main xml layout, I called it rlayout_main, and set it to invisible :

    <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. Create an AlphaAnimation and animate your main parent layout :

     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);
         }
     }
    

    (EDIT) : To save a little code you can also use ViewPropertyAnimator to animate your layout instead of creating an 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);
        }
    }
    

Hope this helps, like I said its not perfect because it excludes the ActionBar, but it might be something to start with.

查看更多
登录 后发表回答