如何将视图转换为Drawable?

时间:2011-04-25 08:38:46

标签: android

我有一个View我希望将其转换为图像以便将其存储在某个地方。 但是,如何将此View转换为图像?

2 个答案:

答案 0 :(得分:9)

尝试使用此视图拍摄视图并存储在SD卡中..

View view = TextView.getRootView();
//You can use any view of your View instead of TextView

if (view != null)
{
    System.out.println("view is not null.....");
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bm = view.getDrawingCache();

    try
    {
        if (bm != null)
        {
            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();
        }
    }
    catch(Exception e)
    {
        System.out.println("Error="+e);
        e.printStackTrace();
    }
}

答案 1 :(得分:5)

  1. 在视图上启用绘图缓存:

    view.setDrawingCacheEnabled(true);
    
  2. 从缓存中创建一个位图:

    bitmap = Bitmap.createBitmap(view.getDrawingCache());
    
  3. 将位图保存在任何地方......

  4. 禁用绘图缓存:

    view.setDrawingCacheEnabled(false);