2015-09-29 87 views
0

我使用了一個開源庫來抓取imageview。我已經申請了圖書館的觀點一樣,位圖抓取後給出空白屏幕截圖

<com.winsontan520.WScratchView 
    xmlns:wsv="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/scratch_view" 
    android:layout_width="300dp" 
    android:layout_height="300dp" 
    android:layout_centerInParent="true" /> 

(注:這個觀點延伸到surfaceview)。

那麼我在該視圖上

WScratchView _imageToErase = (WScratchView) findViewById(R.id.scratch_view); 
BitmapFactory.Options options2 = new BitmapFactory.Options(); 
options2.inDither = false; 
options2.inPurgeable = true; 
options2.inInputShareable = true; 
options2.inPreferredConfig = Config.ARGB_8888; 
Bitmap bitmap = BitmapFactory.decodeFile(filepath, options2); 
_imageToErase.setScratchBitmap(bitmap); 

然後圖像源設置到該視圖後,我會劃傷它一點施加的圖像源。刮擦後,我已經採取截圖該視圖的像從這一觀點,即

View u = findViewById(R.id.scratch_view); 
u.setDrawingCacheEnabled(true); 
u.buildDrawingCache(true); 
Bitmap b = Bitmap.createBitmap(u.getDrawingCache()); 
u.setDrawingCacheEnabled(false); 
File mlr = new File(Environment.getExternalStorageDirectory() 
.toString() + File.separator + "Colors"); 
    if (!mlr.exists()) { 
      mlr.mkdir(); 
     } 
String extr = mlr.getPath(); 
String fileName = new SimpleDateFormat("yyyyMMddhhmm'_Colors.png'") 
.format(new Date()); 
File myPath = new File(extr, fileName); 
FileOutputStream fos = null; 
try { 
     fos = new FileOutputStream(myPath); 
     b.compress(Bitmap.CompressFormat.PNG, 100, fos); 
     fos.flush(); 
     fos.close(); 
     MediaStore.Images.Media.insertImage(getContentResolver(), b, 
     "Screen", "screen"); 
    } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 

採取屏幕截圖示出了空白的視圖,有或沒有劃傷。我搜查了很多,但我不知道我在哪裏做錯了。任何幫助將非常感激。謝謝

回答

0

這是因爲呈現SurfaceView這是從任何其他角度不同。

嘗試這樣的:

Bitmap screenshot = Bitmap.createBitmap(_imageToErase.getWidth(), _imageToErase.getHeight(), Bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(screenshot); 
_imageToErase.draw(canvas); 


File mlr = new File(Environment.getExternalStorageDirectory() 
.toString() + File.separator + "Colors"); 
    if (!mlr.exists()) { 
      mlr.mkdir(); 
     } 
String extr = mlr.getPath(); 
String fileName = new SimpleDateFormat("yyyyMMddhhmm'_Colors.png'") 
.format(new Date()); 
File myPath = new File(extr, fileName); 
FileOutputStream fos = null; 
try { 
     fos = new FileOutputStream(myPath); 
     screenshot.compress(Bitmap.CompressFormat.PNG, 100, fos); 
     fos.flush(); 
     fos.close(); 
     MediaStore.Images.Media.insertImage(getContentResolver(), b, 
     "Screen", "screen"); 
    } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
+0

感謝您的答覆,這是大加讚賞。 Mehul,你沒有在你的代碼中進行任何屏幕截圖。但是我寫了你的前三行,並截取了截圖。它不工作。 –