rootView高度和宽度Xamarin.Android [英] rootView Height and Width in Xamarin.Android

查看:645
本文介绍了rootView高度和宽度Xamarin.Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得在 Xamarin.Android 平台屏幕捕获。

I am trying to get a screen capture in the Xamarin.Android platform.

public static Android.Content.Context Context { get; private set; }

public override View OnCreateView(View parent, string name, Context context, IAttributeSet attrs)
{
    MainActivity.Context = context;
    return base.OnCreateView(parent, name, context, attrs);
}

我试图找出为什么以下 rootView.Width 高度 0返回所有的时间。

I am trying to find out the why the following rootView.Width and Height returns 0 all the time.

var rootView = ((Activity)MainActivity.Context).Window.DecorView.RootView;
Console.WriteLine ("{0}x{1}", rootView.Width,rootView.Height);

我的最终目标是夺取视图的屏幕截图作为图像和生成PDF。

My ultimate goal is to capture the screenshot of the view as an image and generate pdf.

推荐答案

在onCreateView宽度和放大器;对象的高度都还没有定义。它们在该活动的生命周期的后期阶段定义

In the onCreateView the width & height of objects are not yet defined. They are defined in a later stage of the activity's lifecycle.

您必须使用treeviewobserver这一点。

You have to use the treeviewobserver for this.

与rootview示例:

Example with your rootview:

rootView.ViewTreeObserver.GlobalLayout += (object sender, EventArgs e) => {
      Console.WriteLine ("{0}x{1}", rootView.Width,rootView.Height);
};

在该方法中,宽度&放大器;高度将是已知的。

In that method the width & height will be known.

此外,你要采取的rootview的图片,要做到这一点,最好的办法就是用这个方法,这将自动输出一个位图的视图变量b。

Furthermore, you want to take a picture of the your rootview, the best way to do this is to use this method, this will automatically output a Bitmap of the view to variable b.

rootView.DrawingCacheEnabled = true;
Bitmap b = rootView.GetDrawingCache(true);

希望这可以让你对你的方式!

Hope this gets you on your way!

这篇关于rootView高度和宽度Xamarin.Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