在onCreate中从TextLayout获取位图 [英] Getting Bitmap from TextLayout in onCreate

查看:73
本文介绍了在onCreate中从TextLayout获取位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只要在之后 onCreate()之后发生的事件,我就能将带有TextViewLayout转换为Bitmap.但是当我尝试在创建过程中期间时,它不起作用.

I am able to convert a Layout with a TextView into a Bitmap, as long as the event that happens after onCreate(). But when I try during on create, it does not work.

是否有办法使它正常工作?我尝试了各种不同的方式使用inflate()的方法,但都没有乐趣.

Is there a way to get this to work? I have tried using inflate() in various ways with no joy.

回答了各种问题,包括这一个解决我在代码中成功使用的技术.但是成功并不包括采用onCreate()的技术.

There are various answered questions including this one that address the technique that I'm successfully using in my code. But the success does not include employing the technique from 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在布局准备就绪时得到通知.

View is not rendered yet. You can use view.getViewTreeObserver().addOnGlobalLayoutListener to get notified when your layout is ready.

所以,像这样:

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

这篇关于在onCreate中从TextLayout获取位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