如何在特定时间设置视图可见?

How to set a View Visible on a specific time?

我正在尝试制作一个视频播放器 android 应用程序,我必须在特定时间在视频顶部显示一个 ImageButton。 这是我的布局

<FrameLayout
                android:id="@+id/videoSurfaceContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <SurfaceView
                    android:id="@+id/videoSurface"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout
                    android:id="@+id/thumbnail"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical|end"
                    android:orientation="vertical"
                    android:visibility="gone" >

                    <ImageButton
                        android:id="@+id/imageButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/image" />

                    <TextView
                        android:id="@+id/time"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TEST TEST"
                        android:textColor="#FFFFFF" />
                </LinearLayout>
            </FrameLayout>

我有一个 ArrayList 类型 ThumbnailInfo 其中

public class ThumbnailInfo {
    int time; //time in milliseconds
    String body;


    ThumbnailInfo(int time,String body){
        this.time = time;
        this.body = body;
    }
}

所以现在我将时间存储在数组列表中,我想在特定时间之前 10 秒显示缩略图。

例如 假设 arraylist 中存储的时间是 00:40 , 01:30 , 04:50 (只是为了说明问题,时间以毫秒为单位存储)。 因此,当我播放视频时,我必须将缩略图布局的可见性设置为 00:30、01:20、04:40 的可见性,并将可见性设置为 00:41 的可见性, 01:31 , 04:51

所以我的问题是如何连续检查ArrayList中存储的时间并执行上述操作。

目前我所拥有的是使用 mediaPlayer.getCurrentPosition();

的视频的当前位置

现在我必须不断地将视频的当前位置与存储在 ArrayList 中的时间进行比较。 有没有观察者可以简化任务或任何其他方法。

P.S。 : 用户可以多次暂停视频。

欢迎任何帮助。提前致谢!!!

尝试计时器:
示例:

Timer wox=new Timer();
wox.scheduleAtFixedRate(new TimerTask() {
            public void run() {

                runOnUiThread(new Runnable() {
                    public void run() {
//your actions
         }
                });

            }
        }, 0, 1000);//timer time

为了简单起见,你应该将时间从分钟转换为秒,从0开始。如:

  • VISIBLE at 00:30 , 01:20 , 04:40 => ArrayList< Visible > 值为 30, 80, 280

  • INVISIBLE at 00:41, 01:31 , 04:51 => ArrayList< Invisible > 值为 41, 61, 281

      int seconds = 0;
      CountDownTimer t = new CountDownTimer(Long.MAX_VALUE ,1000) { // interval 1s  
    
                  // This is called every interval. (Every 1 seconds in this example)
                        public void onTick(long millisUntilFinished) {
                            checkVisible(ArrayList visible);// make visible button
                            checkInvisible(ArrayList invisible); // make invisible button
                            seconds++:
                        }
                        public void onFinish() {
                            System.out.println("finished"); 
                            seconds = 0;         
                        }
        }.start();
    

记得打电话

  • t.cancel() 暂停视频时
  • t.finish() 播放完视频后
  • 重置seconds变量;

考虑到用户可能会暂停视频并重新开始播放或可能由于网络不好,视频将不符合您假设的时间。所以显示或隐藏 imageButton 不应该依赖于计时器。

你可以考虑在handler中使用runnable,它会每1秒执行一次来检查视频显示的进度,并相应地显示或隐藏imageButton。

类似于以下代码:

    Handler handler = new Handler();

    Runnable runnable = new Runnable() {
    @Override
    public void run() {
        int progress = checkVideoProgress();
        determineShowOrHideImageButtonByProgress(progress);
        if (videoIsEnd()){
            handler.removeCallbacks(runnable);
        }else {
            handler.postDelayed(runnable,1000);//check every 1 second in this example.
        }
      }
    };

总之,imageButton的可见性应该依赖于视频的进度,而不是os的实时性。