我期待在Android中創建隨機移動可點擊的按鈕(用於兒童遊戲)。 我跟着這個網站上的代碼,屏幕 http://www.techrepublic.com/blog/software-engineer/bouncing-a-ball-on-androids-canvas/在Android上製作一個隨機移動的可點擊按鈕
與動畫的話周圍創造出一些隨機移動圈子視圖更改爲此:
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);
}
我正在尋找的東西,做一些事情類似於這樣做,但允許我將文本添加到圈子並使其可點擊
有沒有什麼辦法可以做到這一點?
感謝
單擊此視圖應該像任何其他視圖一樣工作。你只需要添加一個onClickListener –
我已經嘗試添加一個onClickListener,但它返回一個錯誤'無法解析onClickListener方法' – ShWhite
你可以發佈代碼,你試圖做到這一點。 –