我希望我的動畫在從圖片庫中選取圖片後啓動。我以爲我可以用隨機選擇開關來做到這一點,但是我得到一個錯誤,因爲'球'無法解決。我希望這不是完全錯了,但我不知道如何解決它既不android - 帶隨機圖片的動畫
protected void onDraw(Canvas c) {
Random randLan = new Random();
int imag = randLan.nextInt(4) + 1;
int image = 0;
switch (imag)
{
case 1:
image = R.drawable.ballgel;
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballgel);
break;
case 2:
image = R.drawable.ballgel;
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballbl);
break;
case 3:
image = R.drawable.ballgel;
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballgrue);
break;
case 4:
image = R.drawable.ballgel;
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ballrot);
break;
}
if (x<0) {
x = this.getWidth()/2;
y = this.getHeight()/3;
}
else {
x += xVelocity;
if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
boolean continueAnimation = false;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
if(continueAnimation)
{
h.postDelayed(r, FRAME_RATE);
}
else {
x = this.getWidth()-ball.getBitmap().getWidth();
}
}
你試過了嗎'BitmapDrawable ball'你的開關盒外面? –
當我把它放在開關櫃外時,'(R.drawable。...)的最後部分是什麼;'然後?這應該是變量,但我認爲它需要一個明確的參考,不是嗎? –