android 引导界面的实现方法
/**
* 实现
* @author dujinyang
*
*/
顺序是: oneacitivity -->mainactivity -> twoactivity
然后第2次进去就是:oneactivity -> twoactivity
代码里都有注释的了,这里就不多说了。
oneactivity的代码如下:
[java]
package cn.djy.activity;
import android.app.activity;
import android.content.context;
import android.content.intent;
import android.os.bundle;
import android.os.handler;
/**
* 开机动画
* @author administrator
*
*/
public class oneactivity extends activity {
@override
protected void oncreate(bundle savedinstancestate) {
// todo auto-generated method stub
super.oncreate(savedinstancestate);
setcontentview(r.layout.one);
boolean flag=this.isfirstenter(getapplicationcontext(), this.getclass().getname());
if(flag)
mhandler.sendemptymessagedelayed(switch_guidactivity,4000);
else
mhandler.sendemptymessagedelayed(switch_twoactivity, 4000);
}
//***********************************************************************
//判断应用是否初次加载,读取sharedpreferences 的字段
//***********************************************************************
private static final string sharedpreferences_name="yang";
private static final string key_guide_activity="open";
/**
* mresultstr.equalsignorecase("false") 返回false
* true 则为空值
* @param context
* @param classname
* @return boolean
*/
private boolean isfirstenter(context context,string classname){
if(context==null || classname ==null || "".equalsignorecase(classname)) return false;
string mresultstr=context.getsharedpreferences(sharedpreferences_name,context.mode_world_readable).getstring(key_guide_activity, "");
if(mresultstr.equalsignorecase("false"))
return false;
else
return true;
}
//****************************************
//handler:跳转至不同页面
//****************************************
private final static int switch_twoactivity=1000; //主页
private final static int switch_guidactivity=1001; //滑动手势
private handler mhandler=new handler(){
public void handlemessage(android.os.message msg) {
switch (msg.what) {
case switch_twoactivity:
intent intent=new intent();
intent.setclass(oneactivity.this,twoactivity.class);
oneactivity.this.startactivity(intent);
// oneactivity.this.finish();
break;
case switch_guidactivity:
intent intents=new intent();
intents.setclass(oneactivity.this,mainactivity.class);
oneactivity.this.startactivity(intents);
// oneactivity.this.finish();
break;
}
super.handlemessage(msg);
};
};
}
package cn.djy.activity;
import android.app.activity;
import android.content.context;
import android.content.intent;
import android.os.bundle;
import android.os.handler;
/**
* 开机动画
* @author administrator
*
*/
public class oneactivity extends activity {
@override
protected void oncreate(bundle savedinstancestate) {
// todo auto-generated method stub
super.oncreate(savedinstancestate);
setcontentview(r.layout.one);
boolean flag=this.isfirstenter(getapplicationcontext(), this.getclass().getname());
if(flag)
mhandler.sendemptymessagedelayed(switch_guidactivity,4000);
else
mhandler.sendemptymessagedelayed(switch_twoactivity, 4000);
}
//***********************************************************************
//判断应用是否初次加载,读取sharedpreferences 的字段
//***********************************************************************
private static final string sharedpreferences_name="yang";
private static final string key_guide_activity="open";
/**
* mresultstr.equalsignorecase("false") 返回false
* true 则为空值
* @param context
* @param classname
* @return boolean
*/
private boolean isfirstenter(context context,string classname){
if(context==null || classname ==null || "".equalsignorecase(classname)) return false;
string mresultstr=context.getsharedpreferences(sharedpreferences_name,context.mode_world_readable).getstring(key_guide_activity, "");
if(mresultstr.equalsignorecase("false"))
return false;
else
return true;
}
//****************************************
//handler:跳转至不同页面
//****************************************
private final static int switch_twoactivity=1000; //主页
private final static int switch_guidactivity=1001; //滑动手势
private handler mhandler=new handler(){
public void handlemessage(android.os.message msg) {
switch (msg.what) {
case switch_twoactivity:
intent intent=new intent();
intent.setclass(oneactivity.this,twoactivity.class);
oneactivity.this.startactivity(intent);
// oneactivity.this.finish();
break;
case switch_guidactivity:
intent intents=new intent();
intents.setclass(oneactivity.this,mainactivity.class);
oneactivity.this.startactivity(intents);
// oneactivity.this.finish();
break;
}
super.handlemessage(msg);
};
};
}
mainactivity的代码如下:
[java]
package cn.djy.activity;
import java.util.arraylist;
import android.app.activity;
import android.content.intent;
import android.content.sharedpreferences;
import android.os.bundle;
import android.os.parcelable;
import android.support.v4.view.pageradapter;
import android.support.v4.view.viewpager;
import android.support.v4.view.viewpager.onpagechangelistener;
import android.view.layoutinflater;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.viewgroup;
import android.view.viewgroup.layoutparams;
import android.widget.button;
import android.widget.imageview;
import android.widget.twolinelistitem;
/**
* 实现
* @author dujinyang
*
*/
public class mainactivity extends activity {
private viewpager _viewpager;
/**
* 分页显示的view数组
*/
private arraylist<view> _pagerlist;
private imageview image;
/**
* 小圆点的图片 放进数组
*/
private imageview[] _imageviews;
//包裹滑动的图片linearlayout
private viewgroup _group;
//包裹小圆点的linearlayout
private viewgroup _viewponits;
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.start);
layoutinflater inflater=getlayoutinflater();
_pagerlist =new arraylist<view>();
_pagerlist.add(inflater.inflate(r.layout.viewpager_page1,null));
_pagerlist.add(inflater.inflate(r.layout.viewpager_page2,null));
//创建imageviews数组,大小是要显示的图片数量
_imageviews =new imageview[_pagerlist.size()];
// viewgroup
_group =(viewgroup) inflater.inflate(r.layout.start, null);//加载main.xml
_viewponits=(viewgroup) _group.findviewbyid(r.id.icon_circle);//加载小圆点的图片
_viewpager =(viewpager) _group.findviewbyid(r.id.guidepages);//android.support.v4.view.viewpager
//添加小圆点的图片
for (int i = 0; i < _imageviews.length; i++) {
image= new imageview(this);
//设置小圆点的imageview的参数
image.setlayoutparams(new layoutparams(20,20));//宽高均为20
image.setpadding(20, 0, 20, 0);
_imageviews[i]=image;
//第一张是选中状态
if(i==0){
_imageviews[i].setbackgroundresource(r.drawable.a2a);
}else{
_imageviews[i].setbackgroundresource(r.drawable.a1a);
}
_viewponits.addview(_imageviews[i]);
}
//显示视图
setcontentview(_group);
_viewpager.setadapter(new guidepageadapter());
_viewpager.setonpagechangelistener(new guidepagechangelistener());
}
void setguided(){
sharedpreferences shared=getsharedpreferences("yang",0);
sharedpreferences.editor editor=shared.edit();
editor.putstring("open", "false");
editor.commit();
}
class guidepageadapter extends pageradapter{
@override
public int getcount() {
return _pagerlist.size();
}
//initalization position page
@override
public object instantiateitem(view container, int position) {
((viewpager)container).addview(_pagerlist.get(position));
if(position==1){
button bt=(button) container.findviewbyid(r.id.bt_close);
bt.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
//设置已经引导
setguided();
//intent
intent intent=new intent();
intent.setclass(mainactivity.this,twoactivity.class );
mainactivity.this.startactivity(intent);
mainactivity.this.finish();
}
});
}
return _pagerlist.get(position);
}
//判断是否由对象生成页面
@override
public boolean isviewfromobject(view view, object obj) {
return view==obj;
}
//销毁页面的位置
@override
public void destroyitem(view container, int position, object object) {
((viewpager)container).removeview(_pagerlist.get(position));
}
@override
public void finishupdate(view container) {
//finish
}
@override
public void startupdate(view container) {
//start
}
@override
public int getitemposition(object object) {
//getitemposition
return super.getitemposition(object);
}
@override
public void restorestate(parcelable state, classloader loader) {
super.restorestate(state, loader);
//restore
}
@override
public parcelable savestate() {
return super.savestate();
//save
}
}
class guidepagechangelistener implements onpagechangelistener{
@override
public void onpagescrollstatechanged(int arg0) {
}
@override
public void onpagescrolled(int arg0, float arg1, int arg2) {
}
@override
public void onpageselected(int position) {
for (int i = 0; i < _imageviews.length; i++) {
_imageviews[position].setbackgroundresource(r.drawable.a2a);
if(position!=i)
_imageviews[i].setbackgroundresource(r.drawable.a1a);
}
}
}
}
package cn.djy.activity;
import java.util.arraylist;
import android.app.activity;
import android.content.intent;
import android.content.sharedpreferences;
import android.os.bundle;
import android.os.parcelable;
import android.support.v4.view.pageradapter;
import android.support.v4.view.viewpager;
import android.support.v4.view.viewpager.onpagechangelistener;
import android.view.layoutinflater;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.viewgroup;
import android.view.viewgroup.layoutparams;
import android.widget.button;
import android.widget.imageview;
import android.widget.twolinelistitem;
/**
* 实现
* @author dujinyang
*
*/
public class mainactivity extends activity {
private viewpager _viewpager;
/**
* 分页显示的view数组
*/
private arraylist<view> _pagerlist;
private imageview image;
/**
* 小圆点的图片 放进数组
*/
private imageview[] _imageviews;
//包裹滑动的图片linearlayout
private viewgroup _group;
//包裹小圆点的linearlayout
private viewgroup _viewponits;
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.start);
layoutinflater inflater=getlayoutinflater();
_pagerlist =new arraylist<view>();
_pagerlist.add(inflater.inflate(r.layout.viewpager_page1,null));
_pagerlist.add(inflater.inflate(r.layout.viewpager_page2,null));
//创建imageviews数组,大小是要显示的图片数量
_imageviews =new imageview[_pagerlist.size()];
// viewgroup
_group =(viewgroup) inflater.inflate(r.layout.start, null);//加载main.xml
_viewponits=(viewgroup) _group.findviewbyid(r.id.icon_circle);//加载小圆点的图片
_viewpager =(viewpager) _group.findviewbyid(r.id.guidepages);//android.support.v4.view.viewpager
//添加小圆点的图片
for (int i = 0; i < _imageviews.length; i++) {
image= new imageview(this);
//设置小圆点的imageview的参数
image.setlayoutparams(new layoutparams(20,20));//宽高均为20
image.setpadding(20, 0, 20, 0);
_imageviews[i]=image;
//第一张是选中状态
if(i==0){
_imageviews[i].setbackgroundresource(r.drawable.a2a);
}else{
_imageviews[i].setbackgroundresource(r.drawable.a1a);
}
_viewponits.addview(_imageviews[i]);
}
//显示视图
setcontentview(_group);
_viewpager.setadapter(new guidepageadapter());
_viewpager.setonpagechangelistener(new guidepagechangelistener());
}
void setguided(){
sharedpreferences shared=getsharedpreferences("yang",0);
sharedpreferences.editor editor=shared.edit();
editor.putstring("open", "false");
editor.commit();
}
class guidepageadapter extends pageradapter{
@override
public int getcount() {
return _pagerlist.size();
}
//initalization position page
@override
public object instantiateitem(view container, int position) {
((viewpager)container).addview(_pagerlist.get(position));
if(position==1){
button bt=(button) container.findviewbyid(r.id.bt_close);
bt.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
//设置已经引导
setguided();
//intent
intent intent=new intent();
intent.setclass(mainactivity.this,twoactivity.class );
mainactivity.this.startactivity(intent);
mainactivity.this.finish();
}
});
}
return _pagerlist.get(position);
}
//判断是否由对象生成页面
@override
public boolean isviewfromobject(view view, object obj) {
return view==obj;
}
//销毁页面的位置
@override
public void destroyitem(view container, int position, object object) {
((viewpager)container).removeview(_pagerlist.get(position));
}
@override
public void finishupdate(view container) {
//finish
}
@override
public void startupdate(view container) {
//start
}
@override
public int getitemposition(object object) {
//getitemposition
return super.getitemposition(object);
}
@override
public void restorestate(parcelable state, classloader loader) {
super.restorestate(state, loader);
//restore
}
@override
public parcelable savestate() {
return super.savestate();
//save
}
}
class guidepagechangelistener implements onpagechangelistener{
@override
public void onpagescrollstatechanged(int arg0) {
}
@override
public void onpagescrolled(int arg0, float arg1, int arg2) {
}
@override
public void onpageselected(int position) {
for (int i = 0; i < _imageviews.length; i++) {
_imageviews[position].setbackgroundresource(r.drawable.a2a);
if(position!=i)
_imageviews[i].setbackgroundresource(r.drawable.a1a);
}
}
}
}
最后是twoactivity:这个activity是显示的内容。
[java]
package cn.djy.activity;
import android.app.activity;
import android.os.bundle;
public class twoactivity extends activity{
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.end);
}
}
package cn.djy.activity;
import android.app.activity;
import android.os.bundle;
public class twoactivity extends activity{
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.end);
}
}
主要代码完成。
里面涉及的drawable图片是小圆点图片。
下面贴出xml的。
start.xml
[css]
<?xml version="1.0" encoding="utf-8"?>
<framelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/linear_viewpager">
<android.support.v4.view.viewpager android:id="@+id/guidepages" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</linearlayout>
<linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/linear_circle">
<relativelayout android:layout_width="fill_parent" android:layout_height="wrap_content" >
<linearlayout android:id="@+id/icon_circle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginbottom="40dip"
android:layout_alignparentbottom="true"
android:gravity="center_vertical"
android:orientation="horizontal"/>
</relativelayout>
</linearlayout>
</framelayout>
<?xml version="1.0" encoding="utf-8"?>
<framelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/linear_viewpager">
<android.support.v4.view.viewpager android:id="@+id/guidepages" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</linearlayout>
<linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/linear_circle">
<relativelayout android:layout_width="fill_parent" android:layout_height="wrap_content" >
<linearlayout android:id="@+id/icon_circle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginbottom="40dip"
android:layout_alignparentbottom="true"
android:gravity="center_vertical"
android:orientation="horizontal"/>
</relativelayout>
</linearlayout>
</framelayout>
其实是用group把子项包起来作为显示:
那子项为:
viewpager_page1.xml
[css]
<linearlayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<textview android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="adfasfasfweqwwfqw'"
/>
</linearlayout>
<linearlayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<textview android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="adfasfasfweqwwfqw'"
/>
</linearlayout>
viewpager_page2.xml
[css]
<?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:gravity="center">
<textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="pages222222222222222222"></textview>
<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="asdfi14234124" android:id="@+id/bt_close"></button>
</linearlayout>
<?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:gravity="center">
<textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="pages222222222222222222"></textview>
<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="asdfi14234124" android:id="@+id/bt_close"></button>
</linearlayout>