android – WINDOW_SERVICE无法解析为变量

前端之家收集整理的这篇文章主要介绍了android – WINDOW_SERVICE无法解析为变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我认为WINDOW_SERVICE应该是 android.content.Context中定义的常量.

http://developer.android.com/reference/android/content/Context.html#WINDOW_SERVICE

当我在下面的代码段中使用它时,它会抛出一个错误,说它无法作为变量解析.

显示display =((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

这是完整的代码

  1. package com.commonsware.android.skeleton;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.hardware.Camera;
  6. import android.hardware.Camera.*;
  7. import android.os.Bundle;
  8. import android.view.Display;
  9. import android.view.Surface;
  10. import android.view.SurfaceHolder;
  11. import android.view.SurfaceView;
  12. import android.view.Window;
  13. import android.view.WindowManager;
  14. import android.widget.FrameLayout;
  15. import java.io.IOException;
  16. import java.util.List;
  17.  
  18. // ----------------------------------------------------------------------
  19.  
  20. public class SimpleBulbActivity extends Activity {
  21. private Preview mPreview;
  22. FrameLayout preview;
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27.  
  28. // Hide the window title.
  29. requestWindowFeature(Window.FEATURE_NO_TITLE);
  30. setContentView(R.layout.main);
  31. }
  32.  
  33. protected void onResume() {
  34. super.onResume();
  35. //Setup the FrameLayout with the Camera Preview Screen
  36. mPreview = new Preview(this);
  37. preview = (FrameLayout)findViewById(R.id.preview);
  38. preview.addView(mPreview);
  39. }
  40.  
  41. }
  42.  
  43. // ----------------------------------------------------------------------
  44.  
  45. class Preview extends SurfaceView implements SurfaceHolder.Callback {
  46. SurfaceHolder mHolder;
  47. Camera mCamera;
  48.  
  49. Preview(Context context) {
  50. super(context);
  51.  
  52. // Install a SurfaceHolder.Callback so we get notified when the
  53. // underlying surface is created and destroyed.
  54. mHolder = getHolder();
  55. mHolder.addCallback(this);
  56. mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  57. }
  58.  
  59. public void surfaceCreated(SurfaceHolder holder) {
  60. // The Surface has been created,acquire the camera and tell it where
  61. // to draw.
  62. mCamera = Camera.open();
  63. try {
  64. mCamera.setPreviewDisplay(holder);
  65. } catch (IOException exception) {
  66. mCamera.release();
  67. mCamera = null;
  68. // TODO: add more exception handling logic here
  69. }
  70. }
  71.  
  72. public void surfaceDestroyed(SurfaceHolder holder) {
  73. // Surface will be destroyed when we return,so stop the preview.
  74. // Because the CameraDevice object is not a shared resource,it's very
  75. // important to release it when the activity is paused.
  76. mCamera.stopPreview();
  77. mCamera.release();
  78. mCamera = null;
  79. }
  80.  
  81.  
  82. private Size getOptimalPreviewSize(List<Size> sizes,int w,int h) {
  83. final double ASPECT_TOLERANCE = 0.05;
  84. double targetRatio = (double) w / h;
  85. if (sizes == null) return null;
  86.  
  87. Size optimalSize = null;
  88. double minDiff = Double.MAX_VALUE;
  89.  
  90. int targetHeight = h;
  91.  
  92. // Try to find an size match aspect ratio and size
  93. for (Size size : sizes) {
  94. double ratio = (double) size.width / size.height;
  95. if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
  96. if (Math.abs(size.height - targetHeight) < minDiff) {
  97. optimalSize = size;
  98. minDiff = Math.abs(size.height - targetHeight);
  99. }
  100. }
  101.  
  102. // Cannot find the one match the aspect ratio,ignore the requirement
  103. if (optimalSize == null) {
  104. minDiff = Double.MAX_VALUE;
  105. for (Size size : sizes) {
  106. if (Math.abs(size.height - targetHeight) < minDiff) {
  107. optimalSize = size;
  108. minDiff = Math.abs(size.height - targetHeight);
  109. }
  110. }
  111. }
  112. return optimalSize;
  113. }
  114.  
  115. public void surfaceChanged(SurfaceHolder holder,int format,int h) {
  116. // Now that the size is known,set up the camera parameters and begin
  117. // the preview.
  118. Camera.Parameters parameters = mCamera.getParameters();
  119.  
  120. List<Size> sizes = parameters.getSupportedPreviewSizes();
  121. Size optimalSize = getOptimalPreviewSize(sizes,w,h);
  122. parameters.setPreviewSize(optimalSize.width,optimalSize.height);
  123.  
  124. Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
  125.  
  126. if(display.getRotation() == Surface.ROTATION_0)
  127. {
  128. parameters.setPreviewSize(h,w);
  129. mCamera.setDisplayOrientation(90);
  130. }
  131.  
  132. if(display.getRotation() == Surface.ROTATION_90)
  133. {
  134. parameters.setPreviewSize(w,h);
  135. }
  136.  
  137. if(display.getRotation() == Surface.ROTATION_180)
  138. {
  139. parameters.setPreviewSize(h,w);
  140. }
  141.  
  142. if(display.getRotation() == Surface.ROTATION_270)
  143. {
  144. parameters.setPreviewSize(w,h);
  145. mCamera.setDisplayOrientation(180);
  146. }
  147.  
  148. mCamera.setParameters(parameters);
  149. mCamera.startPreview();
  150. }
  151.  
  152. }

解决方法

尝试…
  1. Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

猜你在找的Android相关文章