Создание растрового изображения макета после динамической установки текста в некоторых параметрах textview - PullRequest
0 голосов
/ 25 октября 2019

Что у меня есть - у меня есть frameLayout в xml, который содержит некоторые TextViews. Я добавляю текст в некоторые поля textview в моем коде Java (скажем, MainActivity), тогда как текст в некоторых текстовых представлениях жестко запрограммирован только в файле 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");

Что я хочу - создать растровое изображение макетас обоими текстовыми представлениями с текстом as-

                      Agent name: My first agent

Что я получаю - Растровое изображение с текстом as-

                      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.

1 Ответ

0 голосов
/ 25 октября 2019
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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...