How to draw a moving circle on the bottom line of the oval?









up vote
2
down vote

favorite












I need to implement something like SeekBar. I created my class heir from View. In which I paint an oval on canvas. When touched, this oval goes up and down. I can not correctly calculate the position for the point (thumb).



enter image description here



here is my code, but it has already been redone many times and stopped working:



public class BalanceView extends View 
private Paint ovalPaint;
private Paint thumbPaint;
private float ovalBottom = 0f;
private Bitmap thumb;
private RectF oval1;


//params
private int maxValue = 18;
private float ovalPaintWidth = 2.0f;
private float ovalHeight = 60f;
//end params


private float touchX = 0f;
private float touchY = 0f;

private float mUnreachedRadius;



public BalanceView(Context context)
super(context);
init(context, null);


public BalanceView(Context context, @Nullable AttributeSet attrs)
super(context, attrs);
init(context, attrs);


public BalanceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
super(context, attrs, defStyleAttr);
init(context, attrs);




private void init(Context ctx, @Nullable AttributeSet attrs)
if (attrs != null)



if (ovalPaint == null)
ovalPaint = new Paint();
ovalPaint.setAntiAlias(true);
ovalPaint.setStrokeWidth(ovalPaintWidth);
ovalPaint.setStyle(Paint.Style.STROKE);
Shader shader = new SweepGradient(10, 10, new intR.color.white, R.color.black, R.color.white, null);
ovalPaint.setShader(shader);


if (thumbPaint == null)
thumbPaint = new Paint();


thumb = BitmapFactory.decodeResource(getResources(), R.drawable.move_point);


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//refershPosition();


@Override
protected void onDraw(Canvas canvas)
paintOval(ovalPaint, canvas);


float cos = computeCos(touchX, touchY);
float y = calcYLocationInWheel(cos);

Log.d("balance", "cos: " + cos + " y: " + y + " touchX: " + touchX + " touchY: " + touchY + "mUnreachedRadius: " + mUnreachedRadius) ;
canvas.drawBitmap(thumb, touchX, y, thumbPaint);

super.onDraw(canvas);


private void paintOval(Paint paint, @NonNull Canvas canvas)
if (paint != null)
int right = getWidth() - 30;
int left = (getWidth() - right) / 2;
float top = ovalBottom - ovalHeight;

oval1 = new RectF(left, top, right + left, ovalBottom);
canvas.drawOval(oval1, paint);



@Override
public boolean dispatchTouchEvent(MotionEvent event)
if (event.getY() >= getTop() && event.getY() <= getBottom())
touchY = event.getY();

if (event.getX() <= oval1.right - thumb.getWidth() && event.getX() >= oval1.left)
touchX = event.getX();


ovalBottom = touchY;
invalidate();
return super.dispatchTouchEvent(event);


private float computeCos(float x, float y)
float width = x - oval1.width() / 2;
float height = y - oval1.height() / 2;
float slope = (float) Math.sqrt(width * width + height * height);
return height / slope;


private float calcYLocationInWheel(double cos)
return oval1.bottom * (float) cos;





I try so:



private float getY(float a, float b, float x)
// return (float) (Math.pow(b, 2d) * ((Math.pow(a, 2d) - Math.pow(x, 2d)) / Math.pow(a, 2d)));
return (float)
((Math.pow(b, 2d) / Math.pow(a, 2d)) * (Math.sqrt(b * b * (a * a - x * x))));



But the data is wrong.










share|improve this question























  • Why don't you extend the SeekBar or ProgressBar instead of the generic view class? That way you can leverage some of the useful behaviour of the classes.
    – Cool Guy CG
    Nov 8 at 13:41










  • I wanted to do by analogy, but oval. From Behind this, as in the example, expanded View github.com/feeeei/CircleSeekbar/blob/master/lib/src/main/java/…
    – Yacapper
    Nov 8 at 13:45














up vote
2
down vote

favorite












I need to implement something like SeekBar. I created my class heir from View. In which I paint an oval on canvas. When touched, this oval goes up and down. I can not correctly calculate the position for the point (thumb).



