从onCreate中的TextLayout获取位图

时间:2017-03-24 18:08:40

标签: android android-layout bitmap imageview android-bitmap

我能够将LayoutTextView转换为Bitmap,只要事件发生在 onCreate()之后。但是当我在创建期间尝试时,它不起作用。

有没有办法让它发挥作用?我尝试过以各种方式使用inflate()而没有任何快乐。

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

Bitmap null during onCreate() but works later

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>

2 个答案:

答案 0 :(得分:1)

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();
        }
    });
}

答案 1 :(得分:0)

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();
        }
    });