0

我期待在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); 
} 

我正在尋找的東西,做一些事情類似於這樣做,但允許我將文本添加到圈子並使其可點擊

有沒有什麼辦法可以做到這一點?

感謝

+0

單擊此視圖應該像任何其他視圖一樣工作。你只需要添加一個onClickListener –

+0

我已經嘗試添加一個onClickListener,但它返回一個錯誤'無法解析onClickListener方法' – ShWhite

+0

你可以發佈代碼,你試圖做到這一點。 –

回答

1

可以使用的drawText方法畫布繪製文本:

public void drawText (CharSequence text, int start, int end, float x, float y, Paint paint) 

事情是這樣的:

Paint paint = new Paint(); 
canvas.drawPaint(paint); 
paint.setColor(Color.MY_COLOR); 
paint.setTextSize(24); 
canvas.drawText("My Text", x, y, paint); 

要點擊你只需要一個onClickListener添加到視圖它:

 myAnimatedWordsView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

      } 
     }); 
+0

這行代碼如下:'AnimatedWordsView.setOnClickListener(new View.OnClickListener()'調用onClickListener同樣無法解析上面的方法 – ShWhite

+0

它不是一個靜態方法,你不能這樣調用它,你需要創建一個AnimatedWordsView實例,那麼你可以在實例上調用setOnClickListener –

+0

如果你有你的xml,你需要做類似下面的事情:AnimatedWordsView myAnimWordsView =(AnimatedWordsView)findViewById(R.id.my_view); –