我有一个具有图像 1 和一个按钮的图像视图。我需要按下按钮将图像源更改为 image2 1 秒,然后设置 image1 。我是java和android的初学者,有人可以帮忙吗?
问问题
1877 次
3 回答
2
你可以使用Handler
这个:
Button button1 = (Button) findViewById(R.id.myButtonId);
ImageView imageView = (ImageView) findViewById(R.id.myImageViewId);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.image1)); // set image1
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.image2)); // set image2
new Handler().postDelayed(new Runnable() {
public void run() {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.image1)); // switch back to image1
}, 1000L); // 1 second delay | change this value to shorten/lengthen delay
}
});
于 2013-07-20T11:31:53.667 回答
1
试试这个代码。
private Handler handler = new Handler();
private Runnable runable = new Runnable() {
@Override
public void run() {
// set thr image to image1
}
};
private void onButtonClickHandler() {
// set the image to image2
//remove
handler.removeCallbacks(runable);
// post a delay
handler.postDelayed(runable, 1000);
}
或者:
private void onButtonClickHandler() {
// set the image to image2
//cancel
timer.cancel();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
}
}, 1000);
}
private Timer timer = new Timer();
于 2013-07-20T10:50:20.787 回答
0
imageView.setImage(img2);
Thread.sleep(1000);
imageView.setImage(img1);
于 2013-07-20T11:10:02.190 回答