Xamarin forms Android - 从 DataTemplate 创建自定义 ListView 分隔符(将 DataTemplate 转换为 Drawable)
Xamarin forms Android - Create Custom ListView divider from DataTemplate (Convert DataTemplate into Drawable)
我正在为 android ListView 实现渲染器,我想控制分隔线的外观。因此,我创建了一个 DataTemplate。
问题是 e.NewElement.SeperatorTemplate.CreateContent()
returns View object.
和 listView.Divider
接受 Drawable 对象。
我需要高效的转换,所以将其保存为图像是不可能的。
有什么想法吗?
找到了使用视图的绘图缓存的解决方案:
//Convert Android view into Drawable
view.DrawingCacheEnabled = true;
view.Layout(0, 0, (int)Math.Round(request.Request.Width * Density), (int)Math.Round(request.Request.Height * Density));
view.BuildDrawingCache(true);
var bitmap = Bitmap.CreateBitmap(view.GetDrawingCache(true));
view.DrawingCacheEnabled = false;
Drawable drawable = new BitmapDrawable(bitmap);
完整代码:
//Get Xamarin forms view from DataTemplate
Xamarin.Forms.View divider = (Xamarin.Forms.View)(e.NewElement.SeperatorTemplate?.CreateContent());
if (divider != null)
{
//get renderer
var dividerRenderer = Platform.CreateRenderer(divider);
//Measuring the element
SizeRequest request = dividerRenderer.Element.Measure(double.PositiveInfinity, double.PositiveInfinity);
//Measuring element Chilren
Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion(dividerRenderer.Element, new Rectangle(0.0, 0.0, request.Request.Width, request.Request.Height));
ViewGroup view = dividerRenderer.ViewGroup;
//Convert Android view into Drawable
view.DrawingCacheEnabled = true;
view.Layout(0, 0, (int)Math.Round(request.Request.Width * Density), (int)Math.Round(request.Request.Height * Density));
view.BuildDrawingCache(true);
var bitmap = Bitmap.CreateBitmap(view.GetDrawingCache(true));
view.DrawingCacheEnabled = false;
//Set divider Drawable divider to native list divider
listView.Divider = new BitmapDrawable(bitmap);
listView.DividerHeight = (int)Math.Round(request.Request.Height * Density);
}
我正在为 android ListView 实现渲染器,我想控制分隔线的外观。因此,我创建了一个 DataTemplate。
问题是 e.NewElement.SeperatorTemplate.CreateContent()
returns View object.
和 listView.Divider
接受 Drawable 对象。
我需要高效的转换,所以将其保存为图像是不可能的。 有什么想法吗?
找到了使用视图的绘图缓存的解决方案:
//Convert Android view into Drawable
view.DrawingCacheEnabled = true;
view.Layout(0, 0, (int)Math.Round(request.Request.Width * Density), (int)Math.Round(request.Request.Height * Density));
view.BuildDrawingCache(true);
var bitmap = Bitmap.CreateBitmap(view.GetDrawingCache(true));
view.DrawingCacheEnabled = false;
Drawable drawable = new BitmapDrawable(bitmap);
完整代码:
//Get Xamarin forms view from DataTemplate
Xamarin.Forms.View divider = (Xamarin.Forms.View)(e.NewElement.SeperatorTemplate?.CreateContent());
if (divider != null)
{
//get renderer
var dividerRenderer = Platform.CreateRenderer(divider);
//Measuring the element
SizeRequest request = dividerRenderer.Element.Measure(double.PositiveInfinity, double.PositiveInfinity);
//Measuring element Chilren
Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion(dividerRenderer.Element, new Rectangle(0.0, 0.0, request.Request.Width, request.Request.Height));
ViewGroup view = dividerRenderer.ViewGroup;
//Convert Android view into Drawable
view.DrawingCacheEnabled = true;
view.Layout(0, 0, (int)Math.Round(request.Request.Width * Density), (int)Math.Round(request.Request.Height * Density));
view.BuildDrawingCache(true);
var bitmap = Bitmap.CreateBitmap(view.GetDrawingCache(true));
view.DrawingCacheEnabled = false;
//Set divider Drawable divider to native list divider
listView.Divider = new BitmapDrawable(bitmap);
listView.DividerHeight = (int)Math.Round(request.Request.Height * Density);
}