Je cherche à créer au hasard cliquable boutons dans Android (pour un jeu d'enfants). J'ai suivi le code sur ce site pour créer des cercles mobiles autour de l'écran http://www.techrepublic.com/blog/software-engineer/bouncing-a-ball-on-androids-canvas/Faire un bouton cliquable se déplaçant au hasard sur Android
avec les mots animés vue changé à ce hasard:
public class AnimatedWordsView extends ImageView {
private Context myContext;
int [] xCoOrd = {-1, -2, -3, -4, -5, -6, -7, -8};
int [] yCoOrd = {-1, -2, -3, -4, -5, -6, -7, -8};
int [] xVeloc = {4, 8, 12, 16, 20, 20, 20, 20};
int [] yVeloc = {2, 4, 6, 8, 10, 12, 14, 16};
private Handler handler;
private final int FRAME_RATE = 30;
public AnimatedWordsView(Context context, AttributeSet attributes){
super(context, attributes);
myContext = context;
handler = new Handler();
}
private Runnable run = new Runnable() {
@Override
public void run() {
invalidate();
}
};
protected void onDraw(Canvas canvas){
BitmapDrawable [] word = {(BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_blue), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_green),
(BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_red), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_yellow),
(BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_green), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_blue),
(BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_red), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_yellow)};
for(int count = 0; count <=7; count++) {
if (xCoOrd[count] < 0 && yCoOrd[count] < 0) {
xCoOrd[count] = this.getWidth()/2;
yCoOrd[count] = this.getHeight()/2;
} else {
xCoOrd[count] += xVeloc[count];
yCoOrd[count] += yVeloc[count];
}
if ((xCoOrd[count] > this.getWidth() - word[count].getBitmap().getWidth()) || (xCoOrd[count] < 0)) {
xVeloc[count] = xVeloc[count] * -1;
}
if ((yCoOrd[count] > this.getHeight() - word[count].getBitmap().getHeight()) || (yCoOrd[count] < 0)) {
yVeloc[count] = yVeloc[count] * -1;
}
canvas.drawBitmap(word[count].getBitmap(),xCoOrd[count],yCoOrd[count],null);
}
handler.postDelayed(run, FRAME_RATE);
}
Qu'est-ce que je cherche quelque chose qui fait quelque chose semblable à ce que cela fait, mais me permet d'ajouter du texte dans les cercles et de les rendre cliquables
Y at-il un moyen de le faire?
Merci
Cliquer sur cette vue devrait fonctionner comme n'importe quelle autre vue. Vous avez juste besoin d'ajouter un onClickListener –
J'ai essayé d'ajouter un onClickListener, mais il retourne une erreur 'impossible de résoudre la méthode onClickListener' – ShWhite
Pouvez-vous poster le code où vous essayiez de faire cela. –