enter image description here



here is my code, but it has already been redone many times and stopped working:



public class BalanceView extends View 
private Paint ovalPaint;
private Paint thumbPaint;
private float ovalBottom = 0f;
private Bitmap thumb;
private RectF oval1;


//params
private int maxValue = 18;
private float ovalPaintWidth = 2.0f;
private float ovalHeight = 60f;
//end params


private float touchX = 0f;
private float touchY = 0f;

private float mUnreachedRadius;



public BalanceView(Context context)
super(context);
init(context, null);


public BalanceView(Context context, @Nullable AttributeSet attrs)
super(context, attrs);
init(context, attrs);


public BalanceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
super(context, attrs, defStyleAttr);
init(context, attrs);




private void init(Context ctx, @Nullable AttributeSet attrs)
if (attrs != null)



if (ovalPaint == null)
ovalPaint = new Paint();
ovalPaint.setAntiAlias(true);
ovalPaint.setStrokeWidth(ovalPaintWidth);
ovalPaint.setStyle(Paint.Style.STROKE);
Shader shader = new SweepGradient(10, 10, new intR.color.white, R.color.black, R.color.white, null);
ovalPaint.setShader(shader);


if (thumbPaint == null)
thumbPaint = new Paint();


thumb = BitmapFactory.decodeResource(getResources(), R.drawable.move_point);


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//refershPosition();


@Override
protected void onDraw(Canvas canvas)
paintOval(ovalPaint, canvas);


float cos = computeCos(touchX, touchY);
float y = calcYLocationInWheel(cos);

Log.d("balance", "cos: " + cos + " y: " + y + " touchX: " + touchX + " touchY: " + touchY + "mUnreachedRadius: " + mUnreachedRadius) ;
canvas.drawBitmap(thumb, touchX, y, thumbPaint);

super.onDraw(canvas);


private void paintOval(Paint paint, @NonNull Canvas canvas)
if (paint != null)
int right = getWidth() - 30;
int left = (getWidth() - right) / 2;
float top = ovalBottom - ovalHeight;

oval1 = new RectF(left, top, right + left, ovalBottom);
canvas.drawOval(oval1, paint);



@Override
public boolean dispatchTouchEvent(MotionEvent event)
if (event.getY() >= getTop() && event.getY() <= getBottom())
touchY = event.getY();

if (event.getX() <= oval1.right - thumb.getWidth() && event.getX() >= oval1.left)
touchX = event.getX();


ovalBottom = touchY;
invalidate();
return super.dispatchTouchEvent(event);


private float computeCos(float x, float y)
float width = x - oval1.width() / 2;
float height = y - oval1.height() / 2;
float slope = (float) Math.sqrt(width * width + height * height);
return height / slope;


private float calcYLocationInWheel(double cos)
return oval1.bottom * (float) cos;





I try so:



private float getY(float a, float b, float x)
// return (float) (Math.pow(b, 2d) * ((Math.pow(a, 2d) - Math.pow(x, 2d)) / Math.pow(a, 2d)));
return (float)
((Math.pow(b, 2d) / Math.pow(a, 2d)) * (Math.sqrt(b * b * (a * a - x * x))));



But the data is wrong.










share|improve this question























  • Why don't you extend the SeekBar or ProgressBar instead of the generic view class? That way you can leverage some of the useful behaviour of the classes.
    – Cool Guy CG
    Nov 8 at 13:41










  • I wanted to do by analogy, but oval. From Behind this, as in the example, expanded View github.com/feeeei/CircleSeekbar/blob/master/lib/src/main/java/…
    – Yacapper
    Nov 8 at 13:45












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I need to implement something like SeekBar. I created my class heir from View. In which I paint an oval on canvas. When touched, this oval goes up and down. I can not correctly calculate the position for the point (thumb).



enter image description here



here is my code, but it has already been redone many times and stopped working:



public class BalanceView extends View 
private Paint ovalPaint;
private Paint thumbPaint;
private float ovalBottom = 0f;
private Bitmap thumb;
private RectF oval1;


