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();
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With