溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android如何實現(xiàn)過渡動畫、引導(dǎo)頁 Android判斷是否第一次啟動App

發(fā)布時間:2021-04-16 13:46:48 來源:億速云 閱讀:311 作者:小新 欄目:移動開發(fā)

這篇文章主要介紹了Android如何實現(xiàn)過渡動畫、引導(dǎo)頁 Android判斷是否第一次啟動App,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

目前的App在安裝后,第一次打開,都會顯示兩秒左右的logo,然后進(jìn)入引導(dǎo)頁。如果關(guān)閉App,再重新打開,則只會顯示logo,然后直接進(jìn)入主頁。

最近寫了這個,記錄一下。

首先是過渡動畫,因為它不論App是否第一次啟動都會顯示。

這里我使用了Handler的postDelayed()方法。把過渡動畫的Activity設(shè)為默認(rèn)啟動的Activity。在當(dāng)前Activity中,執(zhí)行postDelayed()方法,把延時的時長設(shè)為兩秒即可。

過渡頁面如下:transition_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" 
  android:background="#fff" 
  > 
 
  <ImageView 
    android:src="@drawable/profile" 
    android:layout_marginTop="80dp" 
    android:layout_gravity="center" 
    android:layout_width="100dp" 
    android:layout_height="100dp" /> 
 
</LinearLayout>

這里因為我的圖片背景是白色的,就沒有設(shè)置LinearLayout的背景色了,如果Logo的背景色不一樣,則可以進(jìn)行設(shè)置。也可以直接用ImageView解決。

過渡Activity如下:TransitionActivity.java

package com.ikok.transitionandguidingpage; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.Window; 
 
/** 
 * Created by Anonymous on 2016/3/25. 
 */ 
public class TransitionActivity extends Activity { 
 
  boolean isFirstIn = false; 
  private Intent intent; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.transition_view); 
 
    final SharedPreferences sharedPreferences = getSharedPreferences("is_first_in_data",MODE_PRIVATE); 
    isFirstIn = sharedPreferences.getBoolean("isFirstIn",true); 
    new Handler().postDelayed(new Runnable() { 
 
      @Override 
      public void run() { 
        if (isFirstIn) { 
//          Toast.makeText(TransitionActivity.this, "First log", Toast.LENGTH_SHORT).show(); 
          intent = new Intent(TransitionActivity.this, GuideActivity.class); 
          TransitionActivity.this.startActivity(intent); 
          TransitionActivity.this.finish(); 
        } else { 
          intent = new Intent(TransitionActivity.this, MainActivity.class); 
          TransitionActivity.this.startActivity(intent); 
          TransitionActivity.this.finish(); 
        } 
      } 
    }, 2000); 
 
  } 
 
}

顯示了過渡動畫后,則需要判斷是否是第一次啟動App了。因為根據(jù)是否是第一次啟動App會判斷進(jìn)入引導(dǎo)頁還是主頁。
因為這個判斷并不是一次執(zhí)行就不需再執(zhí)行了,而是每次啟動App的時候都需要進(jìn)行判斷。所以這個判斷的數(shù)據(jù)需要持久化。

且為了判斷的時間很短,就不需要進(jìn)行訪問數(shù)據(jù)庫,或者網(wǎng)絡(luò)訪問等耗時操作了。直接使用 SharedPreferences  進(jìn)行處理。

首先去指定 SharedPreferences  文件的名稱,如果不存在則會創(chuàng)建一個。創(chuàng)建的文件存放在 /data/data/<package name>/shared_prefs/ 目錄下。

第二個參數(shù)是指定對該文件的操作模式。默認(rèn)是 MODE_PRIVATE ,和直接傳入0是一樣的,表示只有當(dāng)前程序才能對這個文件進(jìn)行讀寫操作。

MODE_MULTI_PROCESS 是用于多個程序?qū)ν粋€ SharedPreferences  文件進(jìn)行讀寫操作。

創(chuàng)建好了文件,接下來我們讀取標(biāo)志,看程序是否是第一次啟動App。

