Android-几秒钟后在运行时更改图像视图源 [英] Android - Change the Image View Source at run time after few seconds

查看:76
本文介绍了Android-几秒钟后在运行时更改图像视图源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码用于我的应用程序中的启动屏幕.我需要做的是,当活动加载时,图像应显示为默认值,然后在几秒钟后,应将图像更改为同一活动中的另一幅图像.我有另一个图像,例如第一个具有不同颜色的图像.我想在几秒钟后更改该图像的屏幕.

The following code is for a splash screen in my app.What I need to be done is when the activity loads the image should be displayed as default and then after few seconds the image should be changed to another in the same activity. I have another image like the first one with different color. I wanted to change the screen for that image after few seconds.

我已经在我的代码中这样做了.

I have done it like this in my code.

package com.ruchira.busguru;

import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class SplashScreen extends Activity {

    ImageView imgBus;
    MediaPlayer introSound, bellSound;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        imgBus = (ImageView) findViewById(R.id.imgBus);
        imgBus.setImageResource(R.drawable.blackbus);
        introSound = MediaPlayer.create(SplashScreen.this, R.raw.enginestart);
        introSound.start();

                Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(3000);
                } catch (InterruptedException e) {              
                    e.printStackTrace();
                }finally{
                   imgBus.setImageResource(R.drawable.bluekbus);    
                }
            }
        };
        timer.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.splash_screen, menu);
        return true;
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        introSound.stop();
        finish();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        introSound.stop();
        finish();
    }
}

但是问题是程序通过在线程中执行......来停止程序.

but the problem is the program stops by executing in the thread saying...

"android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图."

"android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views."

我如何实现这个目标?有人能帮我解决这个问题吗?我是android开发的新手..

How I can achieve this target ? Does anybody please help me to sort out this problem. I am new to android development..

谢谢.

推荐答案

您应该使用ImageView的Handler和Runnable.处理程序是特定于Android的调度程序,它们可以在UI线程上运行.试试这个:

You should use the ImageView's Handler and a Runnable. Handler's are schedulers specific to Android and they can run on the UI thread. Try this:

ImageView imgBus;
MediaPlayer introSound, bellSound;
Runnable swapImage = new Runnable() {
    @Override
    public void run() {
        imgBus.setImageResource(R.drawable.bluekbus);
    }
};

onCreate()调用中:

imgBus = (ImageView) findViewById(R.id.imgBus);
imgBus.setImageResource(R.drawable.blackbus);
imgBus.postDelayed(swapImage, 3000);  // Add me!

请理解启动屏幕令人厌烦,因为您应该集中精力尽快启动应用程序(在后台加载较慢的元素时).但是有时不可避免地会出现轻微的延迟.

Understand that splash screens are frowned upon because you should focus on starting the app as soon as possible (while loading the slower element in the background). However sometimes a slight delay is unavoidable.

这篇关于Android-几秒钟后在运行时更改图像视图源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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