繁体   English   中英

在 Android 中捕获布局屏幕

[英]Capture layout screen in Android

我正面临一个问题。 我想捕获 XML 布局的屏幕。 我知道这个问题有很多话题。 我把它们都读了。 但是他们中没有人帮助我。 注意:我想获取不属于当前活动的布局的屏幕截图

这是我的函数,它假设捕获 Y 布局(在 X 活动上调用)

fun getTestCapture() : Bitmap{
    val view = layoutInflater.inflate(R.layout.activity_test, null)
    val layout = view.findViewById<LinearLayout>(R.id.linearLayout)

    layout.layout(0,0, layout.measuredWidth, layout.measuredHeight)
    val bitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(bitmap)
    val background = layout.background

    if (background != null) {
        background.draw(canvas)
    }

    layout.draw(canvas)

    storeScreenShot(bitmap, "test.jpg", this)

    return bitmap

}

当我保存位图时,它什么也不显示。 我的保存位图功能工作正常,我不想打扰你。 所以我的问题很简单:是什么导致我的功能不起作用?

尝试以下解决方案

爪哇

yourView.post(new Runnable() {
    @Override
    public void run() {
        //take screenshot
        Bitmap myBitmap = captureScreen(binding.llMain);
        Log.e(TAG, "run :: Screenshot captured..!");
        try {
            if (myBitmap != null) {
                //your save image code
                Log.e(TAG, "run :: Screenshot saved..!");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});

private Bitmap captureScreen(View v) {
    Bitmap screenshot = null;
    try {
        if (v != null) {
            screenshot = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(screenshot);
            v.draw(canvas);
        }
    } catch (Exception e) {
        Log.e(TAG, "Failed to capture screenshot because:" + e.getMessage());
    }
    return screenshot;
}

科特林

binding.idLayout.post(Runnable {
    val myBitmap = getScreenShotFromView(binding.idLayout)
    try {
        if (myBitmap != null) {
            storeScreenShot(myBitmap, "test2.jpg", this@TelecollecteActivity)
        }
    } catch (e: java.lang.Exception) {
        Log.e("BLABLA", "Error ::" + e.message)
    }
})

fun getScreenShotFromView(v: View): Bitmap? {
    var screenshot: Bitmap? = null
    try {
        if (v != null) {
            screenshot = Bitmap.createBitmap(v.measuredWidth, v.measuredHeight, Bitmap.Config.ARGB_8888)
            val canvas = Canvas(screenshot)
            v.draw(canvas)
        }
    } catch (e: Exception) {
        Log.e("BLABLA", "Failed to capture screenshot because:" + e.message)
    }
    return screenshot
}

供你参考

我还添加了经过测试的完整代码,它对我来说运行良好。

activity_main_two.xml

如果您使用黑色,则需要添加背景颜色为白色,其他副完整图像显示为黑色。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/llMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/White"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".Main2Activity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hardik Talaviya" />

</LinearLayout>

主活动

class Main2Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_two)

        llMain.post(Runnable {
            val myBitmap = getScreenShotFromView(llMain)
            try {
                if (myBitmap != null) {
                    //storeScreenShot(myBitmap, "test2.jpg", this@TelecollecteActivity)
                    val extr = Environment.getExternalStorageDirectory().toString()
                    val myPath = File(extr, "test.jpg")
                    var fos: FileOutputStream? = null

                    try {
                        fos = FileOutputStream(myPath)
                        myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)
                        fos!!.flush()
                        fos!!.close()
                        MediaStore.Images.Media.insertImage(contentResolver, myBitmap,
                                "Screen", "screen")

                    } catch (e: FileNotFoundException) {
                        // TODO Auto-generated catch block
                        e.printStackTrace()
                    } catch (e: Exception) {
                        // TODO Auto-generated catch block
                        e.printStackTrace()
                    }

                }
            } catch (e: java.lang.Exception) {
                Log.e("BLABLA", "Error ::" + e.message)
            }
        })
    }

    fun getScreenShotFromView(v: View): Bitmap? {
        var screenshot: Bitmap? = null
        try {
            if (v != null) {
                screenshot = Bitmap.createBitmap(v.measuredWidth, v.measuredHeight, Bitmap.Config.ARGB_8888)
                val canvas = Canvas(screenshot)
                v.draw(canvas)
            }
        } catch (e: Exception) {
            Log.e("BLABLA", "Failed to capture screenshot because:" + e.message)
        }
        return screenshot
    }
}

保存的屏幕截图图像

图片链接

我希望这可以帮助你!

您需要以下代码

layout.buildDrawingCache(true);
bitmap = layout.getDrawingCache(true).copy(Config.ARGB_8888, false);
layout.destroyDrawingCache();

这会将视图捕获到位图中,并且该位图可以存储在文件中。

所以这就是我所做的,但我仍然有一个空的形象。

绑定.idLayout

是我的带有内容的 LinearLayout,我用数据绑定检索

run { Log.v("BLABLA", "jifjifoer")
            val myBitmap = getScreenShotFromView(binding.idLayout)
            try {
                if (myBitmap != null){
                    storeScreenShot(myBitmap, "test2.jpg", this@TelecollecteActivity)
                }
            }catch (e : java.lang.Exception){} }

获取屏幕功能

fun getScreenShotFromView(v: View) : Bitmap? {
    var screenshot : Bitmap? = null;
    try {
        if (v != null) {
            screenshot = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
            val canvas = Canvas(screenshot);
            v.draw(canvas);
        }
    } catch (e : Exception) {
        Log.e("BLABLA", "Failed to capture screenshot because:" + e.message);
    }
    return screenshot
}

编辑

view.post(object : Runnable{
            override fun run() {
                Log.v("BLABLA", "jifjifoer")
                val myBitmap = getScreenShotFromView(binding.parent)
                try {
                    if (myBitmap != null){
                        storeScreenShot(myBitmap, "test2.jpg", this@TelecollecteActivity)
                    }
                }catch (e : java.lang.Exception){}
            }
        })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM