在android中移动固定圆形路径中的对象

时间:2014-06-17 07:00:19

标签: android animation math android-animation

我想在一个固定的圆形路径中移动一个物体,就像围绕太阳的行星一样。

根据我的研究,这是链接: how to make an object move in circular path? 但是,它无法正常工作。

以下是更新后的代码:

public class MainActivity extends Activity {
ourView v;

Bitmap tball;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    v = new ourView(this);

    setContentView(v);
    tball = (BitmapFactory.decodeResource(getResources(),
            R.drawable.blueball));

}

protected void onPause() {
    super.onPause();
    v.pause();
}

protected void onResume() {
    super.onResume();
    v.resume();
}

public class ourView extends SurfaceView implements Runnable {
    Thread t;
    SurfaceHolder holder;
    Boolean isitok = false;

    public ourView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        holder = getHolder();

    }

    // float aX, aY;

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (isitok == true) {
            // perform drawing
            if (!holder.getSurface().isValid()) {
                continue;
            }
            Canvas canvas = holder.lockCanvas();
            canvas.drawARGB(255, 150, 150, 10);

            // System.out.println("Canvas matrix  -" + canvas.getm));
            Paint p = new Paint();

            // canvas.drawBitmap(tball, (x - tball.getWidth()) / 2,
            // (y - tball.getHeight()) / 2, p);
            p.setStyle(Paint.Style.STROKE);
            p.setColor(Color.WHITE);
            p.setColor(Color.parseColor("#0101DF"));

            canvas.drawCircle(canvas.getWidth() / 2,
                    canvas.getHeight() / 2, 110, p);
            canvas.drawCircle(canvas.getWidth() / 2,
                    canvas.getHeight() / 2, 210, p);

            float x = (canvas.getWidth() / 2) - (tball.getWidth() / 2);
            float y = (canvas.getHeight() / 2) - 210 + (210 - 110) / 2
                    - (tball.getHeight() / 2);
            float MX = canvas.getWidth() / 2;
            float MY = canvas.getHeight() / 2;

            for (double i = 0; i < 12 ; i++) {
                float R = 160;
                x = (float) (MX + (R * Math.cos(i)));
                y = (float) (MY + (R * Math.sin(i)));
                //y = (float) (MY - (R * Math.sin( i )));
                canvas.drawBitmap(tball, x, y, p);

                i++;
            }

            // float movingpts[];

            holder.unlockCanvasAndPost(canvas);
        }
    }

    public void pause() {
        isitok = false;
        while (true) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            break;
        }
        t = null;
    }

    public void resume() {
        isitok = true;
        t = new Thread(this);
        t.start();

    }

}

}"

`

1 个答案:

答案 0 :(得分:1)

看看NineOldAndroids。这是一个动画库,每个Android版本都可以回到1.0。我不知道是否有可能实现固定的圆形路径动画,但是库看起来非常强大。

以下是路径动画的示例代码:link-to-sample