我想将球图像以圆形或360度移动,但是我尝试过,但是它只能在 Canvas 上绘制球图像,而不能以圆形旋转。

能否请您提出可行的解决方案,或者给我提供一些类型的源代码,这些源代码可以帮助我绕圈移动对象。

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


    }




}

关于android - 如何在android中沿圆圈移动图像?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21728588/

10-15 19:02
查看更多