//params
private int maxValue = 18;
private float ovalPaintWidth = 2.0f;
private float ovalHeight = 60f;
//end params


private float touchX = 0f;
private float touchY = 0f;

private float mUnreachedRadius;



public BalanceView(Context context)
super(context);
init(context, null);


public BalanceView(Context context, @Nullable AttributeSet attrs)
super(context, attrs);
init(context, attrs);


public BalanceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
super(context, attrs, defStyleAttr);
init(context, attrs);




private void init(Context ctx, @Nullable AttributeSet attrs)
if (attrs != null)



if (ovalPaint == null)
ovalPaint = new Paint();
ovalPaint.setAntiAlias(true);
ovalPaint.setStrokeWidth(ovalPaintWidth);
ovalPaint.setStyle(Paint.Style.STROKE);
Shader shader = new SweepGradient(10, 10, new intR.color.white, R.color.black, R.color.white, null);
ovalPaint.setShader(shader);


if (thumbPaint == null)
thumbPaint = new Paint();


thumb = BitmapFactory.decodeResource(getResources(), R.drawable.move_point);


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//refershPosition();


@Override
protected void onDraw(Canvas canvas)
paintOval(ovalPaint, canvas);


float cos = computeCos(touchX, touchY);
float y = calcYLocationInWheel(cos);

Log.d("balance", "cos: " + cos + " y: " + y + " touchX: " + touchX + " touchY: " + touchY + "mUnreachedRadius: " + mUnreachedRadius) ;
canvas.drawBitmap(thumb, touchX, y, thumbPaint);

super.onDraw(canvas);


private void paintOval(Paint paint, @NonNull Canvas canvas)
if (paint != null)
int right = getWidth() - 30;
int left = (getWidth() - right) / 2;
float top = ovalBottom - ovalHeight;

oval1 = new RectF(left, top, right + left, ovalBottom);
canvas.drawOval(oval1, paint);



@Override
public boolean dispatchTouchEvent(MotionEvent event)
if (event.getY() >= getTop() && event.getY() <= getBottom())
touchY = event.getY();

if (event.getX() <= oval1.right - thumb.getWidth() && event.getX() >= oval1.left)
touchX = event.getX();


ovalBottom = touchY;
invalidate();
return super.dispatchTouchEvent(event);


private float computeCos(float x, float y)
float width = x - oval1.width() / 2;
float height = y - oval1.height() / 2;
float slope = (float) Math.sqrt(width * width + height * height);
return height / slope;


private float calcYLocationInWheel(double cos)
return oval1.bottom * (float) cos;





I try so:



private float getY(float a, float b, float x)
// return (float) (Math.pow(b, 2d) * ((Math.pow(a, 2d) - Math.pow(x, 2d)) / Math.pow(a, 2d)));
return (float)
((Math.pow(b, 2d) / Math.pow(a, 2d)) * (Math.sqrt(b * b * (a * a - x * x))));



But the data is wrong.










share|improve this question















I need to implement something like SeekBar. I created my class heir from View. In which I paint an oval on canvas. When touched, this oval goes up and down. I can not correctly calculate the position for the point (thumb).



enter image description here



here is my code, but it has already been redone many times and stopped working:



public class BalanceView extends View 
private Paint ovalPaint;
private Paint thumbPaint;
private float ovalBottom = 0f;
private Bitmap thumb;
private RectF oval1;


//params
private int maxValue = 18;
private float ovalPaintWidth = 2.0f;
private float ovalHeight = 60f;
//end params


private float touchX = 0f;
private float touchY = 0f;

private float mUnreachedRadius;



public BalanceView(Context context)
super(context);
init(context, null);


public BalanceView(Context context, @Nullable AttributeSet attrs)
super(context, attrs);
init(context, attrs);


public BalanceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
super(context, attrs, defStyleAttr);
init(context, attrs);




private void init(Context ctx, @Nullable AttributeSet attrs)
if (attrs != null)



if (ovalPaint == null)
ovalPaint = new Paint();
ovalPaint.setAntiAlias(true);
ovalPaint.setStrokeWidth(ovalPaintWidth);
ovalPaint.setStyle(Paint.Style.STROKE);
Shader shader = new SweepGradient(10, 10, new intR.color.white, R.color.black, R.color.white, null);
ovalPaint.setShader(shader);


