gpt4 book ai didi

Android TextView.setText() + getDrawingCache(),文字在Bitmap中不可见

转载 作者:行者123 更新时间:2023-11-29 14:13:12 31 4
gpt4 key购买 nike

我对 TextViewgetDrawingCache()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 背景(以编程方式设置,不是在这个例子中),但我没有看到文本:"Some special test string"from idOfTextView.setText( "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);
}
});

关于Android TextView.setText() + getDrawingCache(),文字在Bitmap中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41809918/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com