Android TextView.setText() + getDrawingCache(), 文字在位图中不可见
Android TextView.setText() + getDrawingCache(), text is not visible in Bitmap
我对 TextView
的 getDrawingCache()
和 setText()
有疑问。我在 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);
位图具有正确的分辨率,正确的 mainLinearLayout 背景(以编程方式设置,不是在此示例中),但我在位图中看不到来自 idOfTextView.setText( "Some special test string" )
的文本:"Some special test string"。
有趣的是:当我将 android:text="Test string"
添加到 idOfTextView 时,部分文本 ("Some") 显示在位图中。
当我在代码中注释 setText()
并将 android:text="Test string"
添加到 idOfTextView 时,一切正常。
我试过:编辑布局、使无效、设置可见性、在 Google 上搜索,但我不知道,下一步是什么。
谢谢。
灵感来自 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);
}
});
我对 TextView
的 getDrawingCache()
和 setText()
有疑问。我在 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);
位图具有正确的分辨率,正确的 mainLinearLayout 背景(以编程方式设置,不是在此示例中),但我在位图中看不到来自 idOfTextView.setText( "Some special test string" )
的文本:"Some special test string"。
有趣的是:当我将 android:text="Test string"
添加到 idOfTextView 时,部分文本 ("Some") 显示在位图中。
当我在代码中注释 setText()
并将 android:text="Test string"
添加到 idOfTextView 时,一切正常。
我试过:编辑布局、使无效、设置可见性、在 Google 上搜索,但我不知道,下一步是什么。
谢谢。
灵感来自 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);
}
});