Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to move an image along circle in android?

Tags:

android

I want to move my ball image in a circle or 360 degree, I have tried but it only draws ball image on canvas and not rotating in circle.

Can you please suggest feasible solution or give me some type of source code that can help me to move object in circle.

 protected void onDraw(Canvas canvas) {

    // TODO Auto-generated method stub
    super.onDraw(canvas);
    canvas.drawColor(Color.WHITE);

    int cx = getWidth() / 2;
    int cy = getHeight() / 2;

    float angle = 5;
    float radius = 150;
    float x = (float) (cx + Math.cos(angle * Math.PI / 180F) * radius);
    float y = (float) (cy + Math.sin(angle * Math.PI / 180F) * radius);
    canvas.drawBitmap(ball, x, y, null);
    if (angle < 360) {

        angle += 5;
    }

    invalidate();

}
like image 313
user3301679 Avatar asked Nov 11 '22 13:11

user3301679


1 Answers

public class DotsProgressbar  extends View {

    private Paint paint1;
    float angle = 5;
    float radius = 150;

    public DotsProgressbar(Context context) {
        super(context);
        init();

    }
    public DotsProgressbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DotsProgressbar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
        init();
    }


    public void init(){

        // create the Paint and set its color
        paint1 = new Paint();
        paint1.setColor(Color.WHITE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLUE);


        int cx = getWidth() / 2;
        int cy = getHeight() / 2;


        float x = (float) (cx + Math.cos(angle * Math.PI / 180F) * radius);
        float y = (float) (cy + Math.sin(angle * Math.PI / 180F) * radius);
        canvas.drawCircle(x, y, 20, paint1);

        StartAnimation();
    }


    public void StartAnimation(){

        if (angle < 360) {

            angle += 5;
        }



        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                invalidate();
            }
        };new Handler().postDelayed(runnable,100);


    }




}
like image 93
Mehul Santoki Avatar answered Nov 14 '22 23:11

Mehul Santoki