【问题标题】:Android imageView Switching ProblemAndroid imageView切换问题
【发布时间】:2011-03-03 04:09:09
【问题描述】:

我有 imageview,我必须在其中显示不同的图像 有时间间隔,但是当我更改 ImageResource 显示最后一张图片

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    ImageView image = (ImageView) findViewById(R.id.test_image);
    image.setImageResource(R.drawable.test);

  try {
        Thread.sleep(2000) ;
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.test2);

}

请提出正确的方法

提前致谢

【问题讨论】:

  • 在 UI 线程中使用 Thread.sleep() 真的很糟糕......而且你从不接受答案也很糟糕

标签: android imageview


【解决方案1】:

也许这个帖子就是你要找的:

【讨论】:

    【解决方案2】:

    当您想要更新用户界面而不执行任何操作或事件时,应使用处理程序。

    这是示例代码

    Main.java

    package com.balaji.handler;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.widget.ImageView;
    import android.widget.TextView;
    public class Main extends Activity  { 
    
        private ImageView txtStatus;
        int i=0;
        int imgid[]={R.drawable.icon,R.drawable.back,R.drawable.slider,R.drawable.forward};
        private RefreshHandler mRedrawHandler = new RefreshHandler();
    
        class RefreshHandler extends Handler {
    
            @Override
            public void handleMessage(Message msg) {
                Main.this.updateUI();
            }
    
            public void sleep(long delayMillis) {
                this.removeMessages(0);
                sendMessageDelayed(obtainMessage(0), delayMillis);
            }
    
        };
    
        private void updateUI(){
            //int currentInt = Integer.parseInt((String) txtStatus.getText()) + 10;
            if(i<imgid.length){
            mRedrawHandler.sleep(1000);
            txtStatus.setBackgroundResource(imgid[i]);
            i++;
        }
    }
    
    @Override 
    public void onCreate(Bundle icicle) { 
        super.onCreate(icicle); 
        setContentView(R.layout.main);
        this.txtStatus = (ImageView)this.findViewById(R.id.txtStatus);
        updateUI();
    }   
    }
    

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
    <ImageView 
        android:id="@+id/txtStatus" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" 
        android:layout_centerHorizontal="true">
    </ImageView>
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多