TRY這樣的動畫骰子:
Shake.xml:
<set android:interpolator="@anim/cycle" android:shareinterpolator="true" xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="2000" android:fromxdelta="-5" android:toxdelta="5">
</translate></set>
Dice.xml:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="top|right" android:hapticFeedbackEnabled="true">
<ImageView android:id="@+id/background"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:scaleType="fitXY" />
<ImageButton android:id="@+id/dice" android:clickable="true"
android:onClick="rollDie" android:src="@drawable/dice_sides"
android:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@null" />
</RelativeLayout>
的ImageButton的有安卓背景設置爲@null。這確保了在按鈕周圍沒有邊框。 (基本上,它不會看起來像一個按鈕。)
您應該注意的第二件事是ImageButton的來源稱爲dice_sides。
調用此方法從onClick()
:
public void rollDie(View view) {
// Setup the animation.
Animation shake = AnimationUtils.loadAnimation(view.getContext(),
R.anim.shake);
View dice = findViewById(R.id.dice);
dice.setAnimation(shake);
shake.start();
handler.sendMessageDelayed(Message.obtain(handler, 0, 7), 200);
}
骰子側面的xml:
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0" android:drawable="@drawable/dice_1" />
<item android:maxLevel="1" android:drawable="@drawable/dice_2" />
<item android:maxLevel="2" android:drawable="@drawable/dice_3" />
<item android:maxLevel="3" android:drawable="@drawable/dice_4" />
<item android:maxLevel="4" android:drawable="@drawable/dice_5" />
<item android:maxLevel="5" android:drawable="@drawable/dice_6" />
</level-list>
DiceActivity.java:
// Variables to manage the dice roll.
private Random rnd = new Random();
private DiceRollHandler handler = new DiceRollHandler();
class DiceRollHandler extends Handler {
/**
* @see android.os.Handler#handleMessage(android.os.Message)
*/
public void handleMessage(Message msg) {
int value = rnd.nextInt(6);
ImageView dice = (ImageView) DiceActivity.this
.findViewById(R.id.dice);
dice.setImageLevel(value);
// If there are still rolls available, roll another time.
Integer rollsLeft = (Integer) msg.obj;
if (rollsLeft > 0)
DiceActivity.this.handler.sendMessageDelayed(Message.obtain(
DiceActivity.this.handler, 0, --rollsLeft), 200);
}
}
爲什麼不睡覺()UI線程: 如果您正在調用ui線程上的睡眠,它會阻塞ui線程。不要在ui線程上調用sleep。你不應該阻止ui線程。
http://developer.android.com/reference/java/lang/Thread.html
http://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html
如果實現Thread
或HandlerThread
你會得到ANR
,確保在等待工作線程來完成,不要叫你的UI線程不會阻止Thread.wait()
或Thread.sleep()
。
http://developer.android.com/training/articles/perf-anr.html
還要檢查下如何避免ANR的你正在使用`的Thread.sleep(1500)
的話題;'它休眠1.5secs – 2014-09-19 06:33:00
如果你使用了Thread.Sleep(),那麼你想要做的事情(即顯示滾動圖像)的線程暫停(進入睡眠)一段時間。你想用一個'Timer'在1.5秒內完成其他事情。 – 2014-09-19 06:39:35
看看這個[android中動畫擲骰子的鏈接](http://www.jasoncavett.com/2011/05/changing-images-during-an-android-animation/) – 2014-09-19 06:40:01