2014-04-25 39 views
5

我想轉換我的應用程序的屏幕並將其轉換爲位圖。我已經知道如何將視圖轉換爲位圖,但這不是我想要的,因爲我的屏幕由許多碎片組成。因此,我希望能夠把屏幕,並將其轉換成位圖... 幫助高度讚賞謝謝。Android將當前屏幕轉換爲位圖

+1

那麼截圖? https://code.google.com/p/android-screenshot-library/ – shkschneider

+0

你看,我不希望用戶知道截圖已被採取。只是捕獲屏幕加載到位圖,然後將位圖設置爲圖像視圖 – SynerFlow

回答

9

你可以使用這樣的事情並通過佈局一個視圖;)

public Bitmap takeScreenShot(View view) { 
     // configuramos para que la view almacene la cache en una imagen 
     view.setDrawingCacheEnabled(true); 
     view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW); 
     view.buildDrawingCache(); 

     if(view.getDrawingCache() == null) return null; // Verificamos antes de que no sea null 

     // utilizamos esa cache, para crear el bitmap que tendra la imagen de la view actual 
     Bitmap snapshot = Bitmap.createBitmap(view.getDrawingCache()); 
     view.setDrawingCacheEnabled(false); 
     view.destroyDrawingCache(); 

     return snapshot; 
    } 
+0

好的,繪圖緩存會做什麼?我想要捕捉屏幕不僅僅是一個視圖。我的屏幕是零散的,有許多視圖分佈在xml佈局文件中,在一個視圖中合併在一起 – SynerFlow

+0

如果您傳遞佈局根或主視圖,它將拍攝屏幕內容的快照;) – jackgris

+0

獲取圖形緩存的操作?你需要閱讀這個[getDrawingCache](http://developer.android.com/reference/android/view/View.html#getDrawingCache%28boolean%29) – jackgris

0

View是你的根佈局: -

View v = view.getRootView(); 
v.setDrawingCacheEnabled(true); 
Bitmap b = v.getDrawingCache(); 
String extr = Environment.getExternalStorageDirectory().toString(); 
File myPath = new File(extr, getString(R.string.free_tiket)+".jpg"); 
FileOutputStream fos = null; 
try { 
    fos = new FileOutputStream(myPath); 
    b.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
    fos.flush(); 
    fos.close(); 
    MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen"); 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

見鏈接的詳細信息: -

http://code.google.com/p/android-screenshot-library/wiki/DeveloperGuide

Android - How to take screenshot programatically

+0

也許你沒有看到我的上述評論。我不想實際拍攝截圖。只需將屏幕轉換爲位圖並將其設置爲ImageView的ImageBitmap即可。 – SynerFlow

0
View rootView = findViewById(R.id.relative_facebook).getRootView(); 
     rootView.setDrawingCacheEnabled(true); 
     return rootView.getDrawingCache();