Getting Bitmap from TextLayout in onCreate(在 onCreate 中从 TextLayout 获取位图)
问题描述
只要事件发生在 after onCreate()
.但是当我尝试期间创建时,它不起作用.
有没有办法让它工作?我尝试过以各种方式使用 inflate()
,但没有任何乐趣.
有各种已回答的问题,包括
公共类 MainActivity 扩展 AppCompatActivity {私有静态最终字符串 TAG = MainActivity.class.getSimpleName();私有RelativeLayout mTextLayout;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//这就是我想要的工作替换机器人();//失败.位图为空.}公共无效 onClickGetBitmap(查看视图){替换机器人();//工作.位图代替机器人.}私人无效替换机器人(){mTextLayout = (RelativeLayout) findViewById(sampleRelativeLayout);mTextLayout.setDrawingCacheEnabled(true);位图位图 = mTextLayout.getDrawingCache();如果(位图==空){Toast.makeText(getApplicationContext(), "位图为空", Toast.LENGTH_LONG).show();Log.v(TAG, "位图为空");}别的 {Toast.makeText(getApplicationContext(), "位图大小:" + 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"工具:context=".MainActivity"><相对布局android:id="@+id/sampleRelativeLayout"android:layout_width="50dp"android:layout_height="50dp"><文本视图android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="你好世界!"android:id="@+id/helloTextView"/></相对布局><图像视图android:layout_width="wrap_content"android:layout_height="wrap_content"应用程序:srcCompat="@mipmap/ic_launcher"android:id="@+id/imageViewRobot"android:layout_centerVertical="true"android:layout_centerHorizontal="true"/><按钮android:text="获取位图"android:onClick="onClickGetBitmap"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:id="@+id/按钮"/></相对布局>
View
尚未渲染.您可以使用 view.getViewTreeObserver().addOnGlobalLayoutListener
在布局准备就绪时获得通知.
所以,是这样的:
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);最终 ViewTreeObserver viewTreeObserver = findViewById(sampleRelativeLayout).getViewTreeObserver();viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@覆盖公共无效 onGlobalLayout() {viewTreeObserver.removeOnGlobalLayoutListener(this);替换机器人();}});}
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.
Is there a way to get this to work? I have tried using inflate()
in various ways with no joy.
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
is not rendered yet. You can use view.getViewTreeObserver().addOnGlobalLayoutListener
to get notified when your layout is ready.
So, something like this:
@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 获取位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 onCreate 中从 TextLayout 获取位图


基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01