我想将我的球形图像移动到一个圆圈或360度,我尝试过但它只在画布上绘制球形图像而不是在圆圈中旋转。
您能否提出可行的解决方案或给我一些类型的源代码,以帮助我将对象移动到圆圈中。
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();
}
答案 0 :(得分:1)
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);
}
}
答案 1 :(得分:0)
//cos motion -> constant + cos(angle) * scalar. Sin motion is the same.
// Sin motion + cos motion = Circular motion
int constant = 250;
float angle = 0.05;
int scalar = 100;
float speed = 0.05;
把你的循环放在这里......
float x = constant + sin(angle) * scalar;
float y = constant + cos(angle) * scalar;
ellipse(x,y,50,50);
此link可能有助于我的代码可视化..