getBoolean("isFirstIn",true); 這個是用來獲取標(biāo)志的,它是用來取出文件中對應(yīng)的鍵值對。第一個參數(shù)是鍵,第二個參數(shù)是默認(rèn)值。

它會取出對應(yīng)鍵的值,如果沒有這個鍵,或者沒有值,則直接使用默認(rèn)值,即第二個參數(shù)。因為我創(chuàng)建SharedPreferences  文件的時候并沒有創(chuàng)建這個鍵值對。

所以,它是讀不出對應(yīng)的鍵的值的,則會直接獲取到 true 值。則App判斷為第一次啟動。接下來使用Intent,根據(jù)值,則開啟了引導(dǎo)頁即 GuideActivity 。

引導(dǎo)頁 頁面如下:guide_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
 
  <android.support.v4.view.ViewPager 
    android:id="@+id/viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
 
  </android.support.v4.view.ViewPager> 
 
</LinearLayout>

這里是v4包下的ViewPager。引導(dǎo)頁我決定使用ViewPager+FragmentPagerAdapter來實現(xiàn)。
如果我直接通過判斷VIewPager是否是最后一頁,再左滑進(jìn)入App主頁,ViewPager切換到主頁時候會有一點問題??赡茏蠡艘稽c,但是還想看前兩張引導(dǎo)頁,再右滑,
結(jié)果是直接進(jìn)入了App主頁,而不是上一張。體驗感很不好,所以考慮到最后一頁上有一個按鈕,來進(jìn)行點擊進(jìn)入App主頁。這樣體驗感會好一點。

引導(dǎo)頁Activity如下:GuideAcitivity.java

package com.ikok.transitionandguidingpage; 
 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.Window; 
 
import java.util.ArrayList; 
import java.util.List; 
 
/** 
 * Created by Anonymous on 2016/3/26. 
 */ 
public class GuideActivity extends FragmentActivity { 
 
  private ViewPager mViewPager; 
  private FragmentPagerAdapter mAdapter; 
  private List<Fragment> mFragment = new ArrayList<Fragment>(); 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.guide_view); 
 
    mViewPager = (ViewPager) findViewById(R.id.viewpager); 
 
    Fragment guide1 = new Guide1(); 
    Fragment guide2 = new Guide2(); 
    Fragment guide3 = new Guide3(); 
 
    mFragment.add(guide1); 
    mFragment.add(guide2); 
    mFragment.add(guide3); 
 
    mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) { 
      @Override 
      public Fragment getItem(int position) { 
        return mFragment.get(position); 
      } 
 
      @Override 
      public int getCount() { 
        return mFragment.size(); 
      } 
    }; 
 
    // 為ViewPager添加動畫效果,3.0以上可用 
    mViewPager.setPageTransformer(true,new DepthPageTransformer()); 
//    mViewPager.setPageTransformer(true,new ZoomOutPageTransformer()); 
    mViewPager.setAdapter(mAdapter); 
 
  } 
}

中間創(chuàng)建了三個Fragment,去加載布局,布局就是在xml的根節(jié)點上添加了 background 屬性。

這里我為ViewPager的切換添加了切換動畫。使用的 Google 官方文檔上列出的兩種動畫效果。

當(dāng)然可以進(jìn)行自定義切換動畫,我本來自定義了一個切換20度角的切換動畫,但覺得不是很好看就沒放上來了。

切換動畫,低版本不支持。又添加了 nineoldandroid ,來使動畫兼容到低版本。

最后一個頁面如下:guide_view3.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:background="@drawable/guide3"  
  android:layout_height="match_parent"> 
 
  <Button 
    android:id="@+id/into_app_btn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="進(jìn)入App" 
    android:textColor="#fefefe" 
    android:background="@drawable/button_shape" 
    android:layout_alignParentBottom="true" 
    android:layout_centerInParent="true" 
    android:layout_marginBottom="50dp" 
    /> 
 
</RelativeLayout>

第三頁的代碼如下: Guide3.java

package com.ikok.transitionandguidingpage; 
 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
 
