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

来源:互联网 发布:mac chrome 导入书签 编辑:程序博客网 时间:2024/05/01 19:18

图片缩小并由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.     }  

原创粉丝点击