anndroid 模糊引导界面详解手机开发

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

anndroid 模糊引导界面详解手机开发anndroid 模糊引导界面详解手机开发

我们以前的写法是在需要显示模糊引导的地方,写一个布局,然后第一次使用的时候显示出来。但是这样做代码结构不清晰,所以我们有必要将这些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

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/5547.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论