1

I am trying to move a rectangle that i created to the center of the screen as I am using an accelerometer to allow it to move, is there a way to move the shape to the center of the screen without using android xml?

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

        }
    }
}
user1169775
  • 43
  • 2
  • 8

1 Answers1

0

Yes, check this example, no use of xml there: How can I use the animation framework inside the canvas?

Use on sizeChanged to find the actual size of your canvas...

Community
  • 1
  • 1
Lumis
  • 21,517
  • 8
  • 63
  • 67