ImageView 在相机表面视图后面移动
Posted
技术标签:
【中文标题】ImageView 在相机表面视图后面移动【英文标题】:ImageView moving behind Camera Surface View 【发布时间】:2015-01-04 23:44:13 【问题描述】:我试图在相机表面视图上放置一个图像视图,当应用程序启动时,图像视图位于相机预览的顶部,但在拍摄照片并设置为图像视图后,相机预览嵌套在移动中的 RelativeLayout当调用 startpreview 以再次启动相机时前进,因此相机预览最终位于图像视图上方。在 xml 布局中的 imageview 之后放置了相同布局中的按钮,这些按钮不受影响。如果我在一张图片后没有再次启动相机,则 imageview 有它的图像并且是可见的。转储视图层次结构验证相同。据我所知,这应该是相当直截了当的事情,所以我不确定我哪里出错了。
更新:我还尝试在相机启动后以编程方式添加一个带有位图的全新 ImageView,并且 ImageView 也被移到后面。
XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:id="@+id/add_activity"
android:layout_
>
<RelativeLayout
android:layout_
android:layout_
android:id="@+id/camera_preview"
/>
<ImageView
android:layout_
android:id="@+id/beforeImageView"
android:src="@drawable/app_icon"
android:alpha="0.2"
android:layout_ />
<Button
android:layout_
android:layout_
android:text="Back"
android:id="@+id/close_camera"
android:layout_gravity="bottom|left"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:gravity="bottom|left"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/button_capture"
android:text="Before"
android:layout_
android:layout_
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="20dp"
android:background="@color/purple"
android:textColor="@color/white"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<ProgressBar
android:layout_
android:layout_
android:id="@+id/add_progress_bar"
android:layout_gravity="center" />
</FrameLayout>
JAVA:
public class AddActivity extends Activity
private Camera mCamera;
CameraPreview mPreview;
private RelativeLayout preview;
boolean useFrontCamera = true;
boolean tookBeforeImage = false;
boolean tookAfterImage = false;
Bitmap beforeImage;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
//Hide Notification and Action Bars
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getActionBar();
actionBar.hide();
progressBar = (ProgressBar) findViewById(R.id.add_progress_bar);
progressBar.setVisibility(View.INVISIBLE);
boolean cameraCheck = checkCameraHardware(getApplicationContext());
if (cameraCheck)
StartCamera cameraLaunch = new StartCamera();
cameraLaunch.execute();
Button closeCameraButton = (Button) findViewById(R.id.close_camera);
closeCameraButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
releaseCamera();
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getActionBar();
actionBar.show();
finish();
);
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
mCamera.takePicture(null,null,mPicture);
);
private boolean checkCameraHardware(Context context)
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
return true;
else
return false;
private void releaseCamera()
if (mCamera != null)
mCamera.release();
mCamera = null;
@Override
protected void onPause()
super.onPause();
releaseCamera();
@Override
public void onBackPressed()
releaseCamera();
super.onBackPressed();
@Override
protected void onResume()
super.onResume();
//Hide Notification and Action Bars
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getActionBar();
actionBar.hide();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_add, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
//return true;
return super.onOptionsItemSelected(item);
private class StartCamera extends AsyncTask<String, Void, String>
@Override
protected void onPreExecute()
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
@Override
protected String doInBackground(String... params)
try
if (useFrontCamera)
mCamera = Camera.open();
mCamera.enableShutterSound(true);
else
//Flip Camera
catch (Exception e)
e.printStackTrace();
return null;
@Override
protected void onPostExecute(String s)
super.onPostExecute(s);
mPreview = new CameraPreview(getApplicationContext(),mCamera);
preview = (RelativeLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview,0);
progressBar.setVisibility(View.INVISIBLE);
private Camera.PictureCallback mPicture = new Camera.PictureCallback()
@Override
public void onPictureTaken(byte[] data, Camera camera)
Display display = getWindowManager().getDefaultDisplay();
int rotation = 0;
switch (display.getRotation())
case Surface.ROTATION_0: // This is display orientation
rotation = 90;
break;
case Surface.ROTATION_90:
rotation = 0;
break;
case Surface.ROTATION_180:
rotation = 270;
break;
case Surface.ROTATION_270:
rotation = 180;
break;
Bitmap bitmap = ImageTools.toBitmap(data);
bitmap = ImageTools.rotate(bitmap, rotation);
if (!tookBeforeImage)
beforeImage = bitmap;
ResetCamera cameraReset = new ResetCamera();
cameraReset.execute();
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setText("After");
tookBeforeImage = true;
else if (tookBeforeImage == true && tookAfterImage == false)
else
;
/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera)
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
this.setBackgroundColor(Color.TRANSPARENT);
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
public void surfaceCreated(SurfaceHolder holder)
// The Surface has been created, now tell the camera where to draw the preview.
try
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
catch (IOException e)
Log.d("Camera Error", "Error setting camera preview: " + e.getMessage());
public void surfaceDestroyed(SurfaceHolder holder)
// empty. Take care of releasing the Camera preview in your activity.
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null)
// preview surface does not exist
return;
// stop preview before making changes
try
mCamera.stopPreview();
catch (Exception e)
// ignore: tried to stop a non-existent preview
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
catch (Exception e)
Log.d("Camera Error", "Error starting camera preview: " + e.getMessage());
private static class ImageTools
public static Bitmap toBitmap(byte[] data)
return BitmapFactory.decodeByteArray(data, 0, data.length);
public static Bitmap rotate(Bitmap in, int angle)
Matrix mat = new Matrix();
mat.postRotate(angle);
return Bitmap.createBitmap(in, 0, 0, in.getWidth(), in.getHeight(), mat, true);
private class ResetCamera extends AsyncTask<String, Void, String>
@Override
protected void onPreExecute()
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
@Override
protected String doInBackground(String... params)
mCamera.startPreview();
return null;
@Override
protected void onPostExecute(String s)
super.onPostExecute(s);
ImageView beforeImageView = (ImageView) findViewById(R.id.beforeImageView);
beforeImageView.setImageBitmap(beforeImage);
beforeImageView.setImageAlpha(100);
//Trying to add programmatically still ends up behind Camera Preview
ImageView newBefore = (ImageView) new ImageView(getApplicationContext());
newBefore.setImageBitmap(beforeImage);
newBefore.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
preview.addView(newBefore,0);
progressBar.setVisibility(View.INVISIBLE);
【问题讨论】:
【参考方案1】:这是由于试图加载太大的位图,它正在悄悄地在日志中抛出一个通知,因为它无法创建纹理。减小位图的大小解决了这个问题。
【讨论】:
以上是关于ImageView 在相机表面视图后面移动的主要内容,如果未能解决你的问题,请参考以下文章