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);
 

                // 繪製路徑
                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();
            }
        }
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章