ShowCase──操作引导的简单实现
2015-06-06 21:12
363 查看
前言
实现上还不完善,主要是思路的展示,为了抛砖引玉。效果图
原理
自定义 Dialog 展示引导视图。设置 Dialog 的 BackgroundDrawable 为一个有一块透明的 Bitmap。这个 Bitmap 借由 Canvas 创建。中间透明则用到 Paint 的 XferMode。paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));。使用它可以镂空。
源码
[code]public class ShowCaseDialog extends Dialog { private Window dialogWindow; private Rect transparentRect; public ShowCaseDialog(Context context) { super(context); init(); } public ShowCaseDialog(Context context, int theme) { super(context, theme); init(); } protected ShowCaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); init(); } private void init() { requestWindowFeature(Window.FEATURE_NO_TITLE); dialogWindow = getWindow(); dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); dialogWindow.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); WindowManager.LayoutParams params = dialogWindow.getAttributes(); params.width = getWindowWidth(); params.height = getWindowHeight(); dialogWindow.setAttributes(params); LayoutInflater inflater = LayoutInflater.from(getContext()); inflater.inflate(R.layout.dialog_show_case, (ViewGroup) getWindow().getDecorView(), true); } private int getWindowWidth() { return getContext().getResources().getDisplayMetrics().widthPixels; } public void setTransparentRect(Rect transparentRect) { this.transparentRect = transparentRect; dialogWindow.setBackgroundDrawable(getBgDrawable()); } public Drawable getBgDrawable(){ Activity activity = getOwnerActivity(); Bitmap bgBitmap = Bitmap.createBitmap(getWindowWidth(), getWindowHeight(), Bitmap.Config.ARGB_8888); Canvas bgCanvas = new Canvas(bgBitmap); bgCanvas.drawColor(Color.parseColor("#cc000000")); Paint paint = new Paint(); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); bgCanvas.drawRect(transparentRect, paint); return new BitmapDrawable(getContext().getResources(), bgBitmap); } public int getWindowHeight(){ return getContext().getResources().getDisplayMetrics().heightPixels - Dimension.getStatusBarHeight(getContext()); } }
[code]public class TestShowCaseDialogActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_show_case_dialog); final Button button = (Button) findViewById(R.id.button); button.post(new Runnable() { @Override public void run() { ShowCaseDialog showCaseDialog = new ShowCaseDialog(TestShowCaseDialogActivity.this); int[] location = new int[2]; // button.getLocationInWindow(location); button.getLocationOnScreen(location); int statusBarHeight = Dimension.getStatusBarHeight(getApplicationContext()); Rect transparentRect = new Rect(location[0], location[1] - statusBarHeight, location[0] + button.getMeasuredWidth(), location[1] + button.getMeasuredHeight() - statusBarHeight); showCaseDialog.setTransparentRect(transparentRect); showCaseDialog.show(); } }); } }
遇到的问题
确定控件在非系统区域的位置利用函数
getLocationOnScreen,然后向下偏移 StatusBar 的高度。
Dialog 的布局 LayoutParamas 没有起效
不用
setContentView(int ResId),用
inflater.inflate(R.layout.dialog_show_case, (ViewGroup) getWindow().getDecorView(), true);。
相关文章推荐
- 有限自动机编程
- 【学习笔记】【C语言】关系运算符
- 小小的告别一下这个博客
- rsync 安装使用详解
- mongoDB常见的查询索引(三)
- mongoDB常见的查询索引(三)
- 我们来谈谈C++里面的字符串
- 【转】各种开源项目合集
- windows server 2008 R2域中的DC部署
- 如何给AndroidStudio引入So(百度地图)..........
- Hive常用的SQL命令操作
- windows server 2008 R2域中的DC部署 分类: AD域 Windows服务 2015-06-06 21:09 68人阅读 评论(0) 收藏
- 字符串拼接效率比较
- C语言第一天 环境配置 QT与XCode安装
- Python打包为exe使用py2exe时中文乱码问题
- ruby 常注意的
- CentOSyum操作
- 高人对于LIBSVM的经典总结(全面至极)
- java的clone用法
- 河南省系统集成证书转注