如何在圆角内明智地画线?

时间:2019-05-22 13:00:09

标签: android xamarin.android

在android中,我创建了自定义视图。我先绘制了一个圆,现在我想在一个圆内明智地绘制直线角度。我想要这样的动画。 https://drive.google.com/file/d/1Qx0MBu-77JIlQTByqGTyD-KtGKOB8naG/view?usp=sharing

我使用画布绘制圆和线。我使用了viewpager。如果我滑动viewpager,则饼图将旋转。

我现在所做的一切。设置动画时,它总是从零开始: https://drive.google.com/file/d/12mmAUOeY77jAlj_GmM3Ymcx5m34vli3X/view?usp=sharing

我已完成以下代码:

    @Override
public void onCreate() {
    super.onCreate();

    Random random = new Random();
    int id = random.nextInt(1000);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startMyOwnForeground();
    }
    else {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, FOREGROUND_NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
                .setSmallIcon(R.drawable.ic_launcher_fury)
                .setContentTitle(getResources().getString(R.string.app_is_in_background))
                .build();
        startForeground(id, notification);
    }
}

@RequiresApi(Build.VERSION_CODES.O)
private void startMyOwnForeground()
{
    Random random = new Random();
    int id = random.nextInt(1000);

    NotificationChannel chan = new NotificationChannel(FOREGROUND_NOTIFICATION_CHANNEL_ID, foregroundChannelName, NotificationManager.IMPORTANCE_NONE);
    chan.setLightColor(Color.BLUE);
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(chan);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, FOREGROUND_NOTIFICATION_CHANNEL_ID);
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(android.R.drawable.btn_dropdown)
            .setContentTitle(getResources().getString(R.string.app_is_in_background))
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();
    startForeground(id, notification);
}

1 个答案:

答案 0 :(得分:1)

正在运行GIF。

enter image description here

PieView.cs

  public class PieView:View
   {

    int w, h, pl, pr, pt, pb, usableWidth, usableHeight, radius, cx, cy, lineLenght;
    int handTruncation, hourHandTruncation = 0;
    Paint paint;
     int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    Rect rect = new Rect();
    public PieView(Context context) : base(context)
    {

    }

    public PieView(Context context, IAttributeSet attrs) : base(context, attrs)
    {
    }

    public PieView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
    {
    }

    public PieView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
    {
    }

    public override  void Draw(Canvas canvas)
    {
        base.Draw(canvas);
        w = Width;
        h = Height;

        pl = PaddingLeft+10;
        pr = PaddingRight+10;
        pt = PaddingTop+10;
        pb = PaddingBottom+10;

        usableWidth = w - (pl + pr);
        usableHeight = h - (pt + pb);

        radius = Math.Min(usableWidth, usableHeight) / 2;
        cx = pl + (usableWidth / 2);
        cy = pt + (usableHeight / 2);

        int min = Math.Min(usableWidth, usableHeight);

        handTruncation = min / 20;
        hourHandTruncation = min / 7;


        lineLenght = radius - (pl * 2) - (pr * 2);
        paint = new Paint();
        paint.Color = Android.Graphics.Color.White;
        paint.SetStyle(Paint.Style.Stroke);
        paint.StrokeWidth = 5;

        canvas.DrawCircle(cx, cy, radius , paint);
        drawNumeral(canvas);
        drawHands(canvas);
        PostInvalidateDelayed(200);
        Invalidate();

    }



    private void drawHands(Canvas canvas)
    {

        Calendar c = Calendar.Instance;
        float hour = c.Get(CalendarField.HourOfDay);
        hour = hour > 12 ? hour - 12 : hour;
        drawHand1(canvas, (hour + c.Get(CalendarField.Minute) / 60) * 5f,true);
        drawHand1(canvas, c.Get(CalendarField.Minute),false);
        drawHand1(canvas, c.Get(CalendarField.Second),false);
    }
    private void drawNumeral(Canvas canvas)
    {
        paint.TextSize=50;
        foreach (var number in numbers)
        {
            string tmp = number.ToString();
            paint.GetTextBounds( tmp, 0, tmp.Length, rect); //getTextBounds(tmp, 0, tmp.length(), rect);
            double angle = Math.PI / 6 * (number - 3);
            int x = (int)(w / 2 + Math.Cos(angle) * radius - rect.Width() / 2);
            int y = (int)(h / 2 + Math.Sin(angle) * radius + rect.Height() / 2);
            canvas.DrawText(tmp, x, y, paint);
        }

    }

    private void drawHand1(Canvas canvas, double loc, bool isHour)
    {

       double angle = Math.PI * loc / 30 - Math.PI / 2;
        int handRadius = isHour ? radius - handTruncation - hourHandTruncation : radius - handTruncation;
        canvas.DrawLine(Width / 2, Height / 2,
          (float)(Width / 2 + Math.Cos(angle) * handRadius),
          (float)(Height / 2 + Math.Sin(angle) * handRadius),
          paint);

    }

}

您可以在MainActivity.cs中使用它

        RelativeLayout relativeLayout1 = FindViewById<RelativeLayout> 
        (Resource.Id.relativeLayout1);
        relativeLayout1.SetBackgroundColor(Color.Black);
        AddContentView(new PieView(this),new ViewGroup.LayoutParams(-1,-1));