Android實現(xiàn)過渡動畫、引導頁 Android判斷是否第一次啟動App
目前的App在安裝后,第一次打開,都會顯示兩秒左右的logo,然后進入引導頁。如果關閉App,再重新打開,則只會顯示logo,然后直接進入主頁。
最近寫了這個,記錄一下。
首先是過渡動畫,因為它不論App是否第一次啟動都會顯示。
這里我使用了Handler的postDelayed()方法。把過渡動畫的Activity設為默認啟動的Activity。在當前Activity中,執(zhí)行postDelayed()方法,把延時的時長設為兩秒即可。
過渡頁面如下: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>
這里因為我的圖片背景是白色的,就沒有設置LinearLayout的背景色了,如果Logo的背景色不一樣,則可以進行設置。也可以直接用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會判斷進入引導頁還是主頁。
因為這個判斷并不是一次執(zhí)行就不需再執(zhí)行了,而是每次啟動App的時候都需要進行判斷。所以這個判斷的數(shù)據(jù)需要持久化。
且為了判斷的時間很短,就不需要進行訪問數(shù)據(jù)庫,或者網(wǎng)絡訪問等耗時操作了。直接使用 SharedPreferences 進行處理。
首先去指定 SharedPreferences 文件的名稱,如果不存在則會創(chuàng)建一個。創(chuàng)建的文件存放在 /data/data/<package name>/shared_prefs/ 目錄下。
第二個參數(shù)是指定對該文件的操作模式。默認是 MODE_PRIVATE ,和直接傳入0是一樣的,表示只有當前程序才能對這個文件進行讀寫操作。
MODE_MULTI_PROCESS 是用于多個程序?qū)ν粋€ SharedPreferences 文件進行讀寫操作。
創(chuàng)建好了文件,接下來我們讀取標志,看程序是否是第一次啟動App。
getBoolean("isFirstIn",true); 這個是用來獲取標志的,它是用來取出文件中對應的鍵值對。第一個參數(shù)是鍵,第二個參數(shù)是默認值。
它會取出對應鍵的值,如果沒有這個鍵,或者沒有值,則直接使用默認值,即第二個參數(shù)。因為我創(chuàng)建SharedPreferences 文件的時候并沒有創(chuàng)建這個鍵值對。
所以,它是讀不出對應的鍵的值的,則會直接獲取到 true 值。則App判斷為第一次啟動。接下來使用Intent,根據(jù)值,則開啟了引導頁即 GuideActivity 。
引導頁 頁面如下: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。引導頁我決定使用ViewPager+FragmentPagerAdapter來實現(xiàn)。
如果我直接通過判斷VIewPager是否是最后一頁,再左滑進入App主頁,ViewPager切換到主頁時候會有一點問題??赡茏蠡艘稽c,但是還想看前兩張引導頁,再右滑,
結果是直接進入了App主頁,而不是上一張。體驗感很不好,所以考慮到最后一頁上有一個按鈕,來進行點擊進入App主頁。這樣體驗感會好一點。
引導頁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 官方文檔上列出的兩種動畫效果。
當然可以進行自定義切換動畫,我本來自定義了一個切換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="進入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)聽器。點擊進入主頁,并且修改判斷是否第一次進入App的標志值。
通過 SharedPreferences.Editor 對象去修改標志值。然后 commit ,沒有 commit 是沒有進行更新保存的。
這里getSharedPreferences() 的第二個參數(shù),我直接使用了 0x0000,十六進制的0。
因為當時我使用 MODE_PRIVATE 的時候報錯,然后我就通過查源碼,發(fā)現(xiàn) MODE_PRIVATE 的值就是 0x0000,所以我直接使用了這個 0x0000。
為什么報錯呢?因為 MODE_PRIVATE 是Context 里的變量,在 Fragment 里無法識別。如果一定要用,則使用 Context.MODE_PRIVATE。
為什么 Activity 能用呢?因為 Activity 繼承了 Context, 而 Fragment 沒有繼承 Context。
本來我做的是在主頁的Activity中去修改這個標志值。但是后面考慮到,如果不是第一次啟動,每次進入到主頁,都需要修改一次標志值,即使它沒有變化,還是多做了很多無用功。所以在最后一頁的點擊事件里進行修改。標志值只需要修改一次,引導頁也只出現(xiàn)一次,正好。
主頁就是創(chuàng)建工程默認的主頁了。
其他事項:
給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"
/>
<!-- 設置按鈕的四個角為弧形 -->
<!-- android:radius 弧形的半徑 -->
<corners android:radius="5dip" />
<!-- padding:Button里面的文字與Button邊界的間隔 -->
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/>
</shape>
進入程序會出現(xiàn)一瞬間的空白,然后顯示正常。這是因為AppTheme。這里我新建了一個空的樣式。然后讓默認啟動的Activity去應用空的樣式。
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>
<!--應用空樣式-->
<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>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android編程實現(xiàn)將時間轉(zhuǎn)化成幾分鐘前、幾天前等形式的工具類
這篇文章主要介紹了Android編程實現(xiàn)將時間轉(zhuǎn)化成幾分鐘前、幾天前等形式的工具類,涉及Android針對日期時間的相關運算與判斷簡單操作技巧,需要的朋友可以參考下2018-02-02
Android 使用Vibrator服務實現(xiàn)點擊按鈕帶有震動效果
這篇文章主要介紹了Android 使用Vibrator服務實現(xiàn)點擊按鈕帶有震動效果,,本文通過實例圖文相結合給大家介紹的非常詳細,對大家的學習火鍋工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
Android?Camera開發(fā)實現(xiàn)可復用的相機組件
這篇文章主要為大家詳細介紹了Android?Camera開發(fā)實現(xiàn)可復用的相機組件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
Android Handler之消息循環(huán)的深入解析
本篇文章是對Handler消息循環(huán)進行了詳細的分析介紹,需要的朋友參考下2013-05-05
Android 實現(xiàn)自動打電話與發(fā)短信的實例
這篇文章主要介紹了Android 實現(xiàn)自動打電話與發(fā)短信的實例的相關資料,需要的朋友可以參考下2017-05-05

