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. ANN: DroidEx Android device projector, version 0.0.0.0.0.1

In response to the "[android-developers] Re: Rendering the G1 screen on
a computer screen  via VNC software?" thread, I tossed together a quick
and very dirty DroidEx.

DroidEx provides a window with a constantly-refreshing view of a device
connected to the host computer. You can use this for demonstrating
software that requires a G1/ADP1 instead of an emulator.

To get it, download the 3K JAR file from:

 http://www.***.com/ 

To run it, you need to put it and the ddmslib.jar from your SDK tools/
folder on the classpath and invoke com.commonsware.droidex.DroidEx, such as:

java -cp
DroidEx.jar:/opt/android-sdk-linux_x86-1.0_r2/tools/lib/ddmlib.jar
com.commonsware.droidex.DroidEx

It also requires that the adb daemon be running, such as a development
PC/Mac.

It assumes there is precisely one device plugged in or emulator running.
All it does is open a window showing you the contents of that device's
or emulator's screen. It refreshes ~6 times/second, so it's not the
smoothest thing going, but at that speed it doesn't seem to drag the G1
down very much.

It also suffers from the following:

-- The screen-capture logic in Android proper seems to occasionally miss
updates for some reason. I've seen this with DDMS and hierarchyviewer
too, so I'm assuming it's something in the Android firmware. Hence,
sometimes DroidEx will appear to be a movement behind (e.g., you clicked
up in a list, and DroidEx still shows the previous one as the selected item)

-- Once, adb or the device seemed to reject DroidEx part-way through a
projection session, causing the image to freeze on DroidEx's window and
a bunch of error messages to be logged to stderr. Closing and reopening
DroidEx was sufficient to clear up this condition.

Also, this was compiled against 1.0r2's version of ddmslib.jar, so it
may or may not work with other versions of the SDK.

It is far from perfect, but it may be better than pointing a {*filter*} at a
device for device-based demos. However, as the saying goes, "your
mileage may vary". I disclaim all warranties, will not be responsible if
your use of DroidEx rips a hole in the space-time continuum, etc.

If there's sufficient interest, and I have time, I'll clean up the
extremely ugly code (~100 lines) and post it somewhere.

If you have questions or comments regarding DroidEx, head over to the
CW-Android Google Group, and post 'em there:

 http://www.***.com/ 

Or, reply to this message.

-- 
Mark Murphy (a Commons Guy)
 http://www.***.com/ 
_The Busy Coder's Guide to Android Development_ Version 2.0 Published!

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

2. Need help in running the android junit test case for andriod APIDemo in eclipse

Hi,

I am trying to run Junit test case for the android APIDemo project
under eclipse.

I create an eclipse project from the APIDemo source, it compiles fine
and then I did:
1. Debug->Run as Junit tests

But I get this error:
'Lanuching AllTests' has encountered a problem.
Cannot connect to VM.

And I find this in the .log file in eclipse:
!ENTRY org.eclipse.core.net 1 0 2009-01-28 15:03:52.799
!MESSAGE System property http.nonProxyHosts has been set to
local|*.local|169.25
4/16|*.169.254/16 by an external source. This value will be
overwritten
using th
e values from the preferences

!ENTRY org.eclipse.jdt.launching 4 120 2009-01-28 15:04:03.269
!MESSAGE Cannot connect to VM
!STACK 0
java.net.SocketException: Socket closed
        at java.net.PlainSocketImpl.socketAccept(Native Method)
        at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:384)
        at java.net.ServerSocket.implAccept(ServerSocket.java:450)
        at java.net.ServerSocket.accept(ServerSocket.java:421)
        at
org.eclipse.jdi.internal.connect.SocketTransportService.accept(Socket
TransportService.java:95)
        at
org.eclipse.jdi.internal.connect.SocketTransportImpl.accept(SocketTra
nsportImpl.java:56)
        at
org.eclipse.jdi.internal.connect.SocketListeningConnectorImpl.accept(
SocketListeningConnectorImpl.java:135)
        at
org.eclipse.jdt.internal.launching.StandardVMDe{*filter*}$ConnectRunnable
run(StandardVMDe{*filter*}.java:107)
        at java.lang.Thread.run(Thread.java:613)

!ENTRY org.eclipse.jdt.launching 4 120 2009-01-28 15:23:53.932
!MESSAGE Cannot connect to VM
!STACK 0
java.net.SocketException: Socket closed
        at java.net.PlainSocketImpl.socketAccept(Native Method)
        at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:384)
        at java.net.ServerSocket.implAccept(ServerSocket.java:450)
        at java.net.ServerSocket.accept(ServerSocket.java:421)
        at
org.eclipse.jdi.internal.connect.SocketTransportService.accept(Socket
TransportService.java:95)
        at
org.eclipse.jdi.internal.connect.SocketTransportImpl.accept(SocketTra
nsportImpl.java:56)
        at
org.eclipse.jdi.internal.connect.SocketListeningConnectorImpl.accept(
SocketListeningConnectorImpl.java:135)
        at
org.eclipse.jdt.internal.launching.StandardVMDe{*filter*}$ConnectRunnable
run(StandardVMDe{*filter*}.java:107)
        at java.lang.Thread.run(Thread.java:613)


Thank you for any help.



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

3. Rendering the G1 screen on a computer screen via VNC software?

4. Install Android on Windows/Ubuntu Possible?

5. Problems in running ADT on eclipse on Mac

6. How to run the 'AllTests' of ApiDemos in the android sdk sample

7. Difference between kernel on git and kernel on code.google.com