I want to write percentage progress of a circular custom-written view 
seekBar (called seekArc). The code was written in java for the circular 
seekArc but I have been able to write it in C#. I what to achieve this in a 
circular form

<https://lh3.googleusercontent.com/-dw1-O-oOwT8/WAidmV-dXEI/AAAAAAAACnM/PGnlMRL5T6ohqnczY73LzbSgxnvTj40ygCLcB/s1600/HTRPg.png>
and i was able to do this

<https://lh3.googleusercontent.com/-jIAhM_bO4QY/WAidt_DF2fI/AAAAAAAACnQ/TeG63HmQYPoLNA_7vBRUz3Q74DPo3qtywCLcB/s1600/6Nb1j.png>

i have tried placing the percentage progress inside the thumb of the 
circular seekArc but this is what i get

<https://lh3.googleusercontent.com/-Qtjq5dPBFkg/WAid1SZs_eI/AAAAAAAACnU/UZ9H1-i2Za4ksLxyPHeYy-vXgZawuHcogCLcB/s1600/Capturefvfdd.PNG>

this is my code below.

this code is used to place the text inside the circular seek bar

public void SetThumb(Drawable thumb)
    {
        bool needUpdate;
        // This way, calling setThumb again with the same bitmap will result in
        // it recalcuating mThumbOffset (if for example it the bounds of the
        // drawable changed)
        if (mThumb != null && thumb != mThumb)
        {
            mThumb.SetCallback(null);
            needUpdate = true;
        }
        else
        {
            needUpdate = false;
        }
        if (thumb != null)
        {
            thumb.SetCallback(this);
            if (CanResolveLayoutDirection())
            {
                thumb.SetLayoutDirection(LayoutDirection);
            }
            // Assuming the thumb drawable is symmetric, set the thumb offset
            // such that the thumb will hang halfway off either edge of the
            // progress bar.
            mThumbOffset = thumb.IntrinsicWidth / 2;
            // If we're updating get the new states
            if (needUpdate &&
                    (thumb.IntrinsicWidth != mThumb.IntrinsicWidth
                        || thumb.IntrinsicHeight != mThumb.IntrinsicHeight))
            {
                RequestLayout();
            }
        }
        mThumb = thumb;
        applyThumbTint();
        Invalidate();
        if (needUpdate)
        {
            updateThumbAndTrackPos(Width, Height);
            if (thumb != null && thumb.IsStateful)
            {
                // Note that if the states are different this won't work.
                // For now, let's consider that an app bug.
                int[] state = GetDrawableState();
                thumb.SetState(state);
            }
        }
    }

    private void applyThumbTint()
    {
        if (mThumb != null && (mHasThumbTint || mHasThumbTintMode))
        {
            mThumb = mThumb.Mutate();
            if (mHasThumbTint)
            {
                mThumb.SetTintList(mThumbTintList);
            }
            if (mHasThumbTintMode)
            {
                mThumb.SetTintMode(mThumbTintMode);
            }
            // The drawable (or one of its children) may not have been
            // stateful before applying the tint, so let's try again.
            if (mThumb.IsStateful)
            {
                mThumb.SetState(GetDrawableState());
            }
        }
    }

    private void updateThumbAndTrackPos(int w, int h)
    {
        int paddedHeight = h - PaddingTop - PaddingBottom;
        Drawable track = ContextCompat.GetDrawable(Context, 
Resource.Drawable.seek_arc_control_selector);
        Drawable thumb = mThumb;
        // The max height does not incorporate padding, whereas the height
        // parameter does.
        int trackHeight = Java.Lang.Math.Min(MinimumHeight, paddedHeight);
        int thumbHeight = thumb == null ? 0 : thumb.IntrinsicHeight;
        // Apply offset to whichever item is taller.
        int trackOffset;
        int thumbOffset;
        if (thumbHeight > trackHeight)
        {
            int offsetHeight = (paddedHeight - thumbHeight) / 2;
            trackOffset = offsetHeight + (thumbHeight - trackHeight) / 2;
            thumbOffset = offsetHeight;
        }
        else
        {
            int offsetHeight = (paddedHeight - trackHeight) / 2;
            trackOffset = offsetHeight;
            thumbOffset = offsetHeight + (trackHeight - thumbHeight) / 2;
        }
        if (track != null)
        {
            int trackWidth = w - PaddingRight - PaddingLeft;
            track.SetBounds(0, trackOffset, trackWidth, trackOffset + 
trackHeight);
        }
        if (thumb != null)
        {
            setThumbPos(w, thumb, getScale(), thumbOffset);
        }
    }

    private float getScale()
    {
        int max = getMax();
        return max > 0 ? getProgress() / (float)max : 0;
    }

    private void setThumbPos(int w, Drawable thumb, float scale, int offset)
    {
        int available = w - PaddingLeft - PaddingRight;
        int thumbWidth = thumb.IntrinsicWidth;
        int thumbHeight = thumb.IntrinsicHeight;
        available -= thumbWidth;
        // The extra space for the thumb to move on the track
        available += mThumbOffset * 2;
        int thumbPos = (int)(scale * available + 0.5f);
        int top, bottom;
        if (offset == Java.Lang.Integer.MinValue)
        {
            Rect oldBounds = thumb.Bounds;
            top = oldBounds.Top;
            bottom = oldBounds.Bottom;
        }
        else
        {
            top = offset;
            bottom = offset + thumbHeight;
        }
        //the line below was this => int left = (isLayoutRtl() && 
mMirrorForRtl) ? available - thumbPos : thumbPos;
        int left = available - thumbPos; //before i changed to this
        int right = left + thumbWidth;
        Drawable background = Background;
        if (background != null)
        {
            int offsetX = PaddingLeft - mThumbOffset;
            int offsetY = PaddingTop;
            background.SetHotspotBounds(left + offsetX, top + offsetY,
                    right + offsetX, bottom + offsetY);
        }
        // Canvas will be translated, so 0,0 is where we start drawing
        thumb.SetBounds(left, top, right, bottom);
    }

    public void setProgressBar(int progress, Context context, SeekArc defkk)
    {
        Drawable d = ContextCompat.GetDrawable(context, 
Resource.Drawable.seek_arc_control_selector);
        Canvas c = new Canvas();
        Bitmap bitmap = Bitmap.CreateBitmap(d.IntrinsicWidth, 
d.IntrinsicHeight, Bitmap.Config.Argb8888);
        c.SetBitmap(bitmap);
        d.SetBounds(0, 0, d.IntrinsicWidth, d.IntrinsicHeight);
        d.Draw(c);
        //Bitmap bmp = bitmap.Copy(Bitmap.Config.Argb8888, true);
        string text = progress.ToString() + "%";
        Paint p = new Paint();
        p.SetTypeface(Typeface.CreateFromAsset(context.Assets, 
"fonts/Brandon_bld.otf"));
        p.TextSize = 18;
        p.Color = Color.White;
        int width = (int)p.MeasureText(text);
        int yPos = (int)((c.Height / 2) - ((p.Descent() + p.Ascent()) / 2));
        c.DrawText(text, (bitmap.Width - width) / 2, yPos, p);
        defkk.SetThumb(new BitmapDrawable(Resources, bitmap));//this refers to 
the first function setThumb().
    }


