Шаги:
- Создайте собственное представление программно
< li>Создайте растровое изображение представления. - Создайте URI растрового изображения.
- Поделитесь URI в социальных сетях.
Примечание: все сработало отлично, когда я попытался сделать представление видимым для
пользователя (путем добавления пользовательского представления в макет действий), а затем продолжил шаг 2.
< /blockquote>


Макет представления view_profile_summary.xml:
Код: Выделить всё
...
...
...
Наложение внешнего вида фигуры:
Код: Выделить всё
rounded
30dp
30dp
0dp
0dp
Пользовательское представление ProfileSummaryView:
Код: Выделить всё
class ProfileSummaryView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {
val binding = ViewProfileSummaryBinding.inflate(LayoutInflater.from(context), this, false)
}
Создайте представление и загрузите изображение с помощью Glide: (Действие)
Код: Выделить всё
runOnUiThread {
val view = ProfileSummaryView(this)
val imageLayoutParams = view.binding.profileSummaryImageView.layoutParams
val imageWidth = imageLayoutParams.width
val imageHeight = imageLayoutParams.height
Glide.with(this@MyActivity)
.asBitmap()
.override(imageWidth, imageHeight)
.dontAnimate()
.load(url)
.into(object : BitmapImageViewTarget(view.binding.profileSummaryImageView) {
override fun onResourceReady(resource: Bitmap, transition: Transition?) {
super.onResourceReady(resource, transition)
createUriAndShare(view)
}
})
}
Создание растрового изображения и URI: (Действие) – Вероятно, это так не имеет отношения к этой части
Код: Выделить всё
private fun createUriAndShare(view: ProfileSummaryView) {
CoroutineScope(Dispatchers.Main).launch {
delay(1000L)
val layoutParams = view.binding.root.layoutParams
val width = layoutParams.width
val height = layoutParams.height
view.measure(
View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)
)
view.layout(0, 0, width, height)
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
view.draw(canvas)
bitmap?.let {
val stickerUri = saveBitmapAndGetUri(it)
shareImage(stickerUri)
}
}
}
private fun saveBitmapAndGetUri(bitmap: Bitmap): Uri? {
try {
val cachePath = File(cacheDir, "images")
cachePath.mkdirs()
val file = File(cachePath, "image.jpg")
val fileOutputStream = FileOutputStream(file)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream)
fileOutputStream.flush()
fileOutputStream.close()
return FileProvider.getUriForFile(this, "$packageName.provider", file)
} catch (e: IOException) {
e.printStackTrace()
}
return null
}
private fun shareImage(imageUri: Uri?) {
if (imageUri == null) {
return
}
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.setType("image/*")
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri)
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(Intent.createChooser(shareIntent, "Share your profile"))
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... hich-is-no