在Android中捕获布局屏幕 [英] Capture layout screen in Android
问题描述
我遇到了一个问题.我想捕获XML布局的屏幕.我知道这个问题有很多话题.我都读了.但是没有人帮助我. 注意:我想获取当前活动不包含的布局的屏幕截图
I am facing an issue. I want to capture screen of a XML layout. I know there is a lot of topic on this question. I read them all. But No one of them helped me. Note : I want to get screen capture of a layout wich is not part of the current activity
这是我的函数,用于捕获Y布局(称为X活动)
this my function which is suppose to make capture of the Y layout (called on X activity)
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
}
当我保存位图时,它什么也没显示.我的保存位图功能可以正常工作,我不想打扰您. 所以我的问题很简单:什么原因导致我的功能无法正常工作?
And when I save the bitmap it show me nothing. My save bitmap function work fine, I don't want to bother you with that. So my question is simply : what cause my function not working ?
推荐答案
尝试以下解决方案
Java
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
}
供您参考
我还添加了经过测试的完整代码,对我来说一切正常.
I have also added full code which I was tested and it's working fine for me.
activity_main_two.xml
如果您使用黑色,那么您需要将背景颜色添加为白色,其他副图像则显示为黑色.
If you are use black color So, you need to add background color as white other vice full image show as black.
<?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>
Main2Activity
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
}
}
已保存的屏幕截图图像
我希望这可以为您提供帮助!
I hope this can help you!
这篇关于在Android中捕获布局屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!