先上两张图,后面补上代码

我们以前的写法是在需要显示模糊引导的地方,写一个布局,然后第一次使用的时候显示出来。但是这样做代码结构不清晰,所以我们有必要将这些View独立出来,写成一个自定义的View

public class ShowMemberTipsView extends BaseTipsView {

    @Bind(R.id.btn_sure)
    Button btnSure;
    @Bind(R.id.img_close)
    ImageView imgClose;

    private static final String UNIQUE_KEY = ShowMemberTipsView.class.getSimpleName();
    private Context mContext;

    public ShowMemberTipsView(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public ShowMemberTipsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    public ShowMemberTipsView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        init();
    }

    @Override
    public String getUniquekey() {
        return UNIQUE_KEY;
    }

    private void init() {
        initLayoutParams();
        addStatusBarView();
        addContentView();
    }

    private void initLayoutParams() {
        LayoutParams lp = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        setOrientation(VERTICAL);
        setLayoutParams(lp);
    }

    private void addContentView() {
        View view = inflate(mContext, R.layout.view_show_member_tips, null);
        ButterKnife.bind(this,view);
        addView(view);
    }

    @OnClick(R.id.btn_sure)
    public void sure() {
        if (mOnSureListener != null) {
            mOnSureListener.onSure(ShowMemberTipsView.this);
        }
    }

    @OnClick(R.id.img_close)
    public void close() {
        if (mOnCloseListener != null) {
            mOnCloseListener.onClose(ShowMemberTipsView.this);
        }
    }
}

引用的布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E6000000">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/topbar_layout_width"
            android:layout_marginTop="120dp"
            android:orientation="horizontal">

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="fill_parent"
                android:layout_gravity="center_vertical"
                android:layout_weight="1.0">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:src="@drawable/icon_more_mask_btn" />
            </RelativeLayout>

            <View
                android:layout_width="1.0px"
                android:layout_height="fill_parent"
                android:layout_marginBottom="10.0dip"
                android:layout_marginTop="10.0dip" />

            <View
                android:id="@+id/rl_task_center"
                android:layout_width="0.0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1.0" />
        </LinearLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dp"
            android:src="@drawable/icon_more_mask_text" />

        <Button
            android:id="@+id/btn_sure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dp"
            android:background="@drawable/icon_more_mask_level_btn" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>

    <ImageView
        android:id="@+id/img_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:padding="5dp"
        android:src="@drawable/icon_app_upgrade_close" />
</FrameLayout>

最后是一个BaseView,主要是封装一些公共的方法

public abstract class BaseTipsView extends LinearLayout {
    private static final String TAG = BaseTipsView.class.getSimpleName();
    private String mUniqueKey;
    protected OnCloseListener mOnCloseListener;
    protected OnSureListener mOnSureListener;

    public BaseTipsView(Context context) {
        super(context);
        init();
    }

    public BaseTipsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BaseTipsView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        mUniqueKey = getUniquekey();
        if (TextUtils.isEmpty(mUniqueKey)) {
            throw new IllegalArgumentException("Uniquekey must not empty!");
        }
        maskClick();
    }

    //获取状态
    protected void addStatusBarView() {
        View statusBarView = new View(getContext());
        statusBarView.setBackgroundColor(getContext().getResources().getColor(R.color.line_color));
        int statusBarHeight = getStatusBarHeight();
        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
        statusBarView.setLayoutParams(lp);
        addView(statusBarView);
    }

    public abstract String getUniquekey();

    private void maskClick() {
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 因此View背景透明,防止点击到此View后面的控件
            }
        });
    }

    public void show(final Activity activity) {
        if (!isMoreTipsShowed()) {
            addViewToDector(activity);
            setMoreTipsShowed(true);
        }
    }

    public void dismiss(final Activity activity) {
        removeViewFromDector(activity);
    }

    /**
     * 添加View到dectorView
     * @param activity
     */
    private void addViewToDector(final Activity activity) {
        ViewGroup dectorView = (ViewGroup) activity.getWindow().getDecorView();
        dectorView.addView(this);
    }

    /**
     * 从dectorView移出View
     * @param activity
     */
    private void removeViewFromDector(final Activity activity) {
        ViewGroup dectorView = (ViewGroup) activity.getWindow().getDecorView();
        dectorView.removeView(this);
    }

    public void setOnCloseListener(OnCloseListener onCloseListener) {
        this.mOnCloseListener = onCloseListener;
    }

    public void setOnSureListener(OnSureListener onSureListener) {
        this.mOnSureListener = onSureListener;
    }

    public interface OnCloseListener {
        public void onClose(BaseTipsView baseTipsView);
    }

    public interface OnSureListener {
        public void onSure(BaseTipsView baseTipsView);
    }

    protected boolean isMoreTipsShowed() {
        SharedPreferences sp = getContext().getSharedPreferences(TAG, Context.MODE_PRIVATE);
        return sp.getBoolean(mUniqueKey, false);
    }

    protected void setMoreTipsShowed(boolean isShowed) {
        SharedPreferences sp = getContext().getSharedPreferences(TAG, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(mUniqueKey, isShowed);
        editor.commit();
    }

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

}

最后附上源码下载地址:http://download.csdn.net/detail/xiangzhihong8/9549873

anndroid 模糊引导界面的更多相关文章

  1. App 引导界面

    App 引导界面 1.前言 最近在学习实现App的引导界面,本篇文章对设计流程及需要注意的地方做一个浅显的总结. 附上项目链接,供和我水平类似的初学者参考——http://files.cnblogs. ...

  2. android——利用SharedPreference做引导界面

    很久以前就接触过sharedPreference这个android中的存储介质.但是一直没有实际使用过,今天在看之前做的“民用机型大全”的app时,突然想到可以使用sharedPreference类来 ...

  3. 【Android UI设计与开发】第05期:引导界面(五)实现应用程序只启动一次引导界面

    [Android UI设计与开发]第05期:引导界面(五)实现应用程序只启动一次引导界面 jingqing 发表于 2013-7-11 14:42:02 浏览(229501) 这篇文章算是对整个引导界 ...

  4. Android UI开发第四十一篇——墨迹天气3.0引导界面及动画实现

    周末升级了墨迹天气,看着引导界面做的不错,模仿一下,可能与原作者的代码实现不一样,但是实现的效果还是差不多的.先分享一篇以前的文章,android动画的基础知识,<Android UI开发第十二 ...

  5. 【Android UI设计与开发】3.引导界面(三)实现应用程序只启动一次引导界面

    大部分的引导界面基本上都是千篇一律的,只要熟练掌握了一个,基本上也就没什么好说的了,要想实现应用程序只启动一次引导界面这样的效果,只要使用SharedPreferences类,就会让程序变的非常简单, ...

  6. 【Android】首次进入应用时加载引导界面

    参考文章: [1]http://blog.csdn.net/wsscy2004/article/details/7611529 [2]http://www.androidlearner.net/and ...

  7. SharedPreference 存储小量数据,一般首次启动显示引导界面就用这个。

    写://添加一个SharedPreference并传入数据SharedPreference sharedPreferences = getSharedPreferences("share_d ...

  8. 转-ViewPager组件(仿微信引导界面)

    http://www.cnblogs.com/lichenwei/p/3970053.html 这2天事情比较多,都没时间更新博客,趁周末,继续继续~ 今天来讲个比较新潮的组件——ViewPager ...

  9. 【笔记】WPF实现ViewPager引导界面效果及问题汇总

    最近在开发项目的首次使用引导界面时,遇到了问题,引导界面类似于安卓手机ViewPager那样的效果,希望通过左右滑动手指来实现切换不同页面,其间伴随动画. 实现思路: 1.界面布局:新建一个UserC ...

随机推荐

  1. 算法之路(三)----查找斐波纳契数列中第 N 个数

    算法题目 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: * 前2个数是 0 和 1 . * 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, 1 ...

  2. shiro salt

    1.1 散列算法 散列算法一般用于生成一段文本的摘要信息,散列算法不可逆,将内容可以生成摘要,无法将摘要转成原始内容.散列算法常用于对密码进行散列,常用的散列算法有MD5.SHA.分享牛系列,分享牛专 ...

  3. UE4使用第三方库读写xml文件

    原文链接:http://gad.qq.com/article/detail/7181031 本文首发腾讯GAD开发者平台,未经允许,不得转载 在游戏开发过程中,读写xml几乎已经成为不可或缺的功能,但 ...

  4. 自制 Python小工具 将markdown文件转换成Html文件

    今天看到了一个Python库,名为markdown.瞬间就给了我一个灵感,那就是制作一个将markdown文件转换成html文件的小工具. 我的实验环境 操作系统: Windows 7 64位 旗舰版 ...

  5. static,this,private关键字用法

    1:成员变量和局部变量的区别(理解) (1)在类中的位置不同 成员变量:类中方法外 局部变量:方法定义中或者方法声明上 (2)在内存中的位置不同 成员变量:在堆中 局部变量:在栈中 (3)生命周期不同 ...

  6. Android简易实战教程--第二十二话《自定义组合控件模拟qq登录下拉框和其中的一些”小技巧”》

    转载此文章请注明出处:点击打开链接   http://blog.csdn.net/qq_32059827/article/details/52313516 首先,很荣幸此专栏能被CSDN推荐到主页.荣 ...

  7. 剑指Offer——迅雷笔试题+知识点总结

    剑指Offer--迅雷笔试题+知识点总结 情景回顾 时间:2016.9.19 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:迅雷笔试 总体来说,迅雷笔试内容体量不算多,主要 ...

  8. Android之获取屏幕的尺寸像素及获取状态栏标题栏高度

    在Android的实际开发中,会经常用到获取屏幕的尺寸的问题,以便设置一些布局在屏幕上的固定位置,从而适配各个屏幕的设备. 今天我就来讲一下怎么得到当前设备的屏幕像素吧: 一.在Activity中: ...

  9. 关于Python编程的一些问答

    关于Python编程的一些问答 导语 大约1个月前,oschina.net和华章图书一起合作做了一个活动:OSC第51期高手问答--聊聊python那些事,来推广我参与撰写的书<编写高质量代码: ...

  10. Android之Notification-android学习之旅(二)

    notification常用于下拉式的消息推送. Notification的构成 Nitification的实例 1.新建一个Builder,要选Notification.compat包. 2.然后用 ...