2012-02-08 60 views
1

我想移動一個矩形,我創建到屏幕的中心,因爲我正在使用加速度計來允許它移動,有沒有辦法將形狀移動到屏幕中心而不使用android xml?如何將在onDraw方法中創建的形狀移動到屏幕中心?

public class CustomDrawableView extends View 
    { 
     static final int width = 150; 
     static final int height = 250; 

     public CustomDrawableView(Context context) 
     { 
      super(context); 

      mDrawable = new ShapeDrawable(new RectShape()); 
      mDrawable.getPaint().setColor(0xff74AC23); 
      mDrawable.setBounds(x, y, x + width, y + height); 

     } 

     protected void onDraw(Canvas canvas) 
     { 

      RectF rect = new RectF(AccelActivity.x, AccelActivity.y, AccelActivity.x + width, AccelActivity.y 
        + height); // set bounds of rectangle 
      Paint p = new Paint(); // set some paint options 
      p.setColor(Color.BLUE); 
      canvas.drawRect(rect, p); 
      invalidate(); 

     } 
    } 
} 

回答

相關問題