Java 设置可见性后ImageView仍然可见(View.INVISIBLE)
我正在尝试制作一个相机应用程序,在拍摄照片时显示覆盖图。 显示此覆盖时,其他UI组件(显示图片的FrameLayout除外)应不可见。 但似乎我的两个ImageButton不可见,而我的imageview(ivCompass)不可见 下面是拍照时调用的代码Java 设置可见性后ImageView仍然可见(View.INVISIBLE),java,android,android-layout,imageview,Java,Android,Android Layout,Imageview,我正在尝试制作一个相机应用程序,在拍摄照片时显示覆盖图。 显示此覆盖时,其他UI组件(显示图片的FrameLayout除外)应不可见。 但似乎我的两个ImageButton不可见,而我的imageview(ivCompass)不可见 下面是拍照时调用的代码 Camera.PictureCallback mPicture = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
//create a new intent...
String path = createFile(data);
intent = new Intent();
intent.putExtra("path", path);
mBearingProvider.updateBearing();
bearing = mBearingProvider.getBearing();
cardinalDirection = bearingToString(bearing);
//((TextView) findViewById(R.id.tvPicDirection)).setText(cardinalDirection);
Log.e("Direction", cardinalDirection + "," + bearing);
findViewById(R.id.btnFlash).setVisibility(View.INVISIBLE);
findViewById(R.id.btnCapture).setVisibility(View.INVISIBLE);
findViewById(R.id.ivCompass).setVisibility(View.INVISIBLE);
findViewById(R.id.pictureOverlay).setVisibility(View.VISIBLE);
}
};
这是布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ImageButton
android:id="@+id/btnFlash"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:src="@drawable/camera_flash_on"
android:background="@drawable/circle_flash"
android:onClick="changeFlashMode"/>
<ImageButton
android:id="@+id/btnCapture"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:src="@drawable/icon_camera"
android:background="@drawable/circle_camera"/>
<ImageView
android:id="@+id/ivCompass"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:src="@drawable/camera_compass"
android:background="@android:color/transparent"/>
<RelativeLayout
android:id="@+id/pictureOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:background="@color/alphaBlack"
android:visibility="invisible">
</RelativeLayout>
我认为这只是命名、语法或诸如此类的错误,但我似乎找不到它
编辑:
以下是整个活动
public class CameraActivity extends AppCompatActivity implements BearingToNorthProvider.ChangeEventListener {
private Camera mCamera;
private CameraView mCameraView;
private float mDist = 0f;
private String flashMode;
private ImageButton flashButton;
private Intent intent;
private BearingToNorthProvider mBearingProvider;
private double bearing;
private double currentBearing = 0d;
private String cardinalDirection = "?";
private final int REQUEST_CODE_ASK_PERMISSIONS = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
mCamera = getCameraInstance();
mCameraView = new CameraView(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mCameraView);
ImageButton captureButton = (ImageButton) findViewById(R.id.btnCapture);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(flashMode);
mCamera.setParameters(params);
mCamera.takePicture(null, null, mPicture);
}
});
SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.apiKey), Context.MODE_PRIVATE);
flashMode = sharedPref.getString(getString(R.string.flashMode), Camera.Parameters.FLASH_MODE_OFF);
flashButton = (ImageButton) findViewById(R.id.btnFlash);
setFlashButton();
mBearingProvider = new BearingToNorthProvider(this,this);
mBearingProvider.setChangeEventListener(this);
mBearingProvider.start();
}
@Override
protected void onPause() {
super.onPause();
mBearingProvider.stop();
}
/**
* Helper method to access the camera returns null if it cannot get the
* camera or does not exist
*
* @return the instance of the camera
*/
private Camera getCameraInstance() {
Camera camera = null;
try {
camera = Camera.open();
} catch (Exception e) {
Log.e("CamException", e.toString());
}
return camera;
}
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
//create a new intent...
String path = createFile(data);
intent = new Intent();
intent.putExtra("path", path);
mBearingProvider.updateBearing();
bearing = mBearingProvider.getBearing();
cardinalDirection = bearingToString(bearing);
//((TextView) findViewById(R.id.tvPicDirection)).setText(cardinalDirection);
Log.e("Direction", cardinalDirection + "," + bearing);
findViewById(R.id.btnFlash).setVisibility(View.INVISIBLE);
findViewById(R.id.btnCapture).setVisibility(View.INVISIBLE);
findViewById(R.id.ivCompass).setVisibility(View.INVISIBLE);
findViewById(R.id.pictureOverlay).setVisibility(View.VISIBLE);
}
};
private void confirmPicture(View v) {
/*String direction = String.valueOf(((TextView) findViewById(R.id.tvPicDirection)).getText());
String description = String.valueOf(((EditText) findViewById(R.id.tvPicDescription)).getText());
intent.putExtra("direction", direction);
intent.putExtra("description", description);*/
//close this Activity...
setResult(Activity.RESULT_OK, intent);
finish();
}
//region File Methods
/**
* Method that creates a file from the given byte array and saves the file in the Pictures Directory
* @param data is the array of bytes that represent the picture taken by the camera
* @return the path of created file
*/
private String createFile(byte[] data){
checkFilePermissions();
File picFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "tempPic.jpg" + File.separator);
String path = picFile.getPath();
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(picFile));
bos.write(data);
bos.flush();
bos.close();
return path;
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
/**
* Checks the permission for reading to and writing from the external storage
*/
private void checkFilePermissions() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
int hasWriteExternalStoragePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWriteExternalStoragePermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_ASK_PERMISSIONS);
return;
}
}
}
//endregion
//region Zoom Methods
@Override
public boolean onTouchEvent(MotionEvent event) {
// Get the pointer ID
Camera.Parameters params = mCamera.getParameters();
int action = event.getAction();
if (event.getPointerCount() > 1) {
// handle multi-touch events
if (action == MotionEvent.ACTION_POINTER_DOWN) {
mDist = getFingerSpacing(event);
} else if (action == MotionEvent.ACTION_MOVE && params.isZoomSupported()) {
mCamera.cancelAutoFocus();
handleZoom(event, params);
}
} else {
// handle single touch events
if (action == MotionEvent.ACTION_UP) {
handleFocus(event, params);
}
}
return true;
}
private void handleZoom(MotionEvent event, Camera.Parameters params) {
int maxZoom = params.getMaxZoom();
int zoom = params.getZoom();
float newDist = getFingerSpacing(event);
if (newDist > mDist) {
//zoom in
if (zoom < maxZoom)
zoom++;
} else if (newDist < mDist) {
//zoom out
if (zoom > 0)
zoom--;
}
mDist = newDist;
params.setZoom(zoom);
mCamera.setParameters(params);
}
public void handleFocus(MotionEvent event, Camera.Parameters params) {
int pointerId = event.getPointerId(0);
int pointerIndex = event.findPointerIndex(pointerId);
// Get the pointer's current position
float x = event.getX(pointerIndex);
float y = event.getY(pointerIndex);
List<String> supportedFocusModes = params.getSupportedFocusModes();
if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean b, Camera camera) {
// currently set to auto-focus on single touch
}
});
}
}
/** Determine the space between the first two fingers */
private float getFingerSpacing(MotionEvent event) {
double x = event.getX(0) - event.getX(1);
double y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
}
//endregion
//region Flash Methods
public void changeFlashMode(View v) {
switch (flashMode) {
case Camera.Parameters.FLASH_MODE_ON :
flashMode = Camera.Parameters.FLASH_MODE_AUTO;
break;
case Camera.Parameters.FLASH_MODE_AUTO :
flashMode = Camera.Parameters.FLASH_MODE_OFF;
break;
case Camera.Parameters.FLASH_MODE_OFF :
flashMode = Camera.Parameters.FLASH_MODE_ON;;
break;
}
SharedPreferences sharedPref = getSharedPreferences(getString(R.string.flashMode), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(getString(R.string.flashMode), flashMode);
editor.commit();
setFlashButton();
}
public void setFlashButton() {
switch (flashMode) {
case Camera.Parameters.FLASH_MODE_ON :
flashButton.setImageDrawable(getResources().getDrawable(R.drawable.camera_flash_on));
break;
case Camera.Parameters.FLASH_MODE_AUTO :
flashButton.setImageDrawable(getResources().getDrawable(R.drawable.camera_flash_auto));
break;
case Camera.Parameters.FLASH_MODE_OFF :
flashButton.setImageDrawable(getResources().getDrawable(R.drawable.camera_flash_off));
break;
}
}
//endregion
//region Bearing Methods
/**
* Method that gives a cardinal direction based on the current bearing to the true north
* @param bearing is the bearing to the true north
* @return cardinal direction that belongs to the bearing
*/
private String bearingToString(Double bearing) {
String strHeading = "?";
if (isBetween(bearing,-180.0,-157.5)) { strHeading = "South"; }
else if (isBetween(bearing,-157.5,-112.5)) { strHeading = "SouthWest"; }
else if (isBetween(bearing,-112.5,-67.5)) { strHeading = "West"; }
else if (isBetween(bearing,-67.5,-22.5)) { strHeading = "NorthWest"; }
else if (isBetween(bearing,-22.5,22.5)) { strHeading = "North"; }
else if (isBetween(bearing,22.5,67.5)) { strHeading = "NorthEast"; }
else if (isBetween(bearing,67.5,112.5)) { strHeading = "East"; }
else if (isBetween(bearing,112.5,157.5)) { strHeading = "SouthEast"; }
else if (isBetween(bearing,157.5,180.0)) { strHeading = "South"; }
return strHeading;
}
/**
* Method that checks if a certain number is in a certain range of numbers
* @param x is the number to check
* @param lower is the number that defines the lower boundary of the number range
* @param upper is the number that defines the upper boundary of the number range
* @return true if the number is between the other numbers, false otherwise
*/
private boolean isBetween(double x, double lower, double upper) {
return lower <= x && x <= upper;
}
/*
Method that triggers when the bearing changes, it sets the current bearing and sends an updated context to the provider
*/
@Override
public void onBearingChanged(double bearing) {
this.bearing = bearing;
mBearingProvider.setContext(this);
ImageView image = (ImageView) findViewById(R.id.ivCompass);
// create a rotation animation (reverse turn degree degrees)
if (bearing < 0) {
bearing += 360;
}
RotateAnimation ra = new RotateAnimation((float)currentBearing,(float)-bearing, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF,0.5f);
// how long the animation will take place
ra.setDuration(210);
// set the animation after the end of the reservation status
ra.setFillAfter(true);
// Start the animation
image.startAnimation(ra);
currentBearing = -bearing;
mBearingProvider.setContext(this);
}
//endregion
}
公共类CameraActivity扩展AppCompative实现BearingToNorthProvider.ChangeEventListener{
私人摄像机麦卡梅拉;
私人摄影师麦卡梅拉维;
私有浮动mDist=0f;
私有字符串模式;
私有图像按钮闪动按钮;
私人意图;
私人承担或提供方mBearingProvider;
私人双轴承;
专用双电流轴承=0d;
私有字符串基数方向=“?”;
私有最终整数请求\代码\请求\权限=2;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_摄像头);
mCamera=getCameraInstance();
mCameraView=新的CameraView(这是mCamera);
FrameLayout preview=(FrameLayout)findviewbyd(R.id.camera\u preview);
preview.addView(mCameraView);
ImageButton captureButton=(ImageButton)findViewById(R.id.btnCapture);
captureButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Camera.Parameters params=mCamera.getParameters();
参数设置flashMode(flashMode);
mCamera.setParameters(参数);
takePicture(null,null,mPicture);
}
});
SharedReferences SharedReferences=this.getSharedReferences(getString(R.string.apiKey),Context.MODE\u PRIVATE);
flashMode=sharedPref.getString(getString(R.string.flashMode),Camera.Parameters.FLASH\u MODE\u OFF);
flashButton=(ImageButton)findViewById(R.id.btnFlash);
setFlashButton();
mBearingProvider=新轴承或提供程序(this,this);
mBearingProvider.setChangeEventListener(此);
mBearingProvider.start();
}
@凌驾
受保护的void onPause(){
super.onPause();
mBearingProvider.stop();
}
/**
*如果无法获取该值,则访问摄影机的Helper方法将返回null
*摄像机或摄像机不存在
*
*@返回相机的实例
*/
私人摄像机getCameraInstance(){
摄像头=空;
试一试{
camera=camera.open();
}捕获(例外e){
Log.e(“CamException”,e.toString());
}
返回摄像机;
}
Camera.PictureCallback mPicture=新建Camera.PictureCallback(){
@凌驾
公共void onPictureTaken(字节[]数据,摄像头){
//创建一个新的意图。。。
字符串路径=创建文件(数据);
intent=新intent();
intent.putExtra(“路径”,路径);
mBearingProvider.updateBearing();
轴承=mBearingProvider.getBearing();
基本方向=轴承定位(轴承);
//((TextView)findviewbyd(R.id.tvPicDirection)).setText(cardinalDirection);
对数e(“方向”,基数方向+”,“+方位);
findViewById(R.id.btnFlash).setVisibility(View.INVISIBLE);
findViewById(R.id.btnCapture).setVisibility(View.INVISIBLE);
findViewById(R.id.ivCompass).setVisibility(View.INVISIBLE);
findViewById(R.id.pictureOverlay).setVisibility(View.VISIBLE);
}
};
私有无效确认图片(视图五){
/*String direction=String.valueOf(((TextView)findviewbyd(R.id.tvPicDirection)).getText();
String description=String.valueOf(((EditText)findViewById(R.id.tvPicDescription)).getText());
意向。额外(“方向”,方向);
意图。额外(“说明”,说明)*/
//关闭此活动。。。
设置结果(Activity.RESULT_OK,intent);
完成();
}
//区域文件方法
/**
*方法,该方法从给定的字节数组创建文件并将文件保存在Pictures目录中
*@param data是表示相机拍摄的图片的字节数组
*@返回已创建文件的路径
*/
私有字符串创建文件(字节[]数据){
checkFilePermissions();
File picFile=新文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+File.separator+“tempPic.jpg”+File.separator);
字符串路径=picFile.getPath();
试一试{
BufferedOutputStream bos=新的BufferedOutputStream(新文件输出流(picFile));
写入(数据);
bos.flush();
bos.close();
返回路径;
}捕获(IOE异常){
e、 printStackTrace();
返回“”;
}
}
/**
*检查对外部存储器的读写权限
*/
私有void checkFilePermissions(){
if(android.os.Build.VERSION.SDK\u INT>=android.os.Build.VERSION\u code.M){
int hasWriteExternalStoragePermission=checkSelfPermission(Manifest.permission.WRITE\u EXTERNAL\u STORAGE);
if(hasWriteExternalStoragePermission!=已授予PackageManager.PERMISSION){
requestPermissions(新字符串[]{Manifest.permission.WRITE\u EXTERNAL\u STORAGE},
请求\代码\请求\权限);
返回;
}
}
}
//端区
//区域缩放方法
@凌驾
公共布尔onTouchEvent(运动事件){
//获取指针ID
Camera.Parameters params=mCamera.getParameters();
int action=event.getAction();
if(event.getPointerCount()>1){
//处理多点触控事件
if(action==MotionEvent.action\u指针\u向下){
mDist=getFingerSpacing(事件);
}else if(action==MotionEvent.action\u MOVE&¶ms.isZoomSupported()){
mCamera.cancelAutoFocus();
handleZoom(事件、参数);
}
@Override
public void onBearingChanged(double bearing) {
this.bearing = bearing;
mBearingProvider.setContext(this);
ImageView image = (ImageView) findViewById(R.id.ivCompass);
if (image.getVisibility() == View.VISIBLE) {
// create a rotation animation (reverse turn degree degrees)
if (bearing < 0) {
bearing += 360;
}
RotateAnimation ra = new RotateAnimation((float) currentBearing, (float) -bearing, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// how long the animation will take place
ra.setDuration(210);
// set the animation after the end of the reservation status
ra.setFillAfter(true);
// Start the animation
image.startAnimation(ra);
currentBearing = -bearing;
}
mBearingProvider.setContext(this);
}
findViewById(R.id.ivCompass).clearAnimation();