/** 
 * Created by Anonymous on 2016/3/27. 
 */ 
public class Guide3 extends Fragment { 
 
  private Button mIntoAppBtn; 
  private View view; 
 
  @Override 
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    view = inflater.inflate(R.layout.guide_view3,container,false); 
    return view; 
  } 
 
  @Override 
  public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    mIntoAppBtn = (Button) view.findViewById(R.id.into_app_btn); 
    mIntoAppBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        Intent intent = new Intent(getActivity(), MainActivity.class); 
        startActivity(intent); 
 
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("is_first_in_data", 0x0000); 
        SharedPreferences.Editor editor = sharedPreferences.edit(); 
        editor.putBoolean("isFirstIn", false); 
        editor.commit(); 
 
        getActivity().finish(); 
      } 
    }); 
  } 
}

這里我就對頁面上的Button綁定了一個點擊事件監(jiān)聽器。點擊進(jìn)入主頁,并且修改判斷是否第一次進(jìn)入App的標(biāo)志值。
通過 SharedPreferences.Editor 對象去修改標(biāo)志值。然后 commit ,沒有 commit 是沒有進(jìn)行更新保存的。

這里getSharedPreferences() 的第二個參數(shù),我直接使用了 0x0000,十六進(jìn)制的0。

因為當(dāng)時我使用 MODE_PRIVATE 的時候報錯,然后我就通過查源碼,發(fā)現(xiàn) MODE_PRIVATE 的值就是 0x0000,所以我直接使用了這個 0x0000。

為什么報錯呢?因為 MODE_PRIVATE 是Context 里的變量,在 Fragment 里無法識別。如果一定要用,則使用 Context.MODE_PRIVATE。

為什么 Activity 能用呢?因為 Activity 繼承了 Context, 而 Fragment 沒有繼承 Context。

本來我做的是在主頁的Activity中去修改這個標(biāo)志值。但是后面考慮到,如果不是第一次啟動,每次進(jìn)入到主頁,都需要修改一次標(biāo)志值,即使它沒有變化,還是多做了很多無用功。所以在最后一頁的點擊事件里進(jìn)行修改。標(biāo)志值只需要修改一次,引導(dǎo)頁也只出現(xiàn)一次,正好。

主頁就是創(chuàng)建工程默認(rèn)的主頁了。

其他事項:

給Button加了樣式屬性。
button_shape.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape="rectangle"> 
  <!-- 填充的顏色 --> 
  <solid android:color="#00FFFFFF" /> 
  <stroke android:color="#fefefe" 
      android:width="1dp" 
    /> 
  <!-- 設(shè)置按鈕的四個角為弧形 --> 
  <!-- android:radius 弧形的半徑 --> 
  <corners android:radius="5dip" /> 
 
  <!-- padding:Button里面的文字與Button邊界的間隔 --> 
  <padding 
    android:left="10dp" 
    android:top="10dp" 
    android:right="10dp" 
    android:bottom="10dp" 
    /> 
</shape>

進(jìn)入程序會出現(xiàn)一瞬間的空白,然后顯示正常。這是因為AppTheme。這里我新建了一個空的樣式。然后讓默認(rèn)啟動的Activity去應(yīng)用空的樣式。
style.xml

<pre name="code" class="html"><resources> 
 
  <!-- Base application theme. --> 
  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
  </style> 
 
  <!--空程序樣式--> 
  <style name="EmptyTheme"> 
 
  </style> 
 
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  package="com.ikok.transitionandguidingpage"> 
 
  <application 
    android:allowBackup="true" 
    android:icon="@drawable/profile" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
 
    </activity> 
    <!--應(yīng)用空樣式--> 
    <activity android:name=".TransitionActivity" 
          android:theme="@style/EmptyTheme"> 
      <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
 
        <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
    </activity> 
    <activity android:name=".GuideActivity"> 
 
    </activity> 
  </application> 
 
</manifest>

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Android如何實現(xiàn)過渡動畫、引導(dǎo)頁 Android判斷是否第一次啟動App”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI