用DecorView实现应用引导

WindowManager和Window都可以获取根View,只是获取的root view不同,前者是整个OS中唯一的一个窗口管理的windows,作为引导的话有点问题的,并不适合做引导,那么看到了window里面也可以获取根view--它是ViewGroup,可以尝试在当前activity中获取DecorView往里面添加view的方式实现。使用方式:遮罩图片若干就像下面数组似的,在要引导的activity的布局中的root节点添加id=“root_container”,当然你可以根据需要改


首先写以供DecorView的管理类:

public class DecorViewGuideHelper {
    private Activity activity;
    private View rootView;
    private List coverIds;
    private int current = 0;
    private int length = 0;
    private int CONTINUE = 0x1234;
    public DecorViewGuideHelper(Activity activity, List coverIds) {
        this.activity = activity;
        this.coverIds = coverIds;
        this.rootView = activity.getWindow().getDecorView().findViewById(R.id.root_container);
        SharePreferenceUtil.getInstance(activity).setGUIDId(4);
        this.length = coverIds.size();
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x1234:
                    current++;
                    display();
                    break;

                default:
                    break;
            }
        }

    };
    public void display() {
       final  ViewParent viewParent = rootView.getParent();
        if (viewParent instanceof FrameLayout) {
            if (length > 0 && current < length) {
                final FrameLayout container = (FrameLayout) viewParent;
                coverIds.get(current).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        container.removeView(coverIds.get(current));
                   
                        handler.sendEmptyMessage(CONTINUE);
                    }
                });
                container.addView(coverIds.get(current));
            }
        }

    }
}

组装view数组

private void initGuid(LayoutInflater inflater){   
 guid=new ArrayList<>();   
 guid.add(inflater.inflate(R.layout.layout_guid1, null));    
View guid2=inflater.inflate(R.layout.layout_guid2, null);    
View lltop2=guid2.findViewById(lltop);    
ViewGroup.LayoutParams para2;   
 para2 =  lltop2.getLayoutParams();    
para2.width = ScreenUtils.getScreenWidth(getActivity());   
 para2.height = para2.width*10/22;   
 lltop2.setLayoutParams(para2);   
 guid.add(guid2);   
 View guid3=inflater.inflate(R.layout.layout_guid3, null);   
 View lltop3=guid3.findViewById(lltop);    
ViewGroup.LayoutParams para3;   
 para3 =  lltop3.getLayoutParams();    
para3.width = ScreenUtils.getScreenWidth(getActivity());    
para3.height = para3.width*6/22;    
lltop3.setLayoutParams(para3);    
guid.add(guid3);    
View guid4=inflater.inflate(R.layout.layout_guid4, null);    
View lltop4=guid4.findViewById(lltop);    
ViewGroup.LayoutParams para4;    
para4 =  lltop4.getLayoutParams();    
para4.width = ScreenUtils.getScreenWidth(getActivity());    
para4.height = para4.width*12/22;    
lltop4.setLayoutParams(para4);   
 guid.add(guid4);
}

写四个布局:


    
  
          
    



   
    
 

.......................

然后调用

private void showGuide() {   
 DecorViewGuideHelper helper = new DecorViewGuideHelper(getActivity(),guid);   
 helper.display();
}

效果如图:

用DecorView实现应用引导_第1张图片
![应用宝截屏2016121302.png](http://upload-images.jianshu.io/upload_images/3403713-8ea8fe6867a3c1aa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

你可能感兴趣的:(用DecorView实现应用引导)