我能够转换Layout
with a TextView
into a Bitmap
,只要事件发生after onCreate()
。但当我尝试时during在创建时,它不起作用。
有办法让它发挥作用吗?我尝试过使用inflate()
以各种方式,没有喜悦。
有各种已回答的问题,包括this one https://stackoverflow.com/questions/4715044/android-how-to-convert-whole-imageview-to-bitmap这解决了我在代码中成功使用的技术。但成功并不包括采用来自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();
}
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)