我正在尝试在相机预览中使用微调器构建相机应用程序。 我遗漏了一些我认为不可取的相机代码。当我按下它时,旋转器正在反应(改变颜色),但不会下降。我的微调器非常小(50dp),没有显示箭头,我的手机很旧但状况良好(Samsung Galaxy Pocket plus S5301,Android 4.04版)。也许这与表面变化有关?
爪哇:
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 values / 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 :(得分: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."
答案 1 :(得分:0)
你正在横向使用微调器。
Spinners将垂直显示项目。
Current Spinners horizontally Aligned
从&#34; wrap_content&#34;更改LinearLayout的高度to&#34; match_parent&#34;垂直查看微调器的项目。