Hi to all, i'm new for Android, i want to create a simple application that move an image to the screen, i read in many forums and i found the way to do that, i use a custom view in my activity:
private static final String TAG = DroidzActivity.class.getSimpleName(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); Log.d(TAG, "View added"); } @Override protected void onDestroy() { Log.d(TAG, "Destroying..."); super.onDestroy(); } @Override protected void onStop() { Log.d(TAG, "Stopping..."); super.onStop(); } public void onClick(View v) { } } the main.xml is: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1"> <Button android:text="Restart" android:id="@+id/buttonRestart" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <TextView android:text="-" android:layout_width="wrap_content" android:id="@+id/textViewPunteggio" android:textAppearance="? android:attr/textAppearanceLarge" android:layout_height="wrap_content"></TextView> </LinearLayout> <net.obviam.droidz.MainGamePanel android:layout_height="match_parent" android:id="@+id/mainGamePanel1" android:layout_width="323dp"></net.obviam.droidz.MainGamePanel> </LinearLayout> and my custom view is: public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback { private static final String TAG = MainGamePanel.class.getSimpleName(); private MainThread thread; private Droid droid; public MainGamePanel(Context context, AttributeSet attr) { super(context,attr); // adding the callback (this) to the surface holder to intercept events getHolder().addCallback(this); // create droid and load bitmap droid = new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.chicco), 50, 50); // create the game loop thread thread = new MainThread(getHolder(), this); setFocusable(true); } public Droid getDroid() { return droid; } public void setDroid(Droid droid) { this.droid = droid; } public void render(Canvas canvas) { canvas.drawColor(Color.BLACK); droid.draw(canvas); } public void update() { // check collision with right wall if heading right if (droid.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT && droid.getX() + droid.getBitmap().getWidth() / 4 >= getWidth()) { droid.getSpeed().toggleXDirection(); } // check collision with left wall if heading left if (droid.getSpeed().getxDirection() == Speed.DIRECTION_LEFT && droid.getX() - droid.getBitmap().getWidth() / 4 <= 0) { droid.getSpeed().toggleXDirection(); } // check collision with bottom wall if heading down if (droid.getSpeed().getyDirection() == Speed.DIRECTION_DOWN && droid.getY() + droid.getBitmap().getHeight() / 4 >= getHeight()) { droid.getSpeed().toggleYDirection(); } // check collision with top wall if heading up if (droid.getSpeed().getyDirection() == Speed.DIRECTION_UP && droid.getY() - droid.getBitmap().getHeight() / 4 <= 0) { droid.getSpeed().toggleYDirection(); } // Update the lone droid droid.update(); } public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) { } public void surfaceCreated(SurfaceHolder holder) { // at this point the surface is created and // we can safely start the game loop thread.setRunning(true); thread.start(); } public void surfaceDestroyed(SurfaceHolder holder) { Log.d(TAG, "Surface is being destroyed"); // tell the thread to shut down and wait for it to finish // this is a clean shutdown boolean retry = true; while (retry) { try { thread.join(); retry = false; } catch (InterruptedException e) { // try again shutting down the thread } } Log.d(TAG, "Thread was shut down cleanly"); } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { // delegating event handling to the droid droid.handleActionDown((int) event.getX(), (int) event.getY()); if (droid.isTouched()) { score++; Log.d(TAG, "Punteggio:" + score); } } if (event.getAction() == MotionEvent.ACTION_MOVE) { // Questo lo utilizzi solo in caso di drag dell dito (trascina) // if (droid.isTouched()) { // droid.setX((int)event.getX()); // droid.setY((int)event.getY()); // } } if (event.getAction() == MotionEvent.ACTION_UP) { // touch was released if (droid.isTouched()) { droid.setTouched(false); } } return true; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } @Override protected void onDraw(Canvas canvas) { // fills the canvas with black canvas.drawColor(Color.BLACK); droid.draw(canvas); } } at this point what is the way to modify the object in main.xml? es: if i want setText of the label "textViewPunteggio" only when the image is touched ? i try findViewById(R.id.textViewPunteggio) but it result null, probably because i am in the subView. Any help is appreciated. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en