Android,更改视图并截屏

时间:2014-11-30 15:59:15

标签: java android android-layout screenshot android-view

我想将视图的可见性从GONE更改为VISIBLE,并获取整个应用程序的屏幕截图并将其保存在文件中。

我可以截取屏幕截图,但jpeg文件中的视图仍然不可见(GONE)。从按钮单击调用该方法:

public void onClick(View v) {
    View screen = (View) findViewById(R.id.mainLayout);    

    //I change this from View.GONE
    TextView hiddenText = (TextView) findViewById(R.id.hiddenText);
    hiddenText.setVisibility(View.VISIBLE);
    hiddenText.setText( "WEEEEEEEEEE" + (int)(Math.random()*10) );

    View rootview = screen.getRootView();
    //View rootview = getWindow().getDecorView().getRootView();
    rootview.invalidate();  //I try to redraw the view without success
    rootview.requestLayout();
    rootview.setDrawingCacheEnabled(true);
    Bitmap bitmap = rootview.getDrawingCache();                 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);                                                 
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + 
              File.separator + "Facebook" + File.separator + "my_screenshot.jpeg";
    File file = new File(path);
    file.getParentFile().mkdirs(); //if the folder doesn't exists it's created

    try {
        boolean asd = file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        ostream.write(bytes.toByteArray()); 
        ostream.close();
    }
    catch (Exception e){
        e.printStackTrace();
    }

    //hiddenText.setVisibility(View.GONE);                      
}

之后我保存文件我应该再次隐藏TextView hiddenText,但我不知道在哪个点设置可见性GONE

0 个答案:

没有答案