1

我是 android 编程的新手,我搜索了关于这个主题的所有答案,但我仍然无法实现我想做的事情。我的问题是:我想在给定的时间段内用另一张图片更新 imageview 的图片。imageview每次都需要更新不同的图片,总共15-20次。这是我到目前为止所做的,但它根本不起作用。

public class IlkMasal extends Activity {

    MediaPlayer sound;
    private Handler m_handler;
    private ImageView image;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firsttale);
        m_handler = new Handler();


    Button menu = (Button)findViewById(R.id.Button01);
    Button startbutton = (Button)findViewById(R.id.button1);

    sound = MediaPlayer.create(this, R.raw.music4);
    sound.start();
    image = (ImageView)findViewById(R.id.imageView1);
    image.setImageResource(R.drawable.picture2);

    startbutton.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            m_handler.removeCallbacks(m_statusChecker);
            m_handler.postDelayed(m_statusChecker, 2000);
        }
    });

    menu.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Intent main= new Intent("android.intent.action.MAIN");
            startActivity(main);
        }  
    });

    }

    Runnable m_statusChecker = new Runnable()
    {
        public void run(){
            image.setImageResource(R.drawable.picture3);
            m_handler.removeCallbacks(m_statusChecker);
            m_handler.postDelayed(m_statusChecker, 2000);
        }
    };
}

单击开始按钮后,我想每次都用不同的图片更新 ui。我会感谢你的帮助。

4

2 回答 2

2

Yo can set this runnable to run on your onClic and you will still need a method to get the next picture you want to show.

final int delay = 5000;
final int period = 1000;
final Runnable r = new Runnable() {
   public void run() {
    image.setImageResource(getNextPicture());
    postDelayed(this, period);
   }
 };

postDelayed(r, delay);

Regards.

于 2012-07-11T08:04:14.410 回答
0

使用runOnUiThread从非 Ui 线程更新 UI 元素:

    Current_Activity.this.runOnUiThread(new Runnable() {

     @Override
     public void run() {
      // TODO Auto-generated method stub
      image.setImageResource(R.drawable.picture3); //Update UI elements here
     }
    });
于 2012-07-11T07:58:51.130 回答