Android surfaceView填充全屏,亲测 有效

亦凉 2021-09-07 06:19 1244阅读 0赞

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NzZG5fbG92ZVFpbmdRaW5n_size_16_color_FFFFFF_t_70

使用环境:

  1. 使用到surfaceView,发现底部总有部分不能填充

使用方法:

获取到surfaceView的实际宽高,将这个宽高通过surfaceView的holder赋值。

代码如下:

  1. @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  2. @Override
  3. public void onWindowFocusChanged(boolean hasFocus) {
  4. super.onWindowFocusChanged(hasFocus);
  5. if (hasFocus) {
  6. int width = mSurfaceView.getMeasuredWidth() / 2;
  7. //int height = mSurfaceView.getMeasuredHeight();
  8. SurfaceHolder holder = mSurfaceView.getHolder();
  9. holder.setFixedSize(width, width);
  10. //setSurfaceViewCorner(width);
  11. }
  12. }

项目中需要用到圆形的surfaceView,代码如下:

  1. @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  2. private void setSurfaceViewCorner(final float radius) {
  3. mSurfaceView.setOutlineProvider(new ViewOutlineProvider() {
  4. @Override
  5. public void getOutline(View view, Outline outline) {
  6. Rect rect = new Rect();
  7. view.getGlobalVisibleRect(rect);
  8. int leftMargin = 0;
  9. int topMargin = 0;
  10. Rect selfRect = new Rect(leftMargin, topMargin,
  11. rect.right - rect.left,
  12. rect.bottom - rect.top);
  13. LogUtil.e("right-->" + (rect.right - rect.left));
  14. LogUtil.e("bottom-->" + (rect.bottom - rect.top));
  15. outline.setRoundRect(selfRect, radius);
  16. }
  17. });
  18. mSurfaceView.setClipToOutline(true);
  19. }

更多讨论,欢迎来询:88627109

祝,工作顺利,永无BUG!

发表评论

表情:
评论列表 (有 0 条评论,1244人围观)

还没有评论,来说两句吧...

相关阅读