在 onCreate 中从 TextLayout 获取位图

Getting Bitmap from TextLayout in onCreate

我可以将 LayoutTextView 转换成 Bitmap,只要事件发生 之后 onCreate()。但是当我在 during 创建时尝试,它不起作用。

有没有办法让它工作?我尝试过以各种方式使用 inflate() 但并不愉快。

有各种已回答的问题,包括 this one 解决了我在我的代码中成功使用的技术。但成功不包括采用 onCreate().

中的技术

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    private RelativeLayout mTextLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // This is what I would like to work
        replaceTheRobot(); // Fails.  Bitmap is null.
    }

    public void onClickGetBitmap(View view) {
        replaceTheRobot(); // Works.  Bitmap replaces the robot.
    }

    private void replaceTheRobot() {
        mTextLayout = (RelativeLayout) findViewById(sampleRelativeLayout);
        mTextLayout.setDrawingCacheEnabled(true);
        Bitmap bitmap = mTextLayout.getDrawingCache();
        if (bitmap == null) {
            Toast.makeText(getApplicationContext(), "The bitmap was null", Toast.LENGTH_LONG).show();
            Log.v(TAG, "The bitmap was null");
        }
        else {
            Toast.makeText(getApplicationContext(), "The bitmap size: " + bitmap.getWidth() +", " + bitmap.getHeight(), Toast.LENGTH_LONG).show();
            ImageView imageViewRobot = (ImageView) findViewById(R.id.imageViewRobot);
            imageViewRobot.setImageDrawable(new BitmapDrawable(this.getResources(), bitmap));
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <RelativeLayout
        android:id="@+id/sampleRelativeLayout"
        android:layout_width="50dp"
        android:layout_height="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:id="@+id/helloTextView" />
    </RelativeLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/imageViewRobot"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:text="Get Bitmap"
        android:onClick="onClickGetBitmap"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/button" />
</RelativeLayout>

View 尚未呈现。您可以使用 view.getViewTreeObserver().addOnGlobalLayoutListener 在布局准备就绪时收到通知。

所以,像这样:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ViewTreeObserver viewTreeObserver = findViewById(sampleRelativeLayout).getViewTreeObserver();
    viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
        viewTreeObserver.removeOnGlobalLayoutListener(this);
        replaceTheRobot();
        }
    });
}

基于 user1779222 答案,如果您希望管理更早的 Android 版本,您可以测试 运行 非常相似的名称 removeGlobalOnLayoutListener 而不是 removeOnGlobalLayoutListener.另外,为了避免错误 java.lang.IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again,您可以重新获取观察者,而不是使用原来的观察者。进行这些更改后,结果如下:

    final ViewTreeObserver viewTreeObserver = findViewById(sampleRelativeLayout).getViewTreeObserver();
    viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                findViewById(sampleRelativeLayout).getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } else {
                findViewById(sampleRelativeLayout).getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
            replaceTheRobot();
        }
    });