【问题标题】:Save layout as an image将布局另存为图像
【发布时间】:2014-09-16 08:23:53
【问题描述】:

我有一个 LinearLayout,我想将该视图的内容保存为图像。我已经完成了一半。

File imageFile;
// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/a.png";   

// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
Bitmap bitmap;
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
    fout = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.PNG, 10, fout);
    fout.flush();
    fout.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

使用上面的代码,它将保存视图的副本,但保存在设备屏幕上看到的内容。我有更多关于我想要保存的视图的信息。上面来自herethis 的代码只会将屏幕上看到的信息保存为图像。我希望保存所有信息,即使是不在屏幕上的信息(您需要滚动查看)。

我怎样才能做到这一点?

【问题讨论】:

  • "你需要滚动才能看到的地方" -- 滚动什么? ListView? GridView? ScrollView? WebView? MapView?还有什么?
  • 查看其余信息。它是一个线性布局内的表格视图
  • 您不能滚动“线性布局内的表格视图”。然而你声称你缺少的是“你需要滚动才能看到的地方”。
  • 你可以将 clipChildren 设置为 false 到父视图
  • @commonsware,有一个列表视图填充该表视图(我的消息由于某种原因被切断)。

标签: android android-linearlayout


【解决方案1】:

另一种选择是使用:

Bitmap b = Bitmap.createBitmap(width, height....)
v1.setLayoutParams // Full width and height of content
Canvas c = new Canvas(b);
v1.draw(c); // You now have full bitmap
saveBitmap(b);

【讨论】:

    【解决方案2】:

    在其上运行测量/布局传递并将其绘制到画布上。假设你的父母被称为“视图”并且是一个垂直的线性布局:

    view.measure(someWidth, MeasureSpec.UNSPECIFIED);
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    
    Canvas c = new Canvas(bitmap);
    
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.draw(c);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-20
      • 2016-08-28
      • 2017-12-26
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      • 2021-09-11
      相关资源
      最近更新 更多