[英]Create Bitmap of a layout after seting text in some textview parameters dynamically
What I have- I have a frameLayout in xml which contains some TextViews.我所拥有的 - 我在 xml 中有一个 frameLayout,其中包含一些 TextView。 I am adding text in some of textview fields in my java code(say MainActivity) whereas text in some of textviews are hardcoded in XML file only.
我在我的 java 代码(比如 MainActivity)中的一些 textview 字段中添加文本,而某些文本视图中的文本仅在 XML 文件中硬编码。 In my XML file(abc.xml)-
在我的 XML 文件(abc.xml)中-
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:text="Agent name: "
android:textSize="4dp" />
<TextView
android:id="@+id/agentName"
android:layout_marginTop="1dp"
android:textSize="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
In my mainactivity i am seting agentName as- TextView tvAgentName = findViewById(R.id.agentName);
在我的主要活动中,我将
TextView tvAgentName = findViewById(R.id.agentName);
tvAgentName.setText("My first agent");
What I want- Create a bitmap of the layout having both textviews with texts as-我想要什么 - 创建一个 bitmap 的布局,其中包含两个带有文本的文本视图 -
Agent name: My first agent
What I am getting- A bitmap having texts as-我得到的是-一个 bitmap 的文本为-
Agent name:
Note- I am creating bitmap from layout using below function-注意-我正在使用以下功能从布局创建 bitmap-
View inflatedFrame = getLayoutInflater().inflate(R.layout.abc, null);
Log.d("INFLAMTE", "onActivityResult: "+inflatedFrame);
frameLayout = inflatedFrame.findViewById(R.id.screen) ;
Log.d("FRAME LAYOUT IS ", "onActivityResult: "+frameLayout);
frameLayout.setDrawingCacheEnabled(true);
frameLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
frameLayout.layout(0, 0, frameLayout.getMeasuredWidth(), frameLayout.getMeasuredHeight());
frameLayout.buildDrawingCache(true);
return frameLayout.getDrawingCache();
}
Thanks in advance.
public Drawable createFromView(int positionNumber) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.drawable.pin_icon, null, false);
TextView tv = (TextView)
view.findViewById(R.id.pin_background);
tv.setText(" " + (positionNumber + 1));
tv.setDrawingCacheEnabled(true);
tv.layout(0, 0, 50, 50);
tv.buildDrawingCache();
Bitmap b = Bitmap.createBitmap(tv.getDrawingCache());
tv.setDrawingCacheEnabled(false);
Drawable d = new BitmapDrawable(b);
return d;
}
//Or convert view into bitmap
private Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
} else {
//does not have background drawable, then draw white
background on the canvas
canvas.drawColor(Color.WHITE);
}
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.