【问题标题】:Android Convert current screen to bitmapAndroid 将当前屏幕转换为位图
【发布时间】:2014-04-25 13:24:34
【问题描述】:

我想转换我的应用程序的屏幕并将其转换为位图。我已经知道如何将视图转换为位图,但这不是我想要的,因为我的屏幕由许多片段组成。因此,我希望能够获取屏幕并将其转换为位图...... 非常感谢您的帮助。

【问题讨论】:

标签: android screen android-imageview android-bitmap


【解决方案1】:

您可以使用类似的东西并将布局作为视图传递;)

public Bitmap takeScreenShot(View view) {
        // configuramos para que la view almacene la cache en una imagen
        view.setDrawingCacheEnabled(true);
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
        view.buildDrawingCache();

        if(view.getDrawingCache() == null) return null; // Verificamos antes de que no sea null

        // utilizamos esa cache, para crear el bitmap que tendra la imagen de la view actual
        Bitmap snapshot = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);
        view.destroyDrawingCache();

        return snapshot;
    }

【讨论】:

  • 好的,获取绘图缓存有什么作用?我想捕获屏幕而不仅仅是一个视图。我的屏幕是碎片化的,有很多视图分布在xml布局文件中,合并到一个视图中
  • 如果您传递布局根目录或主目录,它会对屏幕上的内容进行快照;)
  • 获取绘图缓存有什么作用?你需要阅读这个getDrawingCache
【解决方案2】:

查看是你的根布局:-

View v = view.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString();
File myPath = new File(extr, getString(R.string.free_tiket)+".jpg");
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(myPath);
    b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
    MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

查看链接了解更多信息:-

http://code.google.com/p/android-screenshot-library/wiki/DeveloperGuide

Android - How to take screenshot programatically

【讨论】:

  • 也许你没有阅读我上面的评论。我不想实际截屏。只需将屏幕转换为位图并将其设置为 ImageView 的ImageBitmap
【解决方案3】:
View rootView = findViewById(R.id.relative_facebook).getRootView();
        rootView.setDrawingCacheEnabled(true);
        return rootView.getDrawingCache();

【讨论】:

    猜你喜欢
    • 2019-12-03
    • 2012-01-16
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多