[英]Xamarin: How can I align a logo image in the center of a SkiaSharp Bitmap
我正在尝试在SKBitmap
中间对齐徽标。
这是我的代码:
SKBitmap bmpResult = SKBitmap.Decode(imgByteArray);
bitmap.DrawBitmap(bmpResult, 0, 20);
我已经尝试过使用不同的图像,但结果是一样的。
我知道 SkiaSharp 有MeasureText
,但是,正如它所说,用于文本,而不是图像。
我尝试使用DeviceDisplay.MainDisplayInfo.Width
获取设备宽度并除以 2,但没有结果。
请注意,即使我得到一个确切的数字来划分(例如: bitmap.DrawBitmap(bmpResult, DeviceDisplay.MainDisplayInfo.Width / 2, 20);
),当我有不同的屏幕密度时,最终结果也会不同,即即,与一个设备对齐,与另一个设备对齐。
有谁知道我该如何解决?
您可以创建两个位图(bitmap1,bitmap2)。 先画bitmap1,再画bitmap2。 我在中心制作了两个位图供您参考。
public partial class Page8 : ContentPage
{
SKBitmap bitmap1 = new SKBitmap();
SKBitmap bitmap2 = new SKBitmap();
public Page8()
{
InitializeComponent();
bitmap1 = LoadBitmapResource(typeof(Page8), "App3.image.jpeg");
bitmap2 = LoadBitmapResource(typeof(Page8), "App3.durian_64.png");
// Create the SKCanvasView and set the PaintSurface handler
SKCanvasView canvasView = new SKCanvasView();
canvasView.PaintSurface += OnCanvasViewPaintSurface; ;
Content = canvasView;
}
private void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
{
SKImageInfo info = args.Info;
SKSurface surface = args.Surface;
SKCanvas canvas = surface.Canvas;
canvas.Clear();
float x1 = (info.Width - bitmap1.Width) / 2;
float y1 = (info.Height - bitmap1.Height) / 2;
float x2 = (info.Width - bitmap2.Width) / 2;
float y2 = (info.Height - bitmap2.Height) / 2;
canvas.DrawBitmap(bitmap1, x1, y1);
canvas.DrawBitmap(bitmap2, x2, y2);
}
public static SKBitmap LoadBitmapResource(Type type, string resourceID)
{
Assembly assembly = type.GetTypeInfo().Assembly;
using (Stream stream = assembly.GetManifestResourceStream(resourceID))
{
return SKBitmap.Decode(stream);
}
}
}
在屏幕截图中,您会在 bitmap1 的中心看到 bitmap2(绿色榴莲)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.