我所拥有的-我在xml中有一个frameLayout,其中包含一些TextViews。我在Java代码中的某些textview字段中添加文本(例如MainActivity),而某些textview中的文本仅硬编码在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>
在我的主要活动中,我将agentName设置为-
TextView tvAgentName = findViewById(R.id.agentName);
tvAgentName.setText("My first agent");
我想要的是-创建具有两个textview和文本as的布局的位图-
Agent name: My first agent
我得到的是-带有文本的位图-
Agent name:
注意-我正在使用以下功能从布局创建位图-
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;
}