Android之j截取全屏代码

分享一个简单的截屏代码。

1.主Activity

 
  1. package com.zhf.ScreenshotDemo;  
  2.   
  3. import Android.app.Activity;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.drawable.BitmapDrawable;  
  6. import android.os.Bundle;  
  7. import android.view.Display;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.ImageView;  
  12.   
  13. public class ScreenshotDemoActivity extends Activity {  
  14.     private Button shotButton;  
  15.     private ImageView imageView;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         shotButton=(Button)findViewById(R.id.shotButton);  
  21.         imageView=(ImageView)findViewById(R.id.imageView);  
  22.         shotButton.setOnClickListener(new OnClickListener() {  
  23.             @Override  
  24.             public void onClick(View v) {  
  25.                 //Bitmap-->Drawable   
  26.                 BitmapDrawable bd=new BitmapDrawable(shot());  
  27.                 imageView.setBackgroundDrawable(bd);  
  28. //              imageView.setImageBitmap(shot());   
  29.             }  
  30.         });  
  31.     }  
  32.   
  33.     /** 
  34.      * 截屏方法 
  35.      * @return 
  36.      */  
  37.     private Bitmap shot() {  
  38.         View view = getWindow().getDecorView();  
  39.         Display display = this.getWindowManager().getDefaultDisplay();  
  40.         view.layout(00, display.getWidth(), display.getHeight());  
  41.         view.setDrawingCacheEnabled(true);//允许当前窗口保存缓存信息,这样getDrawingCache()方法才会返回一个Bitmap   
  42.         Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache());  
  43.         return bmp;  
  44.     }  
  45. }  

2.布局

 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <ImageView android:id="@+id/imageView"  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="350dip"  
  9.         />  
  10.   
  11.     <Button android:id="@+id/shotButton"  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="shot"/>  
  15. </LinearLayout> 


1. 截屏函数似乎应该在onCreate方法之后调用,否则截取不到

2. 截取的图片比屏幕要小,看起来像是少了一个标题栏的高度


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值