【问题标题】:How to draw hexagon using x,y, width and height properties?如何使用 x、y、宽度和高度属性绘制六边形?
【发布时间】:2019-05-25 16:40:31
【问题描述】:

我需要使用 x、y、width 和 height 属性来绘制六边形。

【问题讨论】:

    标签: c# android xamarin path shapes


    【解决方案1】:

    我根据你的描述写了一个demo,这是demo的截图。

    这是这个演示的代码。

    activity_main.axml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/iv_ImageView"
            />
    </RelativeLayout>
    

    MainActivity.cs

    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
    
            ImageView iv_ImageView = FindViewById<ImageView>(Resource.Id.iv_ImageView);
            Bitmap icon=BitmapFactory.DecodeResource(Resources,Resource.Drawable.Capture);
            new shape(icon, iv_ImageView);
        }
    }
    

    你可以在这个getHexagonalCroppedBitmap方法中设置宽度、高度和XY(我默认设置width=150height =150centerX = width / 2;centerY = height / 2;默认设置。你可以改变它)。 形状.cs

    internal class shape
    {
        private Bitmap bmp;
        private ImageView img;
    
        public shape(Bitmap bmp, ImageView img)
        {
            this.bmp = bmp;
            this.img = img;
            onDraw();
        }
    
        private void onDraw()
        {
            if(bmp.Width == 0 || bmp.Height == 0)
            {
    
                return;
            }
    
            int w = bmp.Width, h = bmp.Height;
    
            Bitmap roundBitmap = getHexagonalCroppedBitmap(bmp, h);
            img.SetImageBitmap(roundBitmap);
        }
    
        private Bitmap getHexagonalCroppedBitmap(Bitmap bitmap, int radius)
        {
            Bitmap finalBitmap;
            if (bitmap.Width != radius || bitmap.Height!= radius)
                finalBitmap = Bitmap.CreateScaledBitmap(bitmap, radius, radius,
                        false);
            else
                finalBitmap = bitmap;
            Bitmap output = Bitmap.CreateBitmap(finalBitmap.Width,
                    finalBitmap.Height, Bitmap.Config.Argb8888);
            Canvas canvas = new Canvas(output);
    
            Paint paint = new Paint();
            Rect rect = new Rect(0, 0, finalBitmap.Width,
                    finalBitmap.Height);
    
            //note:you could set width and height in here
            int width = 150;
            int height = 150;
            //note: you could set x and y in here, i set the  centerX = width / 2;centerY = height / 2; by default.
    
            float centerX = width / 2;
            float centerY = height / 2;
    
            float radiusValue = width / 2;
            double radian30 = 30 * Math.PI / 180;
            float a = (float)(radiusValue * Math.Sin(radian30));
            float b = (float)(radiusValue * Math.Cos(radian30));
    
            Path path = new Path();
            path.MoveTo(centerX, 0);
    
            path.LineTo(centerX + b, centerY - a);
            path.LineTo(centerX + b, centerY + a);
            path.LineTo(centerX, height);
            path.LineTo(centerX - b, centerY + a);
            path.LineTo(centerX - b, centerY - a);
    
            path.Close();
            canvas.DrawARGB(0, 0, 0, 0);
            paint.Color=(Color.ParseColor("#BAB399"));
            canvas.DrawPath(path, paint);
            paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcIn));
            canvas.DrawBitmap(finalBitmap, rect, rect, paint);
    
            return output;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多