在imageview中设置图像一段时间

时间:2013-07-20 10:37:28

标签: android imageview

我有一个包含图像1的图像视图和一个按钮。 我需要按下按钮才能将图像源更改为image2 1秒钟,然后设置image1。 我是java和android的初学者,有人可以帮忙吗?

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:1)

您可以使用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
    }
});

答案 2 :(得分:0)

imageView.setImage(img2);
Thread.sleep(1000);
imageView.setImage(img1);