Конвертировать FrameLayout в изображение - PullRequest
0 голосов
/ 14 апреля 2020

У меня есть FrameLayout, который имеет ImageView в качестве дочернего элемента. Вдобавок к этому ImageView, я позволил пользователю создавать некоторые тексты с помощью TextView. Работает нормально. Но теперь я хотел отправить FrameLayout с ImageView (и далее динамически добавленным TextViews) как целое изображение.

Как я могу это сделать?

Вот мой XML макет:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <FrameLayout
        android:id="@+id/image_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".FirstFragment">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:contentDescription="@string/imageview_holding_the_image" />
    </FrameLayout>
</layout>

А вот код для создания простого TextView во время выполнения и размещения он поверх FrameLayout ImageView, так что исходное изображение «расширяется» на TextView:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = FragmentFirstBinding.inflate(inflater, container, false )

        // put the image into the ImageView via Glide
        Glide.with(requireActivity()).load(R.drawable.my_cup).into(binding.image)

        // create a TextView
        textView = TextView(requireActivity())

        // specify some attributes & tell how it should be laid out 
        textView.apply{
            text = context.getString(R.string.textOnImageView)
            textSize = 24.0f
            id = R.id.myTextView
            layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER)

        }

        // add the TextView to the FrameLayout
        binding.imageContainer.addView(textView)

        // Inflate the layout for this fragment
        return binding.root
    }

Теперь, когда я хочу создать это «расширенное» изображение (оригинал image + TextView), что мне нужно сделать?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...