Android Xamarin 上的屏幕截图,宽系统屏幕截图
Screenshot on Android Xamarin , Wide system screenshot
我的问题简而言之,我想为用户看到的屏幕截屏,而不是我的 Activity。假设我的应用程序已最小化,屏幕截图是针对屏幕本身而非我的应用程序截取的。
我在某处指出在非 root 设备上是可能的,因为 API 19 可能或 4.0 android,但我找不到办法做到这一点。
我在互联网上尝试了很多解决方案,但没有任何效果。红了很多却什么也没找到
我找到了这段代码,但是,确保它为我的应用程序(布局)截屏
这是 OnCreate 方法:
ImageView img;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
Button btn = FindViewById<Button>(Resource.Id.button1);
img = FindViewById<ImageView>(Resource.Id.imageView1);
btn.Click += Btn_Click;
}
public byte[] CaptureScreen()
{
var view = Window.DecorView.RootView;
view.DrawingCacheEnabled = true;
Bitmap bitmap = view.GetDrawingCache(true);
byte[] bitmapData;
using (var stream = new MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
bitmapData = stream.ToArray();
}
return bitmapData;
}
单击按钮时将其添加到 imageView:
private void Btn_Click(object sender, EventArgs e)
{
Bitmap bitmap = BitmapFactory.DecodeByteArray(CaptureScreen(), 0, CaptureScreen().Length);
img.SetImageBitmap(bitmap);
}
但正如我所说:它需要屏幕截图来显示布局,而不是实际屏幕。
我试图隐藏或最小化该应用程序,但没有任何反应。
我明白了。在到处搜索之后,我发现 java 中有一个新的 class 可以为您完成这项工作。这是媒体投影。它适用于 Android Lollipop 5.0 或更高版本并且不需要 root
以及 Play 商店中的所有应用程序都在使用它,因为我尝试下载许多应用程序以了解截屏背后的想法。
这是关于它的文档:
https://developer.android.com/reference/android/media/projection/MediaProjection
我的问题简而言之,我想为用户看到的屏幕截屏,而不是我的 Activity。假设我的应用程序已最小化,屏幕截图是针对屏幕本身而非我的应用程序截取的。
我在某处指出在非 root 设备上是可能的,因为 API 19 可能或 4.0 android,但我找不到办法做到这一点。
我在互联网上尝试了很多解决方案,但没有任何效果。红了很多却什么也没找到
我找到了这段代码,但是,确保它为我的应用程序(布局)截屏
这是 OnCreate 方法:
ImageView img;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
Button btn = FindViewById<Button>(Resource.Id.button1);
img = FindViewById<ImageView>(Resource.Id.imageView1);
btn.Click += Btn_Click;
}
public byte[] CaptureScreen()
{
var view = Window.DecorView.RootView;
view.DrawingCacheEnabled = true;
Bitmap bitmap = view.GetDrawingCache(true);
byte[] bitmapData;
using (var stream = new MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
bitmapData = stream.ToArray();
}
return bitmapData;
}
单击按钮时将其添加到 imageView:
private void Btn_Click(object sender, EventArgs e)
{
Bitmap bitmap = BitmapFactory.DecodeByteArray(CaptureScreen(), 0, CaptureScreen().Length);
img.SetImageBitmap(bitmap);
}
但正如我所说:它需要屏幕截图来显示布局,而不是实际屏幕。
我试图隐藏或最小化该应用程序,但没有任何反应。
我明白了。在到处搜索之后,我发现 java 中有一个新的 class 可以为您完成这项工作。这是媒体投影。它适用于 Android Lollipop 5.0 或更高版本并且不需要 root 以及 Play 商店中的所有应用程序都在使用它,因为我尝试下载许多应用程序以了解截屏背后的想法。
这是关于它的文档:
https://developer.android.com/reference/android/media/projection/MediaProjection