Android截屏实现——亲测有效代码
来源:互联网 发布:淘宝质量好的鞋店 编辑:程序博客网 时间:2024/05/17 17:16
Android截屏实现——亲测有效代码
将截取到的Bitmap赋给Dialog上的ImageView,并对Dialog加了弹出和收起的动画,实现截屏效果。
1.弹出Dialog方法(截屏+弹出Dialog):
private void popShotSrceenDialog(){ final AlertDialog cutDialog = new AlertDialog.Builder(this).create(); View dialogView = View.inflate(this, R.layout.show_cut_screen_layout, null); ImageView showImg = (ImageView) dialogView.findViewById(R.id.show_cut_screen_img); dialogView.findViewById(R.id.share_cancel).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cutDialog.dismiss(); } }); dialogView.findViewById(R.id.share_img).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(ShotSrceenActivity.this,"点击了share按钮",Toast.LENGTH_SHORT).show(); } }); //获取当前屏幕的大小 int width = getWindow().getDecorView().getRootView().getWidth(); int height = getWindow().getDecorView().getRootView().getHeight(); //生成相同大小的图片 Bitmap temBitmap = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 ); //找到当前页面的跟布局 View view = getWindow().getDecorView().getRootView(); //设置缓存 view.setDrawingCacheEnabled(true); view.buildDrawingCache(); //从缓存中获取当前屏幕的图片 temBitmap = view.getDrawingCache(); showImg.setImageBitmap(temBitmap); cutDialog.setView(dialogView); Window window = cutDialog.getWindow(); window.setBackgroundDrawableResource(android.R.color.transparent); WindowManager m = window.getWindowManager(); Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用 WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值 p.height = (int) (d.getHeight() * 0.8); // 高度设置为屏幕的0.6 p.gravity = Gravity.CENTER;//设置弹出框位置 window.setAttributes(p); window.setWindowAnimations(R.style.dialogWindowAnim); cutDialog.show(); }
2.Dialog布局(图片+按钮):
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/btn_layout" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/share_img" android:layout_width="0dp" android:layout_height="wrap_content" android:text="分享" android:layout_weight="1"/> <Button android:id="@+id/share_cancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消"/> </LinearLayout> <ImageView android:id="@+id/show_cut_screen_img" android:layout_above="@id/btn_layout" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
3.弹出对话框style:
<!--弹出对话框动画--> <style name="dialogWindowAnim" parent="android:Animation" mce_bogus="1"> <item name="android:windowEnterAnimation">@anim/popview_in_amin</item> <item name="android:windowExitAnimation">@anim/popview_out_amin</item> </style>
4.弹出动画:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:interpolator="@android:anim/accelerate_interpolator" android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:fillAfter="false" android:duration="200"/> </set>
5.收起动画:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:interpolator="@android:anim/accelerate_interpolator" android:fromXScale="1.0" android:toXScale="1.0" android:fromYScale="1.0" android:toYScale="0.0" android:pivotX="0%" android:pivotY="100%" android:fillAfter="false" android:duration="200"/> </set>
截取屏幕用上面的方法所得到的图片状态栏位置是白色的一片。测试的时候我认为应该是透明的,解决方式是将状态栏设置为透明,然后再主布局中的最上面加上一个View来改变状态栏颜色,这样截取的Bitmap最上方状态栏就不会是白色的了。
当在一个Activity中需要多次截取不同图片时,要注意用到
//销毁当前屏幕图片的缓存view.destroyDrawingCache();view.setDrawingCacheEnabled(false)
View组件显示的内容可以通过cache机制保存为bitmap, 使用到的api有
void
Bitmap
void
void
void setChildrenDrawingCacheE
setChildrenDrawnWithCach
setChildrenDrawingCacheE
获取cache通常会占用一定的内存,所以通常不需要的时候有必要对其进行清理,通过destroyDrawingCache或setDrawingCacheEnabled(false)实现。
- Android截屏实现——亲测有效代码
- android 调用系统命令实现关机2017亲测有效
- php登录验证及代码实现 含数据库设计 亲测有效
- pv操作实现(代码简洁有效)
- mac下如何上传代码到github(亲测有效)
- python 拨号代码(win10 系统亲测有效)
- android截屏代码实现方法
- android截屏代码:C++实现
- android截屏代码实现方法
- android截屏代码实现方法
- android--代码实现手机截屏
- android截屏代码:C++实现
- android截屏代码实现方法
- android代码实现截屏功能
- android实现截屏功能代码
- android实现截屏功能代码
- Android实现截屏功能代码
- 亲测有效——C#制作图片Button
- 嵌智捷达科技产品型号:CM-i.MX7D核心板
- 真传奇:看看这群清华学生的牛X岁月
- Django分页Paginator
- Spring+Quartz 'jobLauncher' is not serializable
- JavaScript获取浏览器高度和宽度值
- Android截屏实现——亲测有效代码
- bzoj1861 [Zjoi2006]Book 书架
- tomcat的server.xml详解
- Flume架构(一)
- [luogu]P1576最小花费-最短路裸题
- Python bottle 笔记(三) —— route
- (算法分析Week16)132 Pattern[Medium]
- 数据结构实验之排序三:bucket sort
- 关于我不想看到ubuntu中 “检测到系统程序错误”