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. Dynamically loading a .jar file at Runtime

Hi all,

  I am storing the required test.jar file in the /sdcard. I want to
load it dynamically at runtime and want to execute a function xyz()
resides in that. For this purpose

  I had written following code ,

But got ClassCastException : dalvik.system.PathClassLoader

Following is my code ,

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Collabera extends Activity {
    /** Called when the activity is first created. */

        private static final Class[] parameters = new Class[] { URL.class };

         public static void loadURLClass(String classPathURL) throws
IOException {

          File f = new File(classPathURL);
          URL url = f.toURL();

          URLClassLoader systemLoader = (URLClassLoader) ClassLoader
            .getSystemClassLoader();
          Class systemLoaderClass = URLClassLoader.class;
          try {
           Method method = systemLoaderClass.getDeclaredMethod("addURL",
             parameters);
           method.setAccessible(true);
           method.invoke(systemLoader, new Object[] { url });
          } catch (Throwable t) {
           t.printStackTrace();
           throw new IOException(
             "Error, could not add URL to system classloader");
          }
         }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       Log.i("See","**************Before Loading Class
Path**************");
     try {
      Class.forName("test.Test1");
     } catch (ClassNotFoundException e) {
      System.out.println(" Test Class Not Found ....");
     }

     Log.i("See","**************After Loading Class
Path**************");

     try {
      loadURLClass("//sdcard//test.jar");
      Class c = Class.forName("test.Test1");
      Log.i("See"," Test Class Found ....");

      Method method = c.getMethod("xyz", null);

      Object o = c.newInstance();
      String s = (String) method.invoke(o);
      Log.i("See","Got method: " + s);
     } catch (ClassNotFoundException e) {
      System.out.println(" Test Class Not Found ....");
     } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }     }
}


Please help. Thanks in advance.

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

2. When another activity is called from main activity, The calling activity get destroyed?

http://developer.android.com/guide/topics/fundamentals.html




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

3. android on omap 35x evm

4. Understand Broadcast and Intent with Dalvik process

5. Changing drawing order while animating

6. need help on "--core-library"

7. Marquee Api demo