Android实现过渡动画、引导页 Android判断是否第一次启动App

Damara ·
更新时间:2024-09-20
· 701 次阅读

目前的App在安装后,第一次打开,都会显示两秒左右的logo,然后进入引导页。如果关闭App,再重新打开,则只会显示logo,然后直接进入主页。

最近写了这个,记录一下。

首先是过渡动画,因为它不论App是否第一次启动都会显示。

这里我使用了Handler的postDelayed()方法。把过渡动画的Activity设为默认启动的Activity。在当前Activity中,执行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了。因为根据是否是第一次启动App会判断进入引导页还是主页。
因为这个判断并不是一次执行就不需再执行了,而是每次启动App的时候都需要进行判断。所以这个判断的数据需要持久化。

且为了判断的时间很短,就不需要进行访问数据库,或者网络访问等耗时操作了。直接使用 SharedPreferences  进行处理。

首先去指定 SharedPreferences  文件的名称,如果不存在则会创建一个。创建的文件存放在 /data/data/<package name>/shared_prefs/ 目录下。

第二个参数是指定对该文件的操作模式。默认是 MODE_PRIVATE ,和直接传入0是一样的,表示只有当前程序才能对这个文件进行读写操作。

MODE_MULTI_PROCESS 是用于多个程序对同一个 SharedPreferences  文件进行读写操作。

创建好了文件,接下来我们读取标志,看程序是否是第一次启动App。

getBoolean("isFirstIn",true); 这个是用来获取标志的,它是用来取出文件中对应的键值对。第一个参数是键,第二个参数是默认值。

它会取出对应键的值,如果没有这个键,或者没有值,则直接使用默认值,即第二个参数。因为我创建SharedPreferences  文件的时候并没有创建这个键值对。

所以,它是读不出对应的键的值的,则会直接获取到 true 值。则App判断为第一次启动。接下来使用Intent,根据值,则开启了引导页即 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来实现。
如果我直接通过判断VIewPager是否是最后一页,再左滑进入App主页,ViewPager切换到主页时候会有一点问题。可能左滑了一点,但是还想看前两张引导页,再右滑,
结果是直接进入了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); } }

中间创建了三个Fragment,去加载布局,布局就是在xml的根节点上添加了 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绑定了一个点击事件监听器。点击进入主页,并且修改判断是否第一次进入App的标志值。
通过 SharedPreferences.Editor 对象去修改标志值。然后 commit ,没有 commit 是没有进行更新保存的。

这里getSharedPreferences() 的第二个参数,我直接使用了 0x0000,十六进制的0。

因为当时我使用 MODE_PRIVATE 的时候报错,然后我就通过查源码,发现 MODE_PRIVATE 的值就是 0x0000,所以我直接使用了这个 0x0000。

为什么报错呢?因为 MODE_PRIVATE 是Context 里的变量,在 Fragment 里无法识别。如果一定要用,则使用 Context.MODE_PRIVATE。

为什么 Activity 能用呢?因为 Activity 继承了 Context, 而 Fragment 没有继承 Context。

本来我做的是在主页的Activity中去修改这个标志值。但是后面考虑到,如果不是第一次启动,每次进入到主页,都需要修改一次标志值,即使它没有变化,还是多做了很多无用功。所以在最后一页的点击事件里进行修改。标志值只需要修改一次,引导页也只出现一次,正好。

主页就是创建工程默认的主页了。

其他事项:

给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>

进入程序会出现一瞬间的空白,然后显示正常。这是因为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> 您可能感兴趣的文章:Android项目实战教程之高仿网易云音乐启动页实例代码Android启动页出现白屏、黑屏的解决方案Android启动页解决方案(推荐)Android启动页广告(腾讯广告联盟)解决方法及源码下载Android启动页面定时跳转的三种方法Android优化之启动页去黑屏实现秒启动Android开发之开发者头条(一)启动页实现Android启动引导页使用ViewPager实现Android开发实战之漂亮的ViewPager引导页Android控件ViewPager实现带有动画的引导页Android开发实现的ViewPager引导页功能(动态加载指示器)详解Android实现渐变启动页和带有指示器的引导页



引导页 app 动画 Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号