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  setDrawingCacheEnabled(boolean flag)

Bitmap  getDrawingCache(boolean autoScale)

void  buildDrawingCache(boolean autoScale)

void  destroyDrawingCache()

我们要获取它的cache先要通过setDrawingCacheEnable方法把cache开启,然后再调用getDrawingCache方法就可 以获得view的cache图片了。buildDrawingCache方法可以不用调用,因为调用getDrawingCache方法时,若果 cache没有建立,系统会自动调用buildDrawingCache方法生成cache。若果要更新cache, 必须要调用destoryDrawingCache方法把旧的cache销毁,才能建立新的。当调用setDrawingCacheEnabled方法设置为false, 系统也会自动把原来的cache销毁。


ViewGroup在绘制子view时,而外提供了两个方法
void setChildrenDrawingCacheEnabled(boolean enabled)
setChildrenDrawnWithCacheEnabled(boolean enabled)

setChildrenDrawingCacheEnabled方法可以使viewgroup里所有的子view开启cache, setChildrenDrawnWithCacheEnabled使在绘制子view时,若该子view开启了cache, 则使用它的cache进行绘制,从而节省绘制时间。

获取cache通常会占用一定的内存,所以通常不需要的时候有必要对其进行清理,通过destroyDrawingCache或setDrawingCacheEnabled(false)实现。