ImageView的变化不会太快

时间:2014-09-19 06:32:17

标签: java android imageview thread-sleep

我正在尝试制作一个骰子应用程序,当单击一个按钮时,“滚动”图像显示一段时间,然后切换到骰子结果。我的代码只是跳过滚动图像并等待上次结果的休眠时间,然后立即更改为新结果。知道什么是错的吗?

    public void on click(View arg0)
    {
    image.setImageResource(R.drawable.dice_rolling);

    try{
         Thread.sleep(1500);
    }  catch (InterruptedException e) {
         e.printStackTrace();
    }

    image.setImageResource(R.drawable.dice_result);

1 个答案:

答案 0 :(得分:1)

尝试以这种方式制作动画骰子:

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>
  1. ImageButton将android:background设置为@null。这可确保按钮周围不会绘制边框。 (基本上,它看起来不像一个按钮。)

  2. 你应该注意的第二件事是ImageButton的来源叫做dice_sides。

  3. 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);
     }
    

    Dice Sides 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 &gt; 0)
       DiceActivity.this.handler.sendMessageDelayed(Message.obtain(
         DiceActivity.this.handler, 0, --rollsLeft), 200);
     }
    }
    

    为什么不睡觉()UI线程 如果你在ui线程上调用sleep,它会阻塞ui线程。不要在ui线程上调用sleep。你不应该阻止ui线程。

    http://developer.android.com/reference/java/lang/Thread.html

    http://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html

    你会得到ANR

    如果您实施ThreadHandlerThread,请确保您的UI线程在等待工作线程完成时不会阻止 - 请勿调用Thread.wait()Thread.sleep()

    http://developer.android.com/training/articles/perf-anr.html

    另请查看如何避免ANR

    下的主题