Android切换至SurfaceView时闪屏(黑屏闪一下)以及黑屏移动问题的解决方法

作者:Joanna.Yan 时间:2023-07-21 10:51:45 

1.最近的项目中,有一个Activity用到Fragment+ViewPager,其中一个fragment中实现了视频播放的功能,包含有SurfaceView。结果,每次打开程序第一次进入到该Activity时都会闪屏黑一下。原因就出在SurfaceView。

详解:

I think I found the reason for the black flash. In my case I'm using a SurfaceView inside a Fragment and dynamically adding this fragment to the activity after some action. The moment when I add the fragment to the activity, the screen flashes black. I checked out grepcode for the SurfaceView source and here's what I found: when the surface view appears in the window the very fist time, it requests the window's parameters changing by calling a private IWindowSession.relayout(..) method. This method “gives” you a new frame, window, and window surface. I think the screen blinks right at that moment.
  The solution is pretty simple: if your window already has appropriate parameters it will not refresh all the window's stuff and the screen will not blink. The simplest solution is to add a 0px height plain SurfaceView to the first layout of your activity. This will recreate the window before the activity is shown on the screen, and when you set your second layout it will just continue using the window with the current parameters. I hope this helps.

原因:

SurfaceView因为不同于一般的view,它有自己良好的缓冲以及数据存取机制,系统对他有特殊处理。当surfaceview第一次在当前activity上添加的时候,系统会给WindowManager重新排布局,relayout,这样就会黑一下,这个只会出现在第一次,以后再添加surfaceview时就不会黑屏了。

解决:

可以在没有进入surfaceview的界面(比如很多程序一进去就有个Loading界面)的时候,在其它界面的layout.xml文件中添加一个SurfaceView,宽和高都弄成0dp,这样对布局没影响,而且这个东西使得surfaceview第一次出现了,那么也就是说,下一次我们真正用到SurfaceView的时候就不会闪屏了。

2.viewpager中有surfaceview播放视频,来回滑屏时,关于黑屏移动问题

解决:

surfaceview.setZorderTop(true);

surfaceview.getHolder().setFormat(SurfaceView.TRANSPARENT);

来源:http://www.cnblogs.com/Joanna-Yan/p/4829325.html

标签:Android,surfaceview,闪屏
0
投稿

猜你喜欢

  • java 字段值为null,不返回该字段的问题

    2023-07-13 10:32:34
  • 自定义类加载器以及打破双亲委派模型解析

    2023-06-22 22:03:59
  • 基于Flutter实现图片选择和图片上传

    2023-07-06 04:28:50
  • Flutter模仿实现微信底部导航栏流程详解

    2023-06-21 11:46:12
  • Studio 编译报错:compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.的解决办法

    2023-06-19 17:19:41
  • Android Studio打包jar及aar包的方法

    2023-06-18 15:59:53
  • C++右值引用与move和forward函数的使用详解

    2023-07-05 19:27:33
  • SpringBoot 配合 SpringSecurity 实现自动登录功能的代码

    2023-07-22 04:58:22
  • SpringBoot自动装配原理详解

    2023-07-03 05:49:08
  • Java读写txt文件时防止中文乱码问题出现的方法介绍

    2023-06-23 04:40:32
  • SSH框架网上商城项目第2战之基本增删查改、Service和Action的抽取

    2023-06-21 19:16:23
  • C#正则表达式分解和转换IP地址实例(C#正则表达式大全 c#正则表达式语法)

    2023-07-17 07:11:25
  • Java Comparable及Comparator接口区别详解

    2023-07-03 08:19:50
  • Spring Boot整合流控组件Sentinel的场景分析

    2023-06-22 19:27:53
  • Android Studio中通过CMake使用NDK并编译自定义库和添加预编译库

    2023-06-16 10:31:35
  • Android Java try catch 失效问题及解决

    2023-06-17 17:07:33
  • 一文带你了解C#中抽象方法与虚方法的区别

    2023-07-23 00:14:32
  • Android仿简书搜索框效果的示例代码

    2023-06-18 16:02:58
  • Android WebView无法弹出软键盘的原因及解决办法

    2023-07-07 18:30:00
  • c#实现16进制和字符串之间转换的代码

    2023-07-22 08:28:09
  • asp之家 软件编程 m.aspxhome.com