2017-05-19 8 views
0

두 개의 화면 비트 맵을 얻고 싶습니다. 문제는 TextView 또는 TextClock에 첫 번째 호출의 값이 getDrawingCache() 인 것입니다. TextView 또는 TextClock에 대한 값은 getDrawingCache()의 두 번째 호출 이후 새로 Bitmap으로 새로 고쳐지지 않습니다.안드로이드 getDrawingCache() TextView/TextClock에서 실제 값을 포함하지 않습니다

Nexus 5에서 작업하십시오 (두 번째 비트 맵의 ​​정확한 시간은 TextClock입니다).
다른 많은 장치에서는 작동하지 않습니다. 예 : Huawei Honor 4C.

minSdkVersion 21 
targetSdkVersion 25 
compileSdkVersion 25 

TextClock은 자동으로 값을 변경합니다. TextViewsetText() 메서드를 호출해도 작동하지 않습니다.
어디에 문제가 있습니까?

테스트 :
과 함께 간단한 레이아웃을 만듭니다 TextClock 및 UI 스레드에서이 코드를 실행 :

new Handler().postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     Log.i("TAG", Thread.currentThread().getName()); 
     try { 
      Bitmap bitmapScreen = getBitmapOfScreen(); 
      //File f = new File("/storage/sdcard1", "tmp.jpg"); //Huawei 
      File f = new File("/storage/emulated/0", "tmp.jpg"); //Nexus 5 
      FileOutputStream fis = new FileOutputStream(f); 
      bitmapScreen.compress(Bitmap.CompressFormat.JPEG, 100, fis); 
      fis.flush(); 
      fis.close(); 
      bitmapScreen.recycle(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
}, 5000); 

new Handler().postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     Log.i("TAG", Thread.currentThread().getName()); 
     try { 
      Bitmap bitmapScreen = getBitmapOfScreen(); 
      //File f = new File("/storage/sdcard1", "tmp2.jpg"); //Huawei 
      File f = new File("/storage/emulated/0", "tmp2.jpg"); //Nexus 5 
      FileOutputStream fis = new FileOutputStream(f); 
      bitmapScreen.compress(Bitmap.CompressFormat.JPEG, 100, fis); 
      fis.flush(); 
      fis.close(); 
      bitmapScreen.recycle(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
}, 10000); 

/** Must be called in UI thread.*/ 
public Bitmap getBitmapOfScreen() throws Exception { 

    final View v1 = getWindow().getDecorView().getRootView(); 
    v1.destroyDrawingCache(); 
    v1.setDrawingCacheEnabled(true); 

    v1.buildDrawingCache(); 
    Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
    bitmap.setHasAlpha(true);   //important for PNG 
    v1.setDrawingCacheEnabled(false); 
    return bitmap; 
} 
+0

게시물 getBitmapOfScreen() 메소드 –

+0

있습니다. 아래로 스크롤. – t0m

+0

getdrawingcache에서 비트 맵을 만들기 전에 v1.buildDrawingCache()를 추가하십시오. –

답변

0

해결.

실종 표시는 invalidate()입니다. 지금은 invalidate()TextClock으로 호출하기 전에 getBitmapOfScreen() 호출 전에 비트 맵에서 값이 정확합니다.