gpt4 book ai didi

android - 如何将 View 转换为 Drawable

转载 作者:搜寻专家 更新时间:2023-11-01 08:10:17 31 4
gpt4 key购买 nike

我想将 Android View 转换为 Drawable。

我试试下面的方法:

View view1;

view1 = getLayoutInflater().inflate(R.layout.layout1, null);
view1.setDrawingCacheEnabled(true);
((TextView)(view1.findViewById(R.id.txt_number))).setText("98");

view1.setLayoutParams(new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, getPoint(23.01, 72.55), MapView.LayoutParams.BOTTOM_CENTER));
mapView.addView(overlayPin1);

Log.d("view", "000");
if(view1!=null)
{
Log.d("view", "111");
System.out.println("view is not null.....");
view1.buildDrawingCache();
Bitmap bm = view1.getDrawingCache();
Log.d("view", "222");
try
{
Log.d("view", "333");
//if(bm!=null)
//{
Log.d("view", "444");
String dir = Environment.getExternalStorageDirectory().toString();
System.out.println("bm is not null.....");
OutputStream fos = null;
File file = new File(dir,"sample.JPEG");
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bm.compress(Bitmap.CompressFormat.JPEG, 50, bos);
bos.flush();
bos.close();
Log.d("view", "555");
//}
}
catch(Exception e)
{
Log.d("view", "666");
System.out.println("Error="+e);
e.printStackTrace();
}
}

但我总是得到空位图。

最佳答案

当您的View 完全加载/完成加载时,

view.buildDrawingCache();

获取Drawable的使用,

Drawable drawable = new BitmapDrawable(view.getDrawingCache());

关于android - 如何将 View 转换为 Drawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10413049/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com