我需要将视图转换为位图以预览我的视图并将其保存为图像.我尝试使用以下代码,但它创建了一个空白图像.我无法理解我犯了什么错误.
View viewToBeConverted; Bitmap viewBitmap = Bitmap.createBitmap(viewToBeConverted.getWidth(), viewToBeConverted.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(viewBitmap);
viewToBeConverted.draw(canvas);
savephoto(“f1”, viewBitmap);
//// public void savephoto(String filename,Bitmap bit)
{
File newFile = new File(Environment.getExternalStorageDirectory() + Picture_Card/"+ filename+ ".PNG");
try
{
newFile.createNewFile();
try
{
FileOutputStream pdfFile = new FileOutputStream(newFile); Bitmap bm = bit; ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG,100, baos); byte[] bytes = baos.toByteArray();
pdfFile.write(bytes);
pdfFile.close();
}
catch (FileNotFoundException e)
{ //
}
} catch (IOException e)
{ //
}
}
Run Code Online (Sandbox Code Playgroud) 我知道可以使用自定义视图而不是 Google 地图中的基本地图标记来执行此答案中所述的操作。我很好奇是否可以通过 Compose 达到类似的效果?
由于ComposeView是最后一堂课,无法直接扩展它,所以我正在考虑有一个FrameLayout可以将其作为子项添加的课程。尽管这似乎会导致竞争条件,因为可组合项的绘制与普通 android 略有不同View。
class MapMarkerView : FrameLayout {
constructor(context: Context) : super(context) {
initView()
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
initView()
}
private fun initView() {
val composeView = ComposeView(context).apply {
layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
}
composeView.setContent {
// this runs asynchronously making the the bitmap generation not include composable at runtime?
Text(text = "2345", modifier = Modifier.background(Color.Green, RoundedCornerShape(4.dp)))
}
addView(composeView)
}
}
Run Code Online (Sandbox Code Playgroud)
我读过的大多数文章都有一些用于生成这样的位图的回调函数 …