2017-01-23 8 views
2

У меня проблема с getDrawingCache() и setText() из TextView. Я не вижу текст в Bitmap после setText() и использовал getDrawingCache().Android TextView.setText() + getDrawingCache(), текст не отображается в битовой карте

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/mainLinearLayout" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolBar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/transparencyGray" 
     app:contentInsetEnd="0dp" 
     app:contentInsetStart="0dp" > 
    </android.support.v7.widget.Toolbar> 


    <TextView 
     android:id="@+id/idOfTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/white" 
     android:shadowColor="@color/black" 
     android:shadowDx="0" 
     android:shadowDy="0" 
     android:shadowRadius="5" 
     android:layout_gravity="center" 
     android:textSize="@dimen/textSizeLarge" 
    /> 

</LinearLayout> 

Кодекс:

idOfTextView.setText("Some special test string"); 
// create bitmap screen capture 
View v1 = getWindow().getDecorView().getRootView(); 
//v1.invalidate(); 
v1.setDrawingCacheEnabled(true); 
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
v1.setDrawingCacheEnabled(false); 

Bitmap имеет правильное разрешение, правильный фон mainLinearLayout (набор программно, а не в этом примере), но я не вижу текст: «Некоторые специальные тестовую строку» из idOfTextView.setText("Some special test string") в растровом изображении.

Интересно, когда я добавляю android:text="Test string" в idOfTextView, часть текста («Некоторые») показана в растровом изображении.

Когда я комментирую setText() в коде и добавляю android:text="Test string" в idOfTextView, все в порядке.

Я попытался: отредактировать макет, аннулировать, установитьVisibility, искать в Google, но я не знаю, что дальше.

спасибо.

ответ

-1

Вдохновленный here.
Решение:

idOfTextView.setText("Some special test string"); 
final View v1 = getWindow().getDecorView().getRootView(); 
final ViewTreeObserver vto = affirmation.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     v1.setDrawingCacheEnabled(true); 
     Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
     v1.setDrawingCacheEnabled(false); 
    } 
}); 
+0

Пожалуйста, почему вниз? – t0m

 Смежные вопросы

  • Нет связанных вопросов^_^