如何在Android中沿着圆形移动图像?

5

我想让我的球形图片在一个圆圈或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();

}

每次绘制调用时,将角度变量分配为5.. 将角度变量放在draw函数之外。 - Zar E Ahmer
2个回答

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);


    }




}

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;

在这里放置你的for循环...

   float x = constant + sin(angle) * scalar;
    float y = constant + cos(angle) * scalar;
    ellipse(x,y,50,50);

这个链接可能有助于可视化我的代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接