我正在创建一个相机应用程序.捕获时的图像显示在网格视图中.现在,代码在除三星设备之外的所有设备上都能正常工作.
我正面临着方向问题.当我以纵向模式拍摄图像时,图像在网格视图中显示时会旋转.我没有保留任何旋转代码.其次,使用EXIF我在网格视图中获得了正确的图像,但是当设备方向改变时,图像再次以旋转的方式旋转.
附加图片:


对不起图像的分辨率.请知道它们是否不正确.将再次上传.我知道SO上有很多这样的帮助.但我想我在某个地方被困住了.
我指的是以下链接:
http://blog.andolasoft.com/2013/06/how-to-show-captured-images-dynamically-in-gridview-layout.html
android screen-orientation android-camera android-camera-intent android-orientation
我使用以下代码为每个用户首选项设置方向锁定:
private void doLock(boolean locked) {
if (locked) {
int o = getResources().getConfiguration().orientation;
if (o == Configuration.ORIENTATION_LANDSCAPE)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else if (o == Configuration.ORIENTATION_PORTRAIT)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
}
Run Code Online (Sandbox Code Playgroud)
它的工作原理,除了我处于解锁模式(SCREEN_ORIENTATION_SENSOR)的情况下屏幕显示LANDSCAPE正确(!),然后调用doLock(true)和...
而不是屏幕锁定到LANDSCAPE其当前(正确)横向视图,它锁定到倒置的横向视图.即同样精确但垂直翻转(y变为-y).
为什么这样,我如何处理这个问题才能解决它?
我的初步调查显示除了常见的两个(,)之外还有很多其他可能性,包括,但是我的预感告诉我这个问题可能取决于设备,所以通过使用它我可能只是为我的手机修复问题但不适用于所有其他设备.portraitlandscapereverseLandscape
有没有办法在所有设备中强制正确landscape定位(切换时sensor)?
为了使这更清晰,更容易重现,以下是显示问题的步骤:
SCREEN_ORIENTATION_SENSOR),屏幕显示LANDSCAPE正确(!),doLock(true)LANDSCAPE当前(正确)的横向视图中,而是锁定到倒置的横向视图.即同样精确但垂直翻转(y变为-y).我是Android开发的新手.我有纵向和横向模式的单独屏幕.当我改变我的方向时,相应的屏幕被加载并且活动重新开始.现在我不希望我的活动在我改变方向时重新启动,但是应该加载相应的屏幕(axml).
我试过了
[Activity(Label ="MyActivity",ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation)]
上面的行停止活动重新启动但它加载相同的屏幕(axml).请建议.谢谢
android xamarin.android android-orientation android-activity xamarin
我在我的代码中使用了一个微调器下拉列表,其中我有4到5个动态填充的值,比如说我将"apples"设置为默认值,我从下拉列表中选择"oranges"并将我的屏幕从纵向旋转到横向,它返回默认的"apples"以及与之关联的视图.如何保存状态,以便当我选择"oranges"并旋转到横向时,它会填充所选值/保持在相同的选定状态并保持视图完整/填充在与所选值对应的纵向模式中选择的视图.这是我用于相同的适配器代码:
public class MarketsSpinnerAdapter extends CustomRowAdapter<AdapterRow> {
private List<AdapterRow> mRenderList;
public MarketsSpinnerAdapter(final Context context, final List<AdapterRow> renderList) {
super(context);
mRenderList = new ArrayList<AdapterRow>();
mRenderList.addAll(renderList);
}
@Override
protected void setEntries(final List<AdapterRow> renderList) {
mRenderList = renderList;
}
@Override
protected List<AdapterRow> getEntries() {
return mRenderList;
}
@Override
public View getDropDownView(final int position, final View convertView, final ViewGroup parent) {
return getEntries().get(position).getDropDownView(mContext, convertView);
}
}
Run Code Online (Sandbox Code Playgroud)
相应片段中的相应用法:
private void populateCategoryRows(final Cursor cursor) {
mCategories.clear();
mAllCategories.clear();
cursor.moveToPosition(-1);
Map<String, String> categoryParentNames = new HashMap<String, String>();
int …Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments android-orientation android-spinner
我有一个特定的用例,我希望片段以纵向模式锁定,但仍然可以旋转活动(和/或在同一活动中可见的其他片段).有可能吗?
锁定片段方向的所有解决方案建议使用setRequestedOrientation并锁定活动方向,但我需要其他可见片段进行旋转.
我的应用程序支持API 10+(如果有一个使用API 11+的不错的解决方案,我可能会考虑在API <11中删除对landscape的支持).
提前致谢.
android android-layout android-fragments android-orientation android-activity
我是android的新手,我的问题是,是否有可能以编程方式更改方向?如果是的话,我们怎么做?
我正在开发基于Android的移动应用程序minSdkVersion=15.我想支持平板电脑的两种方向,只支持智能手机的肖像.一切都像魅力一样,但我遇到了一个让我发疯的小虫子.当智能手机处于横向模式并尝试触发新活动时,它会以横向模式打开一段时间,然后自动切换到纵向.我的每个活动都扩展了一个GeneralActivity类:
public class GeneralActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If smartphone lock orientation to portrait
if (!Helper.isTablet(this.getApplicationContext())){
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我检测到具有此功能的平板电脑
public class Helper {
public static boolean isTablet(Context context){
Configuration config = context.getResources().getConfiguration()
return config.smallestScreenWidthDp >= 600;
}
}
Run Code Online (Sandbox Code Playgroud)
我选择不在android:screenOrientationManifest.xml中指定,因为这样我就可以支持平板电脑的所有界面方向.我错过了什么吗?
编辑
我决定应用Jonathan在答案中建议的最佳实践,但我所描述的问题仍然存在.这是我在github上的回购:https://github.com/giacmarangoni/Android-Orientation-Test
我注意到,当从一个Activity处理配置更改时,onLayout和onSizeChanged会在方向更改后立即连续调用两次,无论是从landscape-> portrait还是从portrait-> landscape.此外,第一个onLayout/onSizeChanged包含旧维度(在旋转之前),而第二个onLayout/onSizeChanged包含新(正确)维度.
有谁知道为什么,和/或如何解决这个问题?似乎在配置更改后很长一段时间内屏幕尺寸发生了变化 - 也就是说,在onConfigurationChanged调用配置更改后立即尺寸不正确?
这是下面代码的调试输出,显示从Portrait到Landscape旋转后的onLayout/onSizeChanged调用(请注意,设备为540x960,因此横向宽度应为960,纵向宽度为540):
03-13 17:36:21.140: DEBUG/RotateTest(27765): onConfigurationChanged: LANDSCAPE
03-13 17:36:21.169: DEBUG/RotateTest(27765): onSizeChanged:540,884,0,0
03-13 17:36:21.189: DEBUG/RotateTest(27765): onLayout:true-0,0,540,884
03-13 17:36:21.239: DEBUG/RotateTest(27765): onSizeChanged:960,464,540,884
03-13 17:36:21.259: DEBUG/RotateTest(27765): onLayout:true-0,0,960,464
Run Code Online (Sandbox Code Playgroud)
另请注意,第一个onSizeChanged oldwidth和oldheight为0,表示我们刚刚添加到视图层次结构中 - 但是景观的尺寸错误!
以下是说明此行为的代码:
package com.example;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
public class MyActivity extends Activity
{
private static String TAG = "RotateTest";
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.d(TAG, "onConfigurationChanged: " + (newConfig.orientation == …Run Code Online (Sandbox Code Playgroud) android android-layout android-configchanges android-orientation
使用最新的AppCompat-v21库,我曾经ActionBarActivity创建和填充PreferenceFragment.但是,ActionBar当方向或屏幕大小改变时,似乎不会改变高度和文本大小.对其他活动进行测试,这种行为似乎只发生在PreferenceActivity(与此处提出的问题相反:ActionBar容量/溢出不会改变方向变化).
首先,为了处理方向的变化,我加入android:configChanges="keyboard|keyboardHidden|orientation|screenSize"了Manifest.我怀疑这是造成这个问题的主要原因,但正如我之前提到的,这适用于其他问题Activity.
以下是一些解释此问题的屏幕截图:
PreferenceActivity以纵向模式启动:

从肖像旋转到风景:

PreferenceActivity以横向模式启动:

从风景旋转到肖像:

附加信息
这是PreferenceActivity班级:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class PrefsActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit();
}
}
Run Code Online (Sandbox Code Playgroud)
这种行为是个错误吗?如果没有,是否有解决方法或修复?
我尝试使用新的ToolBar小部件,但没有运气.
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
public class PrefsActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preference);
Toolbar toolbar = …Run Code Online (Sandbox Code Playgroud) android android-appcompat preferenceactivity android-orientation android-actionbar
我已经按照 Google CameraX代码实验室来实现自定义相机。相机预览很好,但是当我在旋转图像捕获图像后拍摄图像时。我正在以纵向模式拍摄图像,但保存的图像是横向的。这是配置相机的方法
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener(Runnable {
// Used to bind the lifecycle of cameras to the lifecycle owner
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
// Preview
val preview = Preview.Builder()
.setTargetRotation(this.windowManager.defaultDisplay.rotation)
.build()
.also {
it.setSurfaceProvider(viewFinder.createSurfaceProvider())
}
imageCapture = ImageCapture.Builder()
.setTargetRotation(this.windowManager.defaultDisplay.rotation)
.build()
val imageAnalyzer = ImageAnalysis.Builder()
.build()
.also {
it.setAnalyzer(cameraExecutor, LuminosityAnalyzer { luma ->
Log.d(TAG, "Average luminosity: $luma")
})
}
// Select back camera as a default
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try { …Run Code Online (Sandbox Code Playgroud) android image-rotation android-orientation android-camera2 android-camerax