c#移动开发在vs2017下 XAMARIN 在imageview图形绘制的例子

       先在axaml里拖进一个imageview的控件,至于画的内容跟原生android差不多的,基本上只要大小写转换下,就可以把安卓的拿来给xamarin用

        private void drawpic()
        {
            //   Display current =GetWindowManager().getDefaultDisplay();// 获取系统设备尺寸
            try
            {
  
                // 创建一个画布与屏幕属性一样,如果是在onDraw方法中就不需要创建了
                alter = Bitmap.CreateBitmap(imageView.MaxWidth, imageView.MaxHeight, Bitmap.Config.Argb8888);
                // alter = Android.Graphics.Bitmap.CreateBitmap(imageView.MaxWidth, imageView.MaxHeight, Bitmap.Config.Argb8888);
                // ARGB_8888就是由4(ARGB)个8位组成即32位


                // 位图位数越高代表其可以存储的颜色信息越多,当然图像也就越逼真
                canvas = new Canvas(alter);
                paint = new Paint();


                // 绘制点
                paint.StrokeWidth = 4.5f;
                paint.Color = Color.Red;
                canvas.DrawPoint(80, 200, paint);// 画一个点
                canvas.DrawPoint(80, 100, paint);// 画一个点
                canvas.DrawPoint(80, 50, paint);// 画一个点

                paint.Color = Color.Black ;
                canvas.DrawPoints(new float[] { 50, 200, 50, 300, 50, 400 }, paint);// 画多个点


                // 设置画笔的粗细
                // 绘制线                                                               
                paint.StrokeWidth = 15.5f;
                paint.Color = Color.Argb(200, 200, 200, 200);
                paint.Color = Color.Yellow;
                canvas.DrawLine(100, 50, 300,350, paint);


                // 绘制圆
                paint.SetStyle(Paint.Style.Stroke);// Paint.Style.STROK---轮廓
                // Paint.Style.FILL_AND_STROKE---填充
                paint.Color = Color.Blue;
                //paint.setAntiAlias(true);// 设置画笔的锯齿效果,true是去除锯齿
                canvas.DrawCircle(200, 200, 160, paint);
                canvas.DrawCircle(300, 300, 80, paint);


                // 绘制椭圆
                RectF rf = new RectF(100, 200, 500, 400);
                paint.Color = Color.White;
                canvas.DrawOval(rf, paint);

                // 绘制矩形
                canvas.DrawRect(rf, paint);
                // 画弧
                RectF rf1 = new RectF(200, 600, 500, 800);
                paint.Color=Color.Red;
                canvas.DrawArc(rf1, 200, 130, false, paint);
                // 画扇形
                RectF rf2 = new RectF(200, 800, 500, 1000);
                paint.Color=Color.Red;
                canvas.DrawArc(rf2, 200, 130, true, paint);
                // 画弧,第一个参数是RectF:该类是第二个参数是角度的开始,第三个参数是多少度,
                // 第四个参数是真的时候画扇形,是假的时候画弧线


                // 绘制文字
                paint.Color = Color.Green;
                paint.StrokeWidth = 2.0f;
                paint.SetTypeface(Typeface.SansSerif);// 参数typeface为字体样式Typeface.DEFAULT:默认字体。
                //paint.setTextSize(30); // 设置字体的大小
                paint.TextSize = 30;
                canvas.DrawText("hello word!!", 500, 700, paint);
 

扫描二维码关注公众号,回复: 6773068 查看本文章

                // 绘制路径
                paint.Color = Color.Green;
                Path path = new Path();
                path.MoveTo(400, 100);
                path.LineTo(200, 350);
                path.LineTo(200, 850);
                path.LineTo(400, 1100);
                path.LineTo(600, 850);
                path.LineTo(600, 350);
                path.Close();// 封闭或者path.lineTo(400, 100);即开始的位置
                canvas.DrawPath(path, paint);


                imageView.SetImageBitmap(alter);//放在最后,可以绘制以上所有图形

            }
            catch (Exception ex)
            {
                Toast.MakeText(this, "是:" + ex, ToastLength.Long).Show();
            }
        }
 

猜你喜欢

转载自blog.csdn.net/u014194297/article/details/93332625