2014-02-12 5 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; 

Поместите свой цикл здесь ...

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


    } 




} 
Смежные вопросы