android - 屏幕 ImageView 中的替代图像

标签 android android-asynctask imageview android-imageview

我的可绘制文件夹中有两张图片,我希望每隔 x 次在我的 View 中交替显示这两张图片。 我尝试使用 Asynctask 但没有用,而且我找不到解决方案。

我的 xml 代码

<ImageView
    android:id="@+id/imageload"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="64dp"
    android:adjustViewBounds="false"
    android:baselineAlignBottom="false"
    android:contentDescription="@string/imatge"
    android:cropToPadding="false"
    android:fitsSystemWindows="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:src="@drawable/hdtitol2" />

我给类(class)打电话:

new ModifyImage().execute(null,null,null);

主类包含带有异步代码的类

public class ModifyImage extends AsyncTask<Void, Void, Void> {

    ImageView img= (ImageView)findViewById(R.id.imageload);

    @Override
    protected void onPreExecute(){

    }
    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        int i = 0;
        boolean opt = true;
        boolean exit = false;
        while(!exit){
            if(i == 1000){
                i = 0;
                if(!opt){
                    img.setImageResource(R.drawable.blackhdtitol2);
                    opt =true;
                }else{
                    opt = false;
                    img.setImageResource(R.drawable.hdtitol2);
                }
            }
            i++;
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void i){

    }

}

最佳答案

这样做,

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        Integer tag = (Integer) img.getTag();
        if(tag == R.drawable.blackhdtitol2){
                img.setImageResource(R.drawable.blackhdtitol2);
                img.setTag(R.drawable.blackhdtitol2);
            }else{
                img.setImageResource(R.drawable.hdtitol2);
                img.setTag(R.drawable.hdtitol2);
            }
    }
}, 60*1000);

关于android - 屏幕 ImageView 中的替代图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16377739/

相关文章:

c# - 使用 32Feet 查找配对的蓝牙设备是否在范围内

php - Mysql数据加密

android - 我可以在一个 Activity 中有两个 AsyncTask 类吗(不同时工作)

android - 在分离的 fragment 中获取上下文/Activity ?

java - AsyncTask,多种不同的操作

java - 从另一个 fragment 单击按钮时更改图像

android - 只有创建 View 层次结构的原始线程才能触及它的 View

android - 使用intelliJ在android中调试后台服务

Android getExternalCacheDir() 返回 null

android - 指定的任务可执行位置...无效