2016-09-15 26 views
0

我正在试图在相机预览中使用微调来构建相机应用程序。 我忽略了一些我认为没有发现的相机代码。当我按下它时,微调器会发生反应(改变颜色),但不会下降。我的微调器非常小(50dp),没有箭头显示,而且我的手机老旧,但状况良好(Samsung Galaxy Pocket S5301,Android版本4.04)。也许这与表面变化有关?SurfaceView上的Android微调不下降

的Java:

public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback { 

private Camera camera; 
private SurfaceView surfaceView; 
private SurfaceHolder surfaceHolder; 

Camera.PictureCallback rawCallback; 
Camera.ShutterCallback shutterCallback; 
Camera.PictureCallback jpegCallback; 

private LayoutInflater controlInflater = null; 
private final String TAG = " CameraActivity"; 

static final int REQUEST_IMAGE_CAPTURE = 1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_camera); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER); 
    getWindow().setFormat(PixelFormat.UNKNOWN); 
    surfaceView = (SurfaceView)findViewById(R.id.camera_preview); 
    surfaceHolder = surfaceView.getHolder(); 
    surfaceHolder.addCallback(this); 
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    controlInflater = LayoutInflater.from(getBaseContext()); 
    View viewControl = controlInflater.inflate(R.layout.activity_camera, null); 
    LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
    this.addContentView(viewControl, layoutParamsControl); 
    if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) { 
     Toast.makeText(getApplicationContext(), "Your phone does not have a camera.", Toast.LENGTH_SHORT).show(); 
     finish(); 
    } 

    Spinner spinner1 = (Spinner) findViewById(R.id.spinner1); 
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item); 
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner1.setAdapter(adapter1); 
} 

public void refreshCamera() { 
    if (surfaceHolder.getSurface() == null) { 
     return; 
    } 

    try { 
     camera.stopPreview(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    try { 
     camera.setPreviewDisplay(surfaceHolder); 
     camera.startPreview(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 


@Override 
public void surfaceCreated(SurfaceHolder holder) { 

    try { 
     camera = Camera.open(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    Camera.Parameters param; 
    param = camera.getParameters(); 
    param.setPreviewSize(352, 288); 
    camera.setParameters(param); 

    try { 
     camera.setPreviewDisplay(surfaceHolder); 
     camera.startPreview(); 
    } 

    catch (Exception e) { 
     System.err.println(e); 
     return; 
    } 
} 

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
    refreshCamera(); 
} 

XML

<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="package.main.CameraActivity"> 


<SurfaceView android:id="@+id/camera_preview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"></SurfaceView> 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <LinearLayout android:id="@+id/topLayout" 
     android:layout_alignParentTop="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <Spinner 
      android:id="@+id/spinner1" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:layout_margin="10dp" 
      android:dropDownWidth="200dp" 
      android:paddingRight="14dp" 
      android:background="@android:drawable/spinner_background" 
      android:spinnerMode="dropdown" /> 

      //More spinners 

    </LinearLayout> 

      //Image in the middle of the screen 

</RelativeLayout> 
</FrameLayout> 

XML值/ strings.xml中

<resources> 
<string-array name="planets_array"> 
    <item>Mercury</item> 
    <item>Venus</item> 
    <item>Earth</item> 
    <item>Mars</item> 
    <item>Jupiter</item> 
    <item>Saturn</item> 
    <item>Uranus</item> 
    <item>Neptune</item> 
</string-array> 
</resources> 

回答

0

更改根布局的FrameLayout到RelativeLayout的。

根据文档FrameLayout应该只有一个孩子。

"FrameLayout is designed to block out an area on the screen to display a single item. 
    Generally, FrameLayout should be used to hold a single child view, 
    because it can be difficult to organize child views in a way that's 
    scalable to different screen sizes without the children overlapping each other. 
    You can, however, 
    add multiple children to a FrameLayout and control their position within the FrameLayout 
    by assigning gravity to each child, 
    using the android:layout_gravity attribute." 
+0

谢谢,但它没有奏效。我改成了RelativeLayout。我试图将FrameLayout放在LinearLayout的周围,然后绕着微调器。然后我从代码中移除了FrameLayout。但没有任何帮助。 – TheEagle