Adding a view to a viewgroup

by Carl » Tue, 07 Jul 2009 20:31:13 GMT


Sponsored Links
 '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
publ



Adding a view to a viewgroup

by Jack Ha » Wed, 08 Jul 2009 00:17:29 GMT


 our 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
obilestick 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:30am, Carl <carl...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Adding a view to a viewgroup

by Carl » Wed, 08 Jul 2009 06:33:35 GMT


 xcellent, thanks.

On 7 July, 17:17, Jack Ha <jack...@t-mobile.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



Adding a view to a viewgroup

by Romain Guy » Wed, 08 Jul 2009 06:48:35 GMT


 lso, you should not call  this.layout(0, 0, width, height) from
onMeasure(), it will be done for you.

On Tue, Jul 7, 2009 at 3:33 PM, Carl<carl...@gmail.com> wrote:



--
Romain Guy
Android framework engineer
romain...@android.com

Note: please don't send private questions to me, as I don't have time
to provide private support. All such questions should be posted on
public forums, where I and others can see and answer them

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Google Nexus One FAQ

Google Nexus One FAQ

http://bit.ly/7siLac 

Sent from Motorola Milestone
twitter : @jerbid

2. login

I cannot find the webpage to login in android-porting group now. Any one can
tell me where to login?

3. Porting to Andriod

4. Rookie Rootie

5. Default home screen widgets

6. Led notification on Motorola Milestone

7. kill running-app