java - 在一些 TextView 参数中动态设置文本后创建布局的位图

标签 java android layout bitmap

我所拥有的 - 我有一个 xml 格式的框架布局,其中包含一些 TextView。我在 java 代码(例如 MainActivity)的某些 TextView 字段中添加文本,而某些 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>

在我的主要 Activity 中,我将 agentName 设置为- TextView tvAgentName = findViewById(R.id.agentName);

                tvAgentName.setText("My first agent");

我想要什么 - 创建一个布局的位图,其中两个 TextView 的文本为 -

                      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;
}

关于java - 在一些 TextView 参数中动态设置文本后创建布局的位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58558044/

相关文章:

html - 塌陷时不均匀的 Bootstrap 药丸

java - 在android中裁剪后保存图像

java - 用于 ssm 的 aws java sdk 给出 java.lang.NoSuchFieldError : SIGNING_REGION

android - 在 Android Studio 3.0.1 中使用没有背景颜色或 xml 的图像 Assets 创建启动器图标

html - 为什么我的布局只在 Firefox 和 IE8(不是 9)中损坏?

java - 想知道如何通过双击和拉伸(stretch)来缩放图像

Java:为什么 BorderLayout 调用 setSize() 后调用 setBounds()?

java - 下载 ubuntu 14.04 中的所有软件包后无法初始化 opennms 数据库 postgres

php - 将数组传递给 BasicNameValuePair?

Android - 如何知道设备是否只有 WiFi?