this is used to draw the circular arc and make it progress along that path.

protected override void OnDraw(Canvas canvas)
    {
        if (!mClockwise)
        {
            canvas.Scale(-1, 1, mArcRect.CenterX(), mArcRect.CenterY());
        }

        // Draw the arcs
        int arcStart = mStartAngle + mAngleOffset + mRotation;
        int arcSweep = mSweepAngle;
        canvas.DrawArc(mArcRect, arcStart, arcSweep, false, mArcPaint);
        canvas.DrawArc(mArcRect, arcStart, mProgressSweep, false, 
mProgressPaint);

        if (mEnabled)
        {
            // Draw the thumb nail
            canvas.Translate(mTranslateX - mThumbXPos, mTranslateY - 
mThumbYPos);
            mThumb.Draw(canvas);
        }
    }

    public static double ToRadian(double val) { return val * (Math.PI / 180); }
    public double ToDegree(double angle) { return angle * (180.0 / Math.PI); }

    protected override void OnMeasure(int widthMeasureSpec, int 
heightMeasureSpec)
    {

        int height = GetDefaultSize(Height, 
heightMeasureSpec);//SuggestedMinimunHeight
        int width = GetDefaultSize(Width, 
widthMeasureSpec);//SuggestedMinimumWidth
        int min = Math.Min(width, height);
        float top = 0;
        float left = 0;
        int arcDiameter = 0;

        mTranslateX = (int)(width * 0.5f);
        mTranslateY = (int)(height * 0.5f);

        arcDiameter = min - PaddingLeft;
        mArcRadius = arcDiameter / 2;
        top = height / 2 - (arcDiameter / 2);
        left = width / 2 - (arcDiameter / 2);
        float qTop = top / 4;//i kept this here to reduce padding height of 
seekbar by setting top to quarter its original size and bottom to qTop + 
arcDiameter
        mArcRect.Set(left, top, left + arcDiameter, top + arcDiameter);

        int arcStart = (int)mProgressSweep + mStartAngle + mRotation + 90;
        mThumbXPos = (int)(mArcRadius * Math.Cos(ToRadian(arcStart)));
        mThumbYPos = (int)(mArcRadius * Math.Sin(ToRadian(arcStart)));

        setTouchInSide(mTouchInside);
        base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
    }


I have been trying to figure out how to achieve positioning the thumb along 
the track but to no avail. Can someone please guide me in the right 
direction?



-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/fab7ff7f-8904-44d8-9dbc-fc4098c92a8f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to