gpt4 book ai didi

c# - xamarin 系统.绘图.图形

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:54 25 4
gpt4 key购买 nike

我正在尝试在 Android 手机屏幕上绘制图形。我已经在 Windows 窗体应用程序中创建了我需要的类,以使测试更容易和更快。我的应用程序使用 System.Drawing 类

当我尝试在 xamarin android 项目中应用我的代码时,System.Drawing 类显然不包含“Graphics”、“Font”、“Pen”和“Brushes”类。 System.Drawing.Graphics 类记录在 xamarin android api 中,但我无法让它工作。

我是否必须导入不同的类才能使用这些类,或者我是否必须更改我编写的代码?

我的图形类(只绘制 x 和 y 轴 + 数字):Graph Class

//author Frank Keuning
using Android.Content;
using Android.Graphics.Drawables;
using Android.Graphics.Drawables.Shapes;
using Android.Views;
using System.Drawing;

namespace App2
{
public class Graph
{
Graphics g;
int height;
int width;
int step;
int x;
int y;

Point yNumPoint;
Point xNumPoint;
Font font = new Font("Verdana", 7.5f);
Pen pen = Pens.Black;

public Graph(Graphics gph, int hght, int wdth, int stp, int xStart, int yStart)
{
g = gph;
height = hght;
width = wdth;
step = stp;
x = xStart;
y = yStart;
}

public void draw(Graphics g)
{
g.DrawLine(pen, x, y, x, height);
g.DrawLine(pen, x, height, width, height);
drawNumbers(g);
}

void drawNumbers(Graphics g)
{
yNumPoint = new Point(0, height - y);
xNumPoint = new Point(x, height + y);
int yNumValue = 0;
int xNumValue = 0;
while (yNumValue <= height)
{
g.DrawString(yNumValue.ToString(), font, Brushes.Black, yNumPoint);
yNumValue += step;
yNumPoint.Y -= step;
}

while (xNumValue <= width)
{
g.DrawString(xNumValue.ToString(), font, Brushes.Black, xNumPoint);
xNumValue += step;
xNumPoint.X += step;
}
}
}
}

最佳答案

对于我的项目,我有相同的要求:在 Android 中使用原生图形。

我想出了一个移植层。它运行速度相对较快,但某些功能可能尚未实现:

https://github.com/cail/AndroidDrawing

关于c# - xamarin 系统.绘图.图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34741944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com