资产文件夹图片上的倒数计时器

时间:2018-10-21 16:34:07

标签: android countdowntimer google-vr android-assetmanager

我想问一下如何实现倒数计时器,以将所有图像加载到素材资源文件夹并根据设置的倒数时间显示它。例如,我的资产文件夹中有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();
        }


}

}

1 个答案:

答案 0 :(得分: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();
}

希望这会有所帮助,如果有任何问题,请发表评论。