Excellent, thanks.
On 7 July, 17:17, Jack Ha <[email protected]> wrote:
> Your Court.onDraw() function will not get called by default for
> efficiency. You need to call setWillNotDraw(false) to enable it.
>
> --
> Jack Ha
> Open Source Development Center
> ・T・ ・ ・Mobile・ stick together
>
> The views, opinions and statements in this email are those of
> the author solely in their individual capacity, and do not
> necessarily represent those of T-Mobile USA, Inc.
>
> On Jul 7, 5:30 am, Carl <[email protected]> wrote:
>
>
>
> > I'm trying to do some simple UI stuff in a game (Basketball).
>
> > I've created my own Court class (which extends ViewGroup) and I want
> > to add a Ball (extends View) to the court.
>
> > Unfortunately my ball doesn't show up on the court, and in fact the
> > court doesn't draw either.
>
> > What have I done wrong?
>
> > package test.com;
>
> > import android.app.Activity;
> > import android.os.Bundle;
> > import android.view.ViewGroup.LayoutParams;
> > import android.widget.FrameLayout;
> > import android.widget.LinearLayout;
>
> > public class test extends Activity
> > {
> > /** Constants */
> > final int FPAR = LinearLayout.LayoutParams.FILL_PARENT;
>
> > private FrameLayout main;
>
> > /** Called when the activity is first created. */
> > @Override
> > public void onCreate(Bundle savedInstanceState)
> > {
> > super.onCreate(savedInstanceState);
> > this.addContentView(new Court(this), new LayoutParams(FPAR,
> > FPAR));
> > }
>
> > }
>
> > package test.com;
>
> > import android.content.Context;
> > import android.graphics.Canvas;
> > import android.graphics.Color;
> > import android.graphics.Paint;
> > import android.graphics.Rect;
> > import android.view.ViewGroup;
>
> > public class Court extends ViewGroup
> > {
> > // court dimensions
> > private int height = 0;
> > private int width = 0;
> > private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
>
> > // ball
> > Ball ball;
>
> > public Court(Context context)
> > {
> > super(context);
> > }
>
> > @Override
> > public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
> > {
> > super.onMeasure(widthMeasureSpec, heightMeasureSpec);
>
> > // fix the sizes here
> > height = this.getMeasuredHeight();
> > width = this.getMeasuredWidth();
> > this.layout(0, 0, width, height);
>
> > // create a new ball
> > ball = new Ball(this, 100, 100, 20, Color.RED);
>
> > // put a ball on the court
> > this.addView(ball);
> > }
>
> > @Override
> > public void onDraw(Canvas canvas)
> > {
> > paint.setColor(Color.RED);
> > Rect rect = new Rect(this.getLeft(), this.getTop(), width,
> > height);
> > canvas.drawRect(rect, paint);
> > // tell the ball to draw itself
> > ball.draw(canvas);
> > }
>
> > @Override
> > protected void onLayout(boolean changed, int l, int t, int r, int b)
> > {
> > // TODO Auto-generated method stub
> > }
>
> > }
>
> > package test.com;
>
> > import android.graphics.Canvas;
> > import android.graphics.Paint;
> > import android.view.MotionEvent;
> > import android.view.View;
>
> > public class Ball extends View
> > {
> > private int x;
> > private int y;
> > private int r;
> > private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
>
> > public Ball(Court court, int x, int y, int r, int color)
> > {
> > super(court.getContext());
> > mPaint.setColor(color);
> > this.x = x;
> > this.y = y;
> > this.r = r;
> > this.layout(this.x, this.y, this.x + (r * 2), this.y + (2 *
> > r));
> > this.setOnTouchListener(this.ballTouchListener);
> > }
>
> > private OnTouchListener ballTouchListener = new OnTouchListener()
> > {
> > @Override
> > public boolean onTouch(View view, MotionEvent event)
> > {
> > // get the location of the click
> > int X = (int)event.getRawX();
> > int Y = (int)event.getRawY();
>
> > Ball ball = (Ball)view;
>
> > // do stuff, depending on what type of touch motion
> > is occurring
> > switch (event.getAction())
> > {
> > case MotionEvent.ACTION_DOWN:
> > break;
> > case MotionEvent.ACTION_MOVE:
> > ball.layout(X, Y, X +
> > ball.getWidth(), Y + ball.getHeight());
> > break;
> > case MotionEvent.ACTION_UP:
> > break;
> > }
> > return true;
> > }
> > };
>
> > @Override
> > protected void onDraw(Canvas canvas)
> > {
> > super.onDraw(canvas);
> > canvas.drawCircle(this.getLeft(), this.getTop(), this.r,
> > mPaint);
> > }
>
> > }- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---