Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Android] Erreur d'affichage

Créer un ViewGroup ?

12 février 2012 à 1:42:05

Bonjour à tous,

J'ai un problème d'affichage de View personnalisées, lors d'un code test pouvant ressembler au jeu Pong:

J'ai créé une View "Balle" (code à la fin du message), et je n'arrive pas à en afficher plus d'une sur l'écran.
Faut-il créer un viewGroup personnaliser pour afficher ceci ? Comment faire ?

J'ajoute à la fin le code de mon activity, et le code xml.
Merci d'avance pour toute réponse :)

Mon code xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <sdz.test.Balle
        android:id="@+id/balle"
        android:layout_width="wrap_content"
   		android:layout_height="wrap_content"
    />
    
    <sdz.test.Balle
        android:id="@+id/balle2"
        android:layout_width="wrap_content"
   		android:layout_height="wrap_content"
    />

</LinearLayout>


Mon activity :
package sdz.test;


import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import java.util.Timer;
import java.util.TimerTask;



public class TestJeuActivity extends Activity {
	
	private Timer timer = null;
	private Balle balle = null;
	private Balle balle2 = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Pour cacher la barre de titre
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        //setContentView(R.layout.ecranjeu);
        // Pour cacher la barre de statut et donc mettre votre application en plein écran
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        
        class TTask extends TimerTask {
    		public void run() {
    			runOnUiThread(new Runnable() {
    				public void run() {
    					balle = ((Balle)findViewById(R.id.balle));
    					balle2 = ((Balle)findViewById(R.id.balle2));
    					
    					Display ecran = getWindowManager().getDefaultDisplay(); 
    					int largeur= ecran.getWidth();
    					int hauteur= ecran.getHeight();
    					
    					balle.move(largeur, hauteur);
    					balle2.move(largeur, hauteur);
    				}
    			});
    		}
    	}
        
        timer = new Timer();
        timer.schedule(new TTask(), 1000, 10);
        
        setContentView(R.layout.main);
        
    }
    
}


Class Balle :
package sdz.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class Balle extends View {
	
	private Bitmap image;
	private int imageSizeI = 5;
	
	private float posStartX;
	private float posStartY;
	
	private float posX;
	private float posY;
	private float vitesseX = 5;
	private float vitesseY = 5;
	
	public Balle(Context context, AttributeSet attrs){
		super(context, attrs);
		image = prepareBitmap(getResources().getDrawable(R.drawable.balle), imageSizeI, imageSizeI);
		resetView();
	}
	
	private static Bitmap prepareBitmap(Drawable drawable, int width, int height){
		Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		drawable.setBounds(0, 0, width, height);
		Canvas canvas = new Canvas(bitmap);
		drawable.draw(canvas);
		return bitmap;
	}
	
	@Override
	public void onDraw(Canvas canvas){
		canvas.drawBitmap(image, posX, posY, null);
	}
	
	public void resetView() {
		posStartX = 60;
		posStartY = 100;
	    posX = posStartX;
	    posY = posStartY;
	}
	
	public void move(int largeur, int hauteur){
		if(posX<0 || posX>largeur-15) {
			vitesseX = -vitesseX;
		}
		if(posY<0 || posY>hauteur-100){
			vitesseY = -vitesseY;
		}
		posX += vitesseX;
		posY += vitesseY;
		invalidate();
	}
	
	public float getX(){
		return posX;
	}
	
	public float getY(){
		return posY;
	}
}


  • Partager sur Facebook
  • Partager sur Twitter