Android:随笔——对页面的View进行截图
我们在做项目时,往往有一个这样的需求:就是对视图的一部分进行截图然后分享出去
这个功能很简单还是简单的看代码吧
<android.support.constraint.ConstraintLayout android:id="@+id/layout_test" android:layout_width="90dp" android:layout_height="90dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.3"> <ImageView android:layout_width="90dp" android:layout_height="90dp" android:src="@drawable/icon_image" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试截图" android:textColor="#2b24c3" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0"/> </android.support.constraint.ConstraintLayout> <Button android:id="@+id/btn_test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="点击测试" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/layout_test"/> <ImageView android:id="@+id/img_show" android:layout_width="90dp" android:layout_height="90dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/btn_test" tools:src="@drawable/icon_image"/>
对 Activity 里面获取控件的代码已省略,直接展示业务代码
public void onClick(View view) { //控件可以进行缓存 mConstraintLayout.setDrawingCacheEnabled(true); //获取缓存的 Bitmap Bitmap drawingCache = mConstraintLayout.getDrawingCache(); //对获取的 Bitmap 进行复制 drawingCache = drawingCache.createBitmap(drawingCache); //关闭视图的缓存 mConstraintLayout.setDrawingCacheEnabled(false); if (drawingCache != null) { mImageView.setImageBitmap(drawingCache); Toast.makeText(this, "获取失败", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "获取成功", Toast.LENGTH_SHORT).show(); } }
总结
看到这个功能感觉无从下手,其实也挺简单的,如果有需求不妨收藏一下,分享给有需求的朋友

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
iOS开发-UITapGestureRecognizer手势
手势在iOS开发中是一个比较常用的功能,不过相对来说大家用的比较少,经常刷网易新闻,上次用了一下捏合手势才发现可以调整字体大小。昨天看到一个介绍摇一摇这个功能的,没看到之前一直都觉得摇一摇是微信的专有的,昨天测试了一下知乎,感觉像发现了一个新大陆,随便截了图,效果如下: 扯的有点远了,很多应用的很多功能其实对于大多数而言是没有用到的,不过作为程序员我们还是应该多研究一下。 基础概念 常见的手势有六种,如下图所示: UITapGestureRecognizer(点击,轻触摸)、UIPinchGestureRecognizer(二指往內或往外拨动,捏合手势)、UIPanGestureRecognizer(拖移)、UISwipeGestureRecognizer(滑动,快速移动)、UIRotationGestureRecognizer(旋转)和UILongPressGestureRecognizer(长按),由于微信的缘故应该大多数人对长按比较熟悉,Tap点击也是高频用到的手势。 苹果官方给出了Tap和Pinch的手势的效果图,其他的效果可以私下试一试: Demo实战 由于有六种手势,基本上...
- 下一篇
android多国语言使用
多国语言:在res目录下建立不同名称的values文件来调用不同的语言包 Values文件汇总如下: 中文(中国):values-zh-rCN 中文(台湾):values-zh-rTW 中文(香港):values-zh-rHK 英语(美国):values-en-rUS 英语(英国):values-en-rGB 英文(澳大利亚):values-en-rAU 英文(加拿大):values-en-rCA 英文(爱尔兰):values-en-rIE 英文(印度):values-en-rIN 英文(新西兰):values-en-rNZ 英文(新加坡):values-en-rSG 英文(南非):values-en-rZA 阿拉伯文(埃及):values-ar-rEG 阿拉伯文(以色列):values-ar-rIL 保加利亚文: values-bg-rBG 加泰罗尼亚文:values-ca-rES 捷克文:values-cs-rCZ 丹麦文:values-da-rDK 德文(奥地利):values-de-rAT 德文(瑞士):values-de-rCH 德文(德国):values-de-rDE 德文...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
-
Docker使用Oracle官方镜像安装(12C,18C,19C)
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS8编译安装MySQL8.0.19
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- MySQL8.0.19开启GTID主从同步CentOS8
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- CentOS7,8上快速安装Gitea,搭建Git服务器
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
推荐阅读
最新文章
- CentOS8编译安装MySQL8.0.19
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- Linux系统CentOS6、CentOS7手动修改IP地址
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- Windows10,CentOS7,CentOS8安装Nodejs环境
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果