if (thumbPaint == null)
thumbPaint = new Paint();


thumb = BitmapFactory.decodeResource(getResources(), R.drawable.move_point);


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//refershPosition();


@Override
protected void onDraw(Canvas canvas)
paintOval(ovalPaint, canvas);


float cos = computeCos(touchX, touchY);
float y = calcYLocationInWheel(cos);

Log.d("balance", "cos: " + cos + " y: " + y + " touchX: " + touchX + " touchY: " + touchY + "mUnreachedRadius: " + mUnreachedRadius) ;
canvas.drawBitmap(thumb, touchX, y, thumbPaint);

super.onDraw(canvas);


private void paintOval(Paint paint, @NonNull Canvas canvas)
if (paint != null)
int right = getWidth() - 30;
int left = (getWidth() - right) / 2;
float top = ovalBottom - ovalHeight;

oval1 = new RectF(left, top, right + left, ovalBottom);
canvas.drawOval(oval1, paint);



@Override
public boolean dispatchTouchEvent(MotionEvent event)
if (event.getY() >= getTop() && event.getY() <= getBottom())
touchY = event.getY();

if (event.getX() <= oval1.right - thumb.getWidth() && event.getX() >= oval1.left)
touchX = event.getX();


ovalBottom = touchY;
invalidate();
return super.dispatchTouchEvent(event);


private float computeCos(float x, float y)
float width = x - oval1.width() / 2;
float height = y - oval1.height() / 2;
float slope = (float) Math.sqrt(width * width + height * height);
return height / slope;


private float calcYLocationInWheel(double cos)
return oval1.bottom * (float) cos;





I try so:



private float getY(float a, float b, float x)
// return (float) (Math.pow(b, 2d) * ((Math.pow(a, 2d) - Math.pow(x, 2d)) / Math.pow(a, 2d)));
return (float)
((Math.pow(b, 2d) / Math.pow(a, 2d)) * (Math.sqrt(b * b * (a * a - x * x))));



But the data is wrong.







android seekbar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 20:28

























asked Nov 8 at 13:38









Yacapper

112




112











  • Why don't you extend the SeekBar or ProgressBar instead of the generic view class? That way you can leverage some of the useful behaviour of the classes.
    – Cool Guy CG
    Nov 8 at 13:41










  • I wanted to do by analogy, but oval. From Behind this, as in the example, expanded View github.com/feeeei/CircleSeekbar/blob/master/lib/src/main/java/…
    – Yacapper
    Nov 8 at 13:45
















  • Why don't you extend the SeekBar or ProgressBar instead of the generic view class? That way you can leverage some of the useful behaviour of the classes.
    – Cool Guy CG
    Nov 8 at 13:41










  • I wanted to do by analogy, but oval. From Behind this, as in the example, expanded View github.com/feeeei/CircleSeekbar/blob/master/lib/src/main/java/…
    – Yacapper
    Nov 8 at 13:45















Why don't you extend the SeekBar or ProgressBar instead of the generic view class? That way you can leverage some of the useful behaviour of the classes.
– Cool Guy CG
Nov 8 at 13:41




Why don't you extend the SeekBar or ProgressBar instead of the generic view class? That way you can leverage some of the useful behaviour of the classes.
– Cool Guy CG
Nov 8 at 13:41












I wanted to do by analogy, but oval. From Behind this, as in the example, expanded View github.com/feeeei/CircleSeekbar/blob/master/lib/src/main/java/…
– Yacapper
Nov 8 at 13:45




I wanted to do by analogy, but oval. From Behind this, as in the example, expanded View github.com/feeeei/CircleSeekbar/blob/master/lib/src/main/java/…
– Yacapper
Nov 8 at 13:45

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208904%2fhow-to-draw-a-moving-circle-on-the-bottom-line-of-the-oval%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208904%2fhow-to-draw-a-moving-circle-on-the-bottom-line-of-the-oval%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto