当我们第一次打开某些APP时,可能会先出现一段可滑动的类似于HTML中的gallery的界面,用来介绍应用的某些特性。这就是所谓的userguide用户引导界面。这种界面一般使用viewpager实现,viewpager是Google提供的开源的框架,在www.Android.com上可以获取到Google已经实现的简易的viewpager。
下面我们使用viewpager,实现一个跟随移动的userguide,首先看一下效果图:
我们可以看到,屏幕中也有一个小的手机,外框部分的颜色可以跟随内框移动,我们需要实现的也就是这种效果。因此我们需要定义两个adapter来实现这个效果。
首先我们来定义布局文件。
activity_view_slash.xml
FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#38bccb"
android:orientation="vertical"
>
<com.ph.mybdmap.linkedViewPager.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<com.ph.mybdmap.linkedViewPager.NotouchLayout
android:layout_width="220dip"
android:layout_height="390dip"
android:layout_gravity="center|top"
android:background="@drawable/phone_frame"
android:layout_marginTop="50dip"
>
<com.ph.mybdmap.linkedViewPager.ViewPager
android:id="@+id/main_scrolllayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="false"
android:layout_centerInParent="true"
/>
</com.ph.mybdmap.linkedViewPager.NotouchLayout>
</FrameLayout>
然后实现myviewadapter.java
package com.ph.mybdmap.linkedViewPager;
import java.util.ArrayList;
import android.os.Parcelable;
import android.view.View;
public class MyPagerAdapter extends PagerAdapter {
private ArrayList<View> mPageViews;
public MyPagerAdapter(ArrayList<View> mPageViews) {
super();
this.mPageViews = mPageViews;
}
@Override
public int getCount() {
return mPageViews.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return super.getItemPosition(object);
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
// TODO Auto-generated method stub
((ViewPager) arg0).removeView(mPageViews.get(arg1));
}
@Override
public Object instantiateItem(View arg0, int arg1) {
// TODO Auto-generated method stub
((ViewPager) arg0).addView(mPageViews.get(arg1));
return mPageViews.get(arg1);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
}
最后实现userguide.java
package com.ph.mybdmap;
import java.util.ArrayList;
import com.ph.mybdmap.R;
import com.ph.mybdmap.linkedViewPager.MyPagerAdapter;
import com.ph.mybdmap.linkedViewPager.ViewPager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class UserGuideActivity extends Activity {
private ViewPager mPager;
private ArrayList<View> mPageViews;
private MyPagerAdapter mPageAdapter;
private ViewPager mFramePager;
private ArrayList<View> mFramePageViews;
private MyPagerAdapter mFramePageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_viewpager_slash);
initViewPager();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void initViewPager(){
mPager = (ViewPager)findViewById(R.id.pager);
mFramePager = (ViewPager)findViewById(R.id.main_scrolllayout);
mPageViews = new ArrayList<View>();
mFramePageViews = new ArrayList<View>();
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View frameView1 = inflater.inflate(R.layout.transparent_layer_image, null);
initFramePagerView(frameView1 ,R.drawable.frame_view1);
View frameView2 = inflater.inflate(R.layout.transparent_layer_image, null);
initFramePagerView(frameView2 ,R.drawable.frame_view2);
View frameView3 = inflater.inflate(R.layout.transparent_layer_image, null);
initFramePagerView(frameView3 ,R.drawable.frame_view3);
View frameView4 = inflater.inflate(R.layout.transparent_layer_image, null);
initFramePagerView(frameView4 ,R.drawable.frame_view4);
View frameView5 = inflater.inflate(R.layout.transparent_layer_image, null);
initFramePagerView(frameView5 ,R.drawable.frame_view5);
mFramePageViews.add(frameView1);
mFramePageViews.add(frameView2);
mFramePageViews.add(frameView3);
mFramePageViews.add(frameView4);
mFramePageViews.add(frameView5);
mFramePageAdapter = new MyPagerAdapter(mFramePageViews);
mFramePager.setAdapter(mFramePageAdapter);
// mFramePager.setOnPageChangeListener(mFramePagerListener);
View view1 = inflater.inflate(R.layout.transparent_layer, null);
initPagerView(view1,"茫茫人海中",0xff369a8e);
View view2 = inflater.inflate(R.layout.transparent_layer, null);
initPagerView(view2,"哪里能找到你",0xffc48951);
View view3 = inflater.inflate(R.layout.transparent_layer, null);
initPagerView(view3,"一键觅你",0xff0179cc);
View view4 = inflater.inflate(R.layout.transparent_layer, null);
initPagerView(view4,"我们在一起",0xff333333);
View view5 = inflater.inflate(R.layout.transparent_layer, null);
TextView textView5 = (TextView)view5.findViewById(R.id.text);
textView5.setVisibility(View.GONE);
Button btn = (Button)view5.findViewById(R.id.button);
btn.setVisibility(View.VISIBLE);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view){
Begin();
}
});
mPageViews.add(view1);
mPageViews.add(view2);
mPageViews.add(view3);
mPageViews.add(view4);
mPageViews.add(view5);
mPageAdapter =new MyPagerAdapter(mPageViews);
mPager.setAdapter(mPageAdapter);
mPager.setFollowViewPager(mFramePager);
// mPager.setOnPageChangeListener();
}
public void initFramePagerView(View frameView ,int drawable){
ImageView image = (ImageView)frameView.findViewById(R.id.image);
image.setImageResource(drawable);
}
public void initPagerView(View view ,String text,int color)
{
TextView textView1 = (TextView)view.findViewById(R.id.text);
LinearLayout linearlay=(LinearLayout) view.findViewById(R.id.linearlayout);
textView1.setText(text);
linearlay.setBackgroundColor(color);
}
public void Begin() {
Intent intent=new Intent(UserGuideActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
}
通过initPagerView(View view ,String text,int color)方法可以定制图片文字和颜色。
最后别忘了由于userguide是一个activity组件,需要在Androidmanifest.xml
里面注册
<activity
android:name=".UserGuideActivity"
android:label="@string/app_name" >
</activity>
由于篇幅有限,实现上图效果的其他类并没完全写出来
,需要的同学们可以到http://download.csdn.net/detail/u014354193/9188191下载源码,由于上传的是一个完整的应用,userguide只是其中的一小部分,找起来比较麻烦,所以辛苦大家了O(∩_∩)O