您的位置:首页 > 移动开发 > Android开发

[置顶] android 添加阴影引导

2015-12-26 17:07 357 查看
           最近需要给自己的Android app添加阴影引导,也就是很多app刚进入的时候,显示一个透明的遮罩,然后遮罩上的某些地方添加一些引导,指引用户去点击app的某些地方。我在这里把自己的方法记录下来,这是我觉得比较简便的一种方法。如果有一些不足你可以指出的话,希望你能告诉我~

          理想中的引导阴影遮罩应该是这样的:我们手指在点击到屏幕的的时候,引导遮罩层会消失,同时如果我们的手指点击到了一些点击事件,那么它也同样能被触发。之前试过几种方法,要么太繁琐,要么点击不能穿透,例如直接添加一个引导的view在我们的window这个对象上。这里我是把引导层加在我的activity的布局上,然后设置ontouh的监听即可。(我的测试设备是红米note2。)

          代码如下:

final View cover = getLayoutInflater().inflate(R.layout.lead, null);
final FrameLayout root = (FrameLayout) findViewById(R.id.root);
root.addView(cover);
cover.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
root.removeView(cover);
return false;
}
});


      然后是我们的布局R.layout.lead: 它就是一个包含了一张图片的简单布局:

         

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/teacher_lead"
>

</ImageView>


     我们的R.id.root指的是我们的Activity setContView所设置的layout的最外层布局的id。

     例如:(其实就是最外层的布局)

    

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ecedf1"
android:orientation="vertical">

<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />

<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:textColor="#c1c1c1" />

<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/maintab_toolbar_bg">

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>

<ImageView
android:id="@+id/leader_lead"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="@drawable/leader_lead"
android:visibility="gone" />
</FrameLayout>




      如上所示,一个简单的引导就做好了,不需要牵扯到当前activity的其它逻辑。如果这个方法有什么问题,希望你能告知我~

上面的方法可能不是最优解,目前我使用的是这个方法,感觉更好:https://github.com/Froyo91/MyCommonCodeRepository/blob/master/%E9%98%B4%E5%BD%B1%E5%BC%95%E5%AF%BC
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: