如何从代码中使用imageview更新图像

ymdaylpp  于 2021-07-07  发布在  Java
关注(0)|答案(4)|浏览(362)

我正在尝试制作一个能够连续显示图像(每秒4或5次)的应用程序,因此我需要更新保存在imageview对象中的图像,如果我通过按下按钮触发功能,它就会工作,每次按下按钮,下一个图像就会显示:

int i = 0;
public void buttonPressed(View view) {
    ImageView image = (ImageView) findViewById(R.id.imageView5);
    String path = String.format("/storage/emulated/0/Download/%d.bmp", i);
    File imgFile = new File(path);
    bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    image.setImageBitmap(bMap);
    i++;
    }

但是,如果我尝试在同一个父函数中多次调用相同的函数,或者尝试在循环中运行它们,则在loopfunc()完成后,将根本不加载第一个图像,而最终只显示最后一个图像:

public void loopFunc() {
ImageView image = (ImageView) findViewById(R.id.imageView5);
 for (i = 1; i < 3; i++) {
    String path = String.format("/storage/emulated/0/Download/%d.bmp", i);
    File imgFile = new File(path);
    bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    image.setImageBitmap(bMap);
    //wait some time or do other things...
    }
}

简单地说,我需要能够执行 image.setImageBitmap(bMap); 从代码而不是从按钮有人知道怎么做?谢谢

ryhaxcpt

ryhaxcpt1#

当您在循环中更改图像路径时,到达最后一个路径只需几毫秒。您需要使用sleep()函数来停留一段时间。

public void showImage()
{
    Thread thread=new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                for (int i = 1; i < 3; i++) {
                String path = String.format("/storage/emulated/0/Download/%d.bmp", i);
                File imgFile = new File(path);
                bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                image.setImageBitmap(bMap);
        }
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
}
rvpgvaaj

rvpgvaaj2#

根本原因
因为循环在很短的时间内运行得太快,所以您看到的第一个图像根本不显示,而只有最后一个图像在之后才最终显示 loopFunc() 完整的。
解决方案
若要每秒连续显示一系列图像,则应使用处理程序。

public void loopFunc() {
    Handler mainHandler = new Handler(Looper.getMainLooper());

    int totalImage = 3;
    for (int i = 1; i < totalImage; i++) {
        int imageIndex = i;
        mainHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                String path = String.format("/storage/emulated/0/Download/%d.bmp", imageIndex);
                File imgFile = new File(path);
                bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                image.setImageBitmap(bMap);
            }
        }, 1000 / totalImage * imageIndex);
    }
}
ig9co6j1

ig9co6j13#

问题在于你用来计算其他东西的时间。里面有你写的延迟函数代码吗。

qoefvg9y

qoefvg9y4#

试试这个。只需创建一个函数,并使用某个线程或处理程序调用oncreate方法。

public void imgViwer()
    {
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (i = 1; i < 3; i++) {
                    String path = String.format("/storage/emulated/0/Download/%d.bmp", i);
                    File imgFile = new File(path);
                    bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                    image.setImageBitmap(bMap);
}
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

每200米后,你的循环就开始工作。

相关问题