这里面主要用到的是WindowManager.LayoutParams的属性及windowManager.addVew(View view,LayoutParams params)的方法
将要显示的引导页的View的Params 设置好,在指定的地方生成guidepage的对像,并调用showGuidePage()方法即可。
public class GuidePage {
Button mButton;
Context mContext;
ImageView mGuideImage;
WindowManager mWindowManager;
WindowManager.LayoutParams mImageViewParams;
WindowManager.LayoutParams mButtonParams;
/**
*
* @param context
* @param windowManager the windowManager which will get the guidepage.
* @param resId the drawable resources id for the guidepage to be shown.
* @param btnText the text that the button of the guidepage will show.
* @param btnGravity the gravity attribute of the button ,you can use Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL and so on for this param.
* @return the guidepage instance to show.
*/
public GuidePage(Context context,WindowManager windowManager,int resId,String btnText,int btnGravity){
this.mContext = context;
this.mButton = new Button(mContext);
this.mButton.setText(btnText);
this.mGuideImage = new ImageView(mContext);
this.mGuideImage.setImageResource(resId);
this.mGuideImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
this.mWindowManager = windowManager;
this.mImageViewParams = new LayoutParams();
this.mImageViewParams.alpha = 0.5f;
this.mImageViewParams.width = LayoutParams.MATCH_PARENT;
this.mImageViewParams.height = LayoutParams.MATCH_PARENT;
this.mImageViewParams.type = 2002;
this.mButtonParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,2003,40,1);
this.mButtonParams.gravity = btnGravity;
}
/**
* show the guidepage instance.
*/
public void showGudiePage(){
mWindowManager.addView(mButton, mButtonParams);
mWindowManager.addView(mGuideImage, mImageViewParams);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mWindowManager.removeView(mButton);
mWindowManager.removeViewImmediate(mGuideImage);
}
});
}
}
//用类似下面的方法来判断程序或是某个功能是否是第一次启用
void isFirstRun(){SharedPreferences sp = mContext.getSharedPreferences("pref_first_run", Context.MODE_PRIVATE);
boolean b = sp.getBoolean("isFirstRun",true);
if(b){
new GuidePage(mContext,mWindowManager,0,null,0).showGudiePage();
sp.edit().putBoolean("isFirstRun", false).commit();
}