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 스레드에서 sleep을 호출하는 경우 UI 스레드를 차단합니다. UI 스레드에서 절전 모드를 호출하지 마십시오. UI 스레드를 차단하면 안됩니다.
http://developer.android.com/reference/java/lang/Thread.html
http://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html
당신은 당신이 Thread
또는 HandlerThread
을 구현하는 경우, ANR
을 얻을 완료-DO 호출 할 수있는 작업자 스레드를 기다리는 동안 UI 스레드가 차단되지 않도록 할 것 Thread.wait()
또는 Thread.sleep()
.
http://developer.android.com/training/articles/perf-anr.html
또한 ANR과 당신이`(1500)에 Thread.sleep를 사용하는
을 방지하는 방법 아래의 항목을 확인,'는 1.5secs –
동안 휴면 당신은에 Thread.sleep()를 사용하는 경우, 다음 스레드는 일정 기간 (예 : 롤링 이미지 표시) 일시 중지 (절전 모드로 전환)를 수행하려고합니다. 1.5 초 안에 뭔가 다른 것을 만들기 위해'타이머 '를 사용하고 싶습니다. –
[이것 저것 안드로이드에서 주사위 굴리기의 애니메이션 링크] (http://www.jasoncavett.com/2011/05/changing-images-during-an-android-animation/)를보십시오. –