Android使用ShowcaseView添加半透明操作提示图片的方法
来源:互联网 发布:win10共享文件夹软件 编辑:程序博客网 时间:2024/04/29 02:49
那么我们怎样在自己的安卓应用上也实现类似的功能呢?相关的中文讨论不多,我看到过如下两个:
- http://chenyoca.iteye.com/blog/1188807
- http://www.2cto.com/kf/201210/161838.html
但是过程都不算简单,这种轮子
性质的工作,我们自己来做反而费力不讨好,在Stackoverflow
上有人给出了解答:
- http://stackoverflow.com/questions/12013334/how-do-you-create-a-transparent-demo-screen-for-an-android-app
- http://stackoverflow.com/questions/26293836/how-to-add-a-semi-transparent-demo-screen-using-showcaseview
使用ShowcaseView开源项目可以非常简单的实现以上的功能。下面的截图是我实现的效果。
Picture 1 是主界面, Picture 2是ShowcaseView默认实现, Picture 3是ShowcaseView用户自定义实现,下面我将简要介绍实现的步骤。
如果你是使用Android Studio,在build.gradle文件中添加下面的代码来依赖ShowcaseView库
compile 'com.github.amlcurran.showcaseview:library:5.0.0'
如果是使用其他IDE,请看这个说明。本项目使用Android Studio 0.8.6.
新建三个Activity:MainActivity, DefaultActivity, CustomActivity.
假设在DefaultActivity中有一个按钮,你想像Picture 2那样突出它,在DefaultActivity的
onCreate()
方法中添加如下代码:Button get_src_bn = (Button)findViewById(R.id.get_source_bn); get_src_bn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Uri uri = Uri.parse("https://github.com/Beeder/ShowcaseViewDemo"); Intent intent = new Intent(Intent.ACTION_VIEW,uri); startActivity(intent); } }); new ShowcaseView.Builder(this) .setTarget(new ViewTarget(get_src_bn))//set button as target .setContentTitle("Default ShowcaseView") .setContentText("This is highlighting the button view.\nIn Default ShowcaseView, you must set the Target you want to highlight!") .hideOnTouchOutside() .build();
这就是使用ShowcaseView的默认方法,很简单吧,你想突出什么,就以什么为Target。关于ShowcaseView的Target的更多内容,可以参考Target-API。
在CustomActivity中,你不想突出任何一个按钮或者View,你只是想像Picture 3一样显示一个半透明图片告诉用户某种手势操作,那么你的代码应该是这样的:
ShowcaseView showcaseView = new ShowcaseView.Builder(this) .setStyle(R.style.Custom_semi_transparent_demo)//setStyle instead of setTarget! .hideOnTouchOutside() .build(); //showcaseView.setBackground(getResources().getDrawable(R.drawable.swipe_back_en));//minAPI=16 showcaseView.setBackgroundDrawable(getResources().getDrawable(R.drawable.swipe_back_en));//deprecated.
你没有target任何一个view,而是使用了
setStyle
来手动的设置一个背景主题,那么这个背景主题就是关键了,Custom_semi_transparent_demo
的代码如下:<!--look at this: https://github.com/amlcurran/ShowcaseView/issues/159--> <!--you can customize it--> <style name="Custom_semi_transparent_demo" parent="ShowcaseView.Light"> <item name="sv_backgroundColor">#663d4353</item> <item name="sv_showcaseColor">#25467A</item> <item name="sv_buttonText">Close</item> </style>
可以看到这个背景主题是继承了ShowcaseView.Light主题,然后修改了背景颜色、字体颜色和按钮文字。设置完正确的style之后,还需要马上设置背景图片,这个例子中的背景图片是
R.drawable.swipe_back_en
,图片必须是png格式的透明图片。
整个步骤就完了,你将看到和上面三张图片显示一样的界面。更多的自定义功能可以看官方wiki。这个Demo的源代码以及英文版介绍请看ShowcaseViewDemo
2014.12.24更新
作者本人已表明,该项目不支持导入Eclipse,请看Issue,不过也有大神成功了,可以看这个回答。
(由此引出的关于“怎样在eclipse中导入library的资料”:http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject、http://stackoverflow.com/a/8281477。)有网友想在一个activity中添加多张半透明提示图片,可以参考https://github.com/amlcurran/ShowcaseView/wiki/Display-showcase-views-sequentially-on-the-same-screen。也就是重载某一个showcaseview的
onShowcaseViewHide(ShowcaseView showcaseView);
或onShowcaseViewDidHide(ShowcaseView showcaseView);
在这个函数里面创建一个新的showcaseview显示,这样就实现了连续显示多个的效果。本Demo已经添加了显示多个提示图片的功能,可以去https://github.com/Beeder/ShowcaseViewDemo下载查看。最新的showcaseview版本是5.0.0,在这个版本中不能修改单个高亮圆圈的半径,只能在
/res/values/dimens.xml
里面重载如下代码。(https://github.com/amlcurran/ShowcaseView/issues/259)<dimen name="showcase_radius">94dp</dimen> <dimen name="showcase_radius_inner">96dp</dimen> <dimen name="showcase_radius_outer">128dp</dimen>
这样会导致项目中所有showcaseview的半径都改了,在旧版本中可以修改单个showcaseview的半径大小,有需要的可以自取。期待作者将修改半径功能增加回来。
- Android使用ShowcaseView添加半透明操作提示图片的方法
- Android使用ShowcaseView添加半透明操作提示图片的方法
- Android添加半透明操作提示的方法
- Android做半透明操作提示图片的方法
- Android做半透明操作提示图片的方法
- 使用半透明div覆盖图片的方法
- Android半透明提示效果的实现
- ShowcaseView
- 一个简单的半透明pop提示窗方法
- Android TextView中添加图片的方法
- 去掉App自动添加的图标半透明效果的方法
- 画半透明的图片
- 图片的半透明处理
- android中图片操作的常用方法
- 使用CImage绘制半透明图片
- J2ME图片半透明处理(使用图片像素)
- 图片的透明半透明显示!
- VC图片的半透明处理
- 字符编码详解及由来
- A* 算法
- tomcat中的线程问题
- TexturePacker命令行使用
- MVC设计模式
- Android使用ShowcaseView添加半透明操作提示图片的方法
- Oracle 游标for循环
- Java之代理(jdk静态代理,jdk动态代理,cglib动态代理,aop,aspectj)
- 大话设计模式C++实现-第27章-解释器模式
- Android开发者网址导航
- 排序技术_各种算法原理 图解 代码实现
- MyMathLib系列(订正两个函数)
- php-excel-reader-2.21 读取excel文件的基本用法
- 设计高可用Web服务