一、大部分APP应用首次打开的时候都会有一个定时进入引导页面的功能,滑动页面进入APP,这个效果的实现主要为三步骤。
1、先定义两个布局文件:activity_entry.xml、activity_main01.xml(此处为滑动的页面的布局,命名不规范)
2、对应布局文件的Activity为:EntryActiviy.class实现启动页面定时跳转效果,MainActivity01.class实现滑动引导页面,要重写ViewPagerAdapter.class类。
3、Activity主件必须mainfests.xml中配置,很多新手老忘了,要再mainfests.xml清单中配置的组件包括四大组件Activities, ContentProviders, Services, 和Intent Receivers,还能指定permissions和instrumentation。
接下来我们来看具体的实现代码,还有很多实现方法,作为菜鸟能实现功能,我已经感到很满足了,所以学友要是有其他号方法也欢迎互相分享!
二、布局文件相对比较简单,不再赘述,直接看代码吧
1.activity_entry.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">
<ImageView
android:id="@+id/entry"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/img_entry"/>
</LinearLayout>
2.activity_main01.xml,这里用ViewPager可以自带滑动功能,注意布局要用FramLayout,用后面加载的页面盖住之前加载的页面,在对应的Activity中记得用Inflate()动态将布局文件加载进去,否则没办法实现。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_launcher"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/dot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
>
</LinearLayout>
<Button
android:id="@+id/startButton"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="立即体验"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:layout_above="@id/dot"
android:background="@drawable/button"
android:layout_gravity="center_horizontal"/>
</RelativeLayout>
</FrameLayout>
三、实现布局文件相应的Activity
1.EntryActivity 用handler机制设置让消息延迟5s发送
public class EntryActivity extends AppCompatActivity {
private ImageView entry;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//获取当前窗口,并调用windowsManager来控制窗口
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉头部的标题
setContentView(R.layout.activity_entry);
//用handler的消息传递机制,让消息延迟5s发送,注意要调用SystemClock.uptimeMillis()系统时钟
handler.sendEmptyMessageAtTime(0, SystemClock.uptimeMillis()+5000);
entry = (ImageView) findViewById(R.id.entry);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
getHome();
super.handleMessage(msg);
}
};
//设置跳转页面
private void getHome() {
Intent intent2 = new Intent();
intent2.setClass(EntryActivity.this, MainAcitvity01.class);
startActivity(intent2);
finish();
}}
3、滑动页面对应的ainainctivity.class
public class MainAcitvity01 extends AppCompatActivity implements
ViewPager.OnPageChangeListener{
private ViewPager vP;
private int []imageArray;
private ViewGroup viewGroup;
private List<View> viewsList;
private ImageView iv_point;
private ImageView [] iv_PointArray;
private Button button;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main01);
button = (Button)findViewById(R.id.startButton);
button.setOnClickListener(new View.OnClickListener(){
@Override//设置监听,当滑动结束后点击按钮跳转到App主页面
public void onClick(View v) {
startActivity(new Intent(MainAcitvity01.this,MainActivity.class));
finish();
}
});
//初始化要加载的页面,即滑动页面
initViewPager();
//初始化活动要加载的点,即滑动时的小圆点
initPoint();
}
private void initViewPager(){
//加载第一张启动页面
vP = (ViewPager)findViewById(R.id.viewpager_launcher);
//滑动页面放到一个imageArray数组中
imageArray = new int[]{R.drawable.welcom_01,R.drawable.welcom_02,R.drawable.welcom_03};
viewsList = new ArrayList<>();
LinearLayout.LayoutParams params= new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
//获取imageArray数组的长度,现实当前页面,当数组中还有页面时,继续添加到viewList中显示
int len = imageArray.length;
for (int i = 0;i<len;i++){
ImageView IV = new ImageView(this);
IV.setLayoutParams(params);
IV.setBackgroundResource(imageArray[i]);
viewsList.add(IV);
}
//实例化ViewPagerAapter
vP.setAdapter(new ViewPagerAdapter(viewsList));
//设置滑动监听
vP.setOnPageChangeListener(this);
}
private void initPoint() {
viewGroup = (ViewGroup)findViewById(R.id.dot);
iv_PointArray = new ImageView[viewsList.size()];
int size = viewsList.size();
for(int i = 0; i <size;i++){
iv_point = new ImageView(this);
//实例化圆点,设置圆点的参数大小,位置
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(60,60);
lp.leftMargin = 20;
lp.rightMargin = 20;
iv_point.setLayoutParams(lp);
iv_PointArray[i] = iv_point;
if(i == 0){
iv_point.setBackgroundResource(R.drawable.dot);
}else{
iv_point.setBackgroundResource(R.drawable.black_dot);
}
viewGroup.addView(iv_PointArray[i]);
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override//滑动页面具体实现方法
public void onPageSelected(int position) {
int lenth = imageArray.length;
for (int i = 0; i < lenth; i++) {
iv_PointArray[i].setBackgroundResource(R.drawable.dot);
if (position != i) {
iv_PointArray[i].setBackgroundResource(R.drawable.black_dot);
}
if (position == lenth - 1) {
button.setVisibility(View.VISIBLE);
} else {
button.setVisibility(View.GONE);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}}
4、重写ViewPagerAdapter
public class ViewPagerAdapter extends PagerAdapter {
public List<View> views;
public ViewPagerAdapter(List<View> views){
this.views = views;
}
@Override
public int getCount() {
return views.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = views.get(position);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(views.get(position));
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}
5、在mainfests清单中配置Activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.changedemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".EntryActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainAcitvity01"/>
<activity android:name=".MainActivity"/>
</application>
</manifest>