用DecorView实现应用引导

sxet3256 8年前
   <p>WindowManager和Window都可以获取根View,只是获取的root view不同,前者是整个OS中唯一的一个窗口管理的windows,作为引导的话有点问题的,并不适合做引导,那么看到了window里面也可以获取根view--它是ViewGroup,可以尝试在当前activity中获取DecorView往里面添加view的方式实现。使用方式:遮罩图片若干就像下面数组似的,在要引导的activity的布局中的root节点添加id=“root_container”,当然你可以根据需要改</p>    <pre>  <code class="language-java"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/root_container"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical"></code></pre>    <p>首先写以供DecorView的管理类:</p>    <pre>  <code class="language-java">public class DecorViewGuideHelper {      private Activity activity;      private View rootView;      private List<View> coverIds;      private int current = 0;      private int length = 0;      private int CONTINUE = 0x1234;      public DecorViewGuideHelper(Activity activity, List<View> 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));              }          }        }  }</code></pre>    <p>组装view数组</p>    <pre>  <code class="language-java">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);  }</code></pre>    <p>写四个布局:</p>    <pre>  <code class="language-java"><?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:background="#88000000"      android:orientation="vertical">      <RelativeLayout          android:layout_width="match_parent"          android:layout_marginTop="30dp"          android:layout_height="match_parent">      <ImageView          android:id="@+id/head"          android:layout_alignParentRight="true"          android:layout_width="52dp"          android:layout_height="52dp"          android:src="@mipmap/icon_circle_guild"/>          <ImageView              android:layout_below="@+id/head"            android:layout_centerHorizontal="true"              android:layout_marginTop="10dp"              android:layout_marginRight="20dp"              android:layout_width="229dp"              android:layout_height="505dp"              android:src="@mipmap/pic_font1"            />      </RelativeLayout>  </LinearLayout></code></pre>    <pre>  <code class="language-java"><?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:background="#88000000"      android:orientation="vertical"    >      <View          android:id="@+id/lltop"          android:layout_width="match_parent"          android:layout_height="150dp"/>      <ImageView          android:layout_width="294dp"          android:layout_height="391dp"          android:layout_gravity="center_horizontal"          android:src="@mipmap/pic_font2"/>  </LinearLayout>  .......................</code></pre>    <p>然后调用</p>    <pre>  <code class="language-java">private void showGuide() {      DecorViewGuideHelper helper = new DecorViewGuideHelper(getActivity(),guid);      helper.display();  }</code></pre>    <p>效果如图:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/3b0142a118b2119dbab80dfccc8fedaa.png"><img src="https://simg.open-open.com/show/e87426371e0aa2dbfd7c31f3a1830157.png"></p>    <p style="text-align:center">应用宝截屏2016121302.png</p>    <p> </p>    <p>来自:http://www.jianshu.com/p/bee20268aff1</p>    <p> </p>