2014-02-12 84 views
5

我想將我的球像圓形或360度,我都試過,但它只能在畫布上繪製球圖像和圈不旋轉。如何在android中沿着圓圈移動圖像?

能否請你提出可行的解決方案或者給我一些類型的源代碼,可以幫助我在圈子移動對象。

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

當繪製調用角度每次爲5分配把角度變量放在外面畫出來 – Nepster

回答

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

link有助於看見我的代碼..

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


    } 




}