inflate了一个View,填充完内容之后,并不打算把它显示在屏幕上只想获得它的截图然后作为imageview显示在另外的地方我用了下面的代码LayoutInflater mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout mIconLayout = (FrameLayout)mInflater.inflate(R.layout.forlder_icon_creator, null, false);
//之后对layout的一些内容进行了填充
mIconLayout.setDrawingCacheEnabled(true);
mIconLayout.buildDrawingCache();
Bitmap bitmap = mIconLayout.getDrawingCache();
但是获得的bitmap是null,求问正确的操作步骤。。

解决方案 »

  1.   


    public void saveScreen(View parent) {
    boolean flag = false;
    if (Environment.MEDIA_MOUNTED.equals(Environment
    .getExternalStorageState())) {// sdcard已装载
    FileOutputStream fos = null;
    Bitmap temp = null;
    try {
    parent.setDrawingCacheEnabled(true);
    parent.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    temp = parent.getDrawingCache(true);
    String prefix = Util.instance().format(new java.util.Date());
    if (Settings.getSSDir() != null) {
    String filepath = Settings.getSSDir() + "/" + prefix
    + ".jpg";
    File file = new File(filepath);
    if (!file.exists()) {
    file.createNewFile();
    }
    fos = new FileOutputStream(file);
    if (temp != null) {
    flag = temp.compress(CompressFormat.JPEG, 90, fos);
    fos.flush();
    }
    }
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if (fos != null) {
    fos.close();
    fos = null;
    }
    if (temp != null && flag) {
    parent.setDrawingCacheEnabled(false);// 这里必须设置为false,否则回收temp后会产生
    // 使用回收图片错误
    temp.recycle();
    temp = null;
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }传入你inflate得到的View,试试看能不能行
      

  2.   

    谢谢了,我用canvas+bitmap画了一个。