android 屏幕截图并缩小 以及存取方法

图片缩小并由Bitmap转换为Imageview方法

static Bitmap ViewToBit(Activity activity){
        float scaleWidth = 1,scaleHeight=1;
        View v=activity.getWindow().getDecorView();
        v.setDrawingCacheEnabled(true);
        v.buildDrawingCache();
        Bitmap b1=v.getDrawingCache();
        int bmpWidth=b1.getWidth();
        int bmpHeight=b1.getHeight();
        double scale=0.5;
         //计算出这次要缩小的比例
        scaleWidth=(float)(scaleWidth*scale);
        scaleHeight=(float)(scaleHeight*scale);
      //产生resize后的Bitmap对象
        Matrix matrix=new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizeBmp=Bitmap.createBitmap(b1, 0, 0, bmpWidth, bmpHeight, matrix, true);
        return resizeBmp;
       
//        Bitmap b=Bitmap.createBitmap(100,100,Bitmap.Config.RGB_565);
//        Bitmap b=v.getDrawingCache(true);
//        Canvas canvas=new Canvas(b);
//        v.draw(canvas);
//        return b;
       
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        bitmap=ViewToBit(this);
        BitmapDrawable bmpDraw=new BitmapDrawable(bitmap);
        ImageView iv2=(ImageView)findViewById(R.id.image02);
        iv2.setImageDrawable(bmpDraw);
       
    }

 

 

屏幕截图保存方法

  1. import  java.io.FileNotFoundException;  
  2. import  java.io.FileOutputStream;  
  3. import  java.io.IOException;  
  4.   
  5. import  android.app.Activity;  
  6. import  android.graphics.Bitmap;  
  7. import  android.graphics.Rect;  
  8. import  android.view.View;  
  9.   
  10. public   class  ScreenShot {  
  11.     // 获取指定Activity的截屏,保存到png文件   
  12.     private   static  Bitmap takeScreenShot(Activity activity){  
  13.         //View是你需要截图的View   
  14.         View view = activity.getWindow().getDecorView();  
  15.         view.setDrawingCacheEnabled(true );  
  16.         view.buildDrawingCache();  
  17.         Bitmap b1 = view.getDrawingCache();  
  18.           
  19.         //获取状态栏高度   
  20.         Rect frame = new  Rect();    
  21.         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);    
  22.         int  statusBarHeight = frame.top;    
  23.         System.out.println(statusBarHeight);  
  24.           
  25.         //获取屏幕长和高   
  26.         int  width = activity.getWindowManager().getDefaultDisplay().getWidth();    
  27.         int  height = activity.getWindowManager().getDefaultDisplay().getHeight();    
  28.         //去掉标题栏   
  29.         //Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);   
  30.         Bitmap b = Bitmap.createBitmap(b1, 0 , statusBarHeight, width, height - statusBarHeight);  
  31.         view.destroyDrawingCache();  
  32.         return  b;  
  33.     }  
  34.       
  35.     //保存到sdcard   
  36.     private   static   void  savePic(Bitmap b,String strFileName){  
  37.         FileOutputStream fos = null ;  
  38.         try  {  
  39.             fos = new  FileOutputStream(strFileName);  
  40.             if  ( null  != fos)  
  41.             {  
  42.                 b.compress(Bitmap.CompressFormat.PNG, 90 , fos);  
  43.                 fos.flush();  
  44.                 fos.close();  
  45.             }  
  46.         } catch  (FileNotFoundException e) {  
  47.             e.printStackTrace();  
  48.         } catch  (IOException e) {  
  49.             e.printStackTrace();  
  50.         }  
  51.     }  
  52.       
  53.     //程序入口   
  54.     public   static   void  shoot(Activity a){  
  55.         ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png" );  
  56.     }  

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值