0

我想问一下如何实现倒数计时器来加载资产文件夹中的所有图像并根据设置的倒数时间显示它。例如,我的资产文件夹中有 5 张图像,我将它们放入数组中。每隔 5 秒,它将显示每个图像。例如:图像1> 5秒通过>图像2> 5秒通过>图像3等等......

下面是我的代码。请与我分享您的知识或任何实施方式。谢谢。

public class MainActivity extends AppCompatActivity {

private VrPanoramaView mVRPanoramaView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   mVRPanoramaView = (VrPanoramaView) findViewById(R.id.vrPanoramaView);

    loadPhotoSphere();


}

private void loadPhotoSphere() {

    VrPanoramaView.Options options = new VrPanoramaView.Options();
    InputStream inputStream = null;


    AssetManager assetManager=getAssets(); // to reach asset
    try {
        String[] images = assetManager.list("img");// to get all item in img folder.

        options.inputType = VrPanoramaView.Options.TYPE_MONO;

        for (int i = 0; i < images.length; i++) // the loop read all image in img folder 
        {
            inputStream = getAssets().open("img/" + images[i]);

            mVRPanoramaView.loadImageFromBitmap(BitmapFactory.decodeStream(inputStream), options);

        }
        }catch (IOException e) {
            // you can print error or log.
            e.printStackTrace();
        }


}

}

4

1 回答 1

0

解决方案:

您可以使用Handler如下所示的来完成此操作:

Handler h = new Handler();
int delay = 5*1000; //1 second = 1000 milisecond, 5 * 1000 = 5seconds
Runnable runnable;

@Override
protected void onResume() {
   //start handler as activity become visible

    h.postDelayed( runnable = new Runnable() {
        public void run() {

            // do the setting of image here

            h.postDelayed(runnable, delay);
        }
    }, delay);

    super.onResume();
}

@Override
protected void onPause() {
    h.removeCallbacks(runnable); //stop handler when activity not visible
    super.onPause();
}

希望对您有所帮助,如有问题请评论。

于 2018-10-21T16:56:00.823 回答