Android的切换ImageView的问题 [英] Android imageView Switching Problem

查看:235
本文介绍了Android的切换ImageView的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ImageView和我在显示不同的图像
随着时间的间隔,但是当我改变ImageResource
显示最后一个图像

i have imageview and i have to display different image in that with time interval, but when i change the ImageResource the last image is displayed

 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);

}

请推荐合适的方式来做到这一点。

Kindly Suggest the Right way to do that

由于提前

推荐答案

每当你想不执行任何动作或事件来更新用户界面,应使用处理器。

Whenever you want to update the user interface without performing any action or event, handlers should be used.

这是样品code

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>

这篇关于Android的切换ImageView的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