草庐IT

Android:仅在相机预览中旋转叠加按钮

coder 2023-12-13 原文

我有一个 Android 应用程序,它使用 LinearLayout 作为主布局,并带有一个由相机预览填充的 SurfaceView。 在这里,我用三个按钮和一个自定义 TextView 膨胀另一个 LinearLayout。我希望相机预览始终保持横向,并且覆盖布局会根据设备方向发生变化。

我尝试在 Activity 的 list 中设置 android:screenOrientation="landscape" 但是(当然)膨胀的布局也始终保持固定,而不设置 android:screenOrientation 属性还有相机预览旋转,减慢应用程序并显示奇怪的预览形状因素。这里是布局的相关代码:

private void setupLayout()
{
    setContentView(R.layout.main);
    getWindow().setFormat(PixelFormat.UNKNOWN);

    // Release camera if owned by someone else
    if (camera != null)
        releaseCamera();

    surfaceView = (SurfaceView) findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    controlInflater = LayoutInflater.from(getBaseContext());
    View viewControl = controlInflater.inflate(R.layout.control, null);
    LayoutParams layoutParamsControl = new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    this.addContentView(viewControl, layoutParamsControl);


    buttonGetCollectingData = (Button) findViewById(R.id.getcolldata);
    buttonGetCollectingData.setOnClickListener(new Button.OnClickListener()
    {
        public void onClick(View arg0)
        {
            ...
        }
    });

    btnBackHome = (Button) findViewById(R.id.btnBackHome);
    btnBackHome.setOnClickListener(new Button.OnClickListener()
    {
        public void onClick(View arg0)
        {
            ...
        }
    });

    autoFitTextViewMainMsg = (AutoFitTextView) findViewById(R.id.autoFitTextViewMainMsg);

    buttonTakePicture.setOnClickListener(new Button.OnClickListener()
    {

        public void onClick(View arg0)
        {
            ...
        }
    });
}

任何关于如何实现这一点的想法将不胜感激!

最佳答案

您可以使用自定义方向事件监听器来获取方向并根据需要设置您的 UI。

首先,创建一个类CustomOrientationEventListener

public abstract class CustomOrientationEventListener  extends OrientationEventListener {


private static final String TAG = "CustomOrientationEvent";
private int prevOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
private Context context;
private final int ROTATION_O    = 1;
private final int ROTATION_90   = 2;
private final int ROTATION_180  = 3;
private final int ROTATION_270  = 4;
private int rotation = 0;

public CustomOrientationEventListener(Context context) {
    super(context);
    this.context = context;
}

@Override
public void onOrientationChanged(int orientation) {

    if (android.provider.Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 0) // 0 = Auto Rotate Disabled
        return;
    int currentOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
    if (orientation >= 340 || orientation < 20 && rotation != ROTATION_O) {
        currentOrientation = Surface.ROTATION_0;
        rotation = ROTATION_O;

    } else if (orientation >= 70 && orientation < 110 && rotation != ROTATION_90) {
        currentOrientation = Surface.ROTATION_90;
        rotation = ROTATION_90;

    } else if (orientation >= 160 && orientation < 200 && rotation != ROTATION_180) {
        currentOrientation = Surface.ROTATION_180;
        rotation = ROTATION_180;

    } else if (orientation >= 250 && orientation < 290 && rotation != ROTATION_270) {
        currentOrientation = Surface.ROTATION_270;
        rotation = ROTATION_270;
    }

    if (prevOrientation != currentOrientation && orientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
            prevOrientation = currentOrientation;
            if (currentOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
                onSimpleOrientationChanged(rotation);
        }
    }
}

public abstract void onSimpleOrientationChanged(int orientation);


}

然后将以下行添加到您的 Activity 标签下的 Android list

  android:configChanges="orientation|keyboardHidden|screenSize"

确保您不添加android:screenOrientation 属性到该 Activity 的 list 中。

然后在相机 Activity 的 onCreate 中使用 CustomOrientationEventListener

public class YourActivity extends AppCompatActivity {

  private CustomOrientationEventListener customOrientationEventListener;

  final int ROTATION_O    = 1;
  final int ROTATION_90   = 2;
  final int ROTATION_180  = 3;
  final int ROTATION_270  = 4;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

       .....

      customOrientationEventListener = new 
 CustomOrientationEventListener(getBaseContext()) {
          @Override
          public void onSimpleOrientationChanged(int orientation) {
              switch(orientation){
                  case ROTATION_O:
                      //rotate as on portrait
                 yourButton.animate().rotation(0).setDuration(500).start();
                      break;
                  case ROTATION_90:
                      //rotate as left on top
                 yourButton.animate().rotation(-90).setDuration(500).start();
                      break;
                  case ROTATION_270:
                      //rotate as right on top
                 yourButton.animate().rotation(90).setDuration(500).start();
                      break;
                  case ROTATION_180:
                      //rotate as upside down
                 yourButton.animate().rotation(180).setDuration(500).start();
                      break;

              }
          }
      };

  }

  @Override
  protected void onResume() {
      super.onResume();
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
      customOrientationEventListener.enable();
  }

  @Override
  protected void onPause() {
      super.onPause();
      customOrientationEventListener.disable();
  }

  @Override
  protected void onDestroy() {
      super.onDestroy();
      customOrientationEventListener.disable();
  }
}

关于Android:仅在相机预览中旋转叠加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16733307/

有关Android:仅在相机预览中旋转叠加按钮的更多相关文章

  1. 旋转矩阵的几何意义 - 2

    点向量坐标矩阵的几何意义介绍旋转矩阵的几何含义之前,先介绍一下点向量坐标矩阵的几何含义点:在一维空间下就是一个标量,如同一条直线上,以任意某一个位置为0点,以一定的尺度间隔为1,2,3...,相反方向为-1,-2,-3...;如此就形成了一维坐标系,这时候任何一个点都可以用一个数值表示,如点p1=5,即即从原点出发沿着x轴正方向移动5个尺度;点p2=-3,负方向移动3个尺度;     在一维坐标系上过原点做垂直于一维坐标系的直线,则形成了二维坐标系,此时描述一个点需要两个数值来表示点p3=(3,2),即从原点出发沿着x轴正方向移动3个尺度,在此基础上沿着y轴正方向移动两个尺度的位置就是点p3。

  2. Unity 3D 制作开关门动画,旋转门制作,推拉门制作,门把手动画制作 - 2

    Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u

  3. [工业相机] 分辨率、精度和公差之间的关系 - 2

    📢博客主页:https://blog.csdn.net/weixin_43197380📢欢迎点赞👍收藏⭐留言📝如有错误敬请指正!📢本文由Loewen丶原创,首发于CSDN,转载注明出处🙉📢现在的付出,都会是一种沉淀,只为让你成为更好的人✨文章预览:一.分辨率(Resolution)1、工业相机的分辨率是如何定义的?2、工业相机的分辨率是如何选择的?二.精度(Accuracy)1、像素精度(PixelAccuracy)2、定位精度和重复定位精度(RepeatPrecision)三.公差(Tolerance)四.课后作业(Post-ClassExercises)视觉行业的初学者,甚至是做了1~2年

  4. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  5. ruby-on-rails - Rails 单选按钮 - 模型中多列的一种选择 - 2

    我希望用户从一个模型的三个选项中选择一个。即我有一个模型视频,可以被评为正面/负面/未知目前我有三列bool值(pos/neg/unknown)。这是处理这种情况的最佳方式吗?为此,表单应该是什么样的?目前我有类似的东西但显然它允许多项选择,而我试图将它限制为只有一个..怎么办? 最佳答案 如果要使用字符串列,让我们说rating。然后在你的表单中:#...#...它只允许一个选择编辑完全相同但使用radio_button_tag: 关于ruby-on-rails-Rails单选按钮-模

  6. ruby-on-rails - NameError(未初始化常量 Unzipper::Zip)但仅在 Heroku 部署(Rails)上 - 2

    我有一个类unzipper.rb,它使用Rubyzip解压文件。在我的本地环境中,我可以成功解压缩文件,而无需使用require'zip'明确包含依赖项但是在Heroku上,我得到一个NameError(uninitializedconstantUnzipper::Zip)我只能通过使用明确的require来解决问题:为什么这在H​​eroku环境中是必需的,但在本地主机上却不是?我的印象是Rails自动需要所有gem。app/services/unzipper.rbrequire'zip'#OnlyrequiredforHeroku.Workslocallywithout!class

  7. ruby-on-rails - 如何从按钮或链接单击的 View 调用 Rails 方法 - 2

    基本上,我试图在用户单击链接(或按钮或某种类型的交互元素)时执行Rails方法。我试着把它放在View中:但这似乎没有用。它最终只是在用户甚至没有点击“添加”链接的情况下调用该函数。我也用link_to试过了,但也没用。我开始认为没有一种干净的方法可以做到这一点。无论如何,感谢您的帮助。附言。我在ApplicationController中定义了该方法,它是一个辅助方法。 最佳答案 View和Controller是相互独立的。为了使链接在Controller内执行函数调用,您需要对应用程序中的端点执行ajax调用。该路由应调用rub

  8. ruby-on-rails - 如何在 Rails 中添加禁用的提交按钮 - 2

    我在ruby​​表单中有一个提交按钮f.submitbtn_text,class:"btnbtn-onemgt12mgb12",id:"btn_id"我想在不使用任何javascript的情况下通过ruby​​禁用此按钮 最佳答案 添加disabled:true选项。f.submitbtn_text,class:"btnbtn-onemgt12mgb12",id:"btn_id",disabled:true 关于ruby-on-rails-如何在Rails中添加禁用的提交按钮,我们在St

  9. ruby-on-rails - 从 ActiveAdmin has_many 表单助手中删除 "Add new"按钮 - 2

    我在事件管理员编辑页面中有嵌套资源,但我只想允许管理员编辑现有资源的内容,而不是添加新的嵌套资源。我的代码看起来像这样:formdo|f|f.inputsdof.input:authorf.input:contentf.has_many:commentsdo|comment_form|comment_form.input:contentcomment_form.input:_destroy,as::boolean,required:false,label:'Remove'endendf.actionsend但它在输入下添加了“添加新评论”按钮。我怎样才能禁用它,并只为主窗体保留f.ac

  10. ruby-on-rails - 仅在某些页面上使用 rails_xss - 2

    我正在使用rails_xss运行Rails2.3.14插入。我有另一个用于创建管理仪表板View的插件。我的问题是rails_xss正在转义我的仪表板插件生成的所有HTML。有没有一种方法可以将rails_xss配置为不转义匹配example.com/admin或基于目录(app/views/admin)或任何类似的页面结果一样吗? 最佳答案 更新仪表板生成插件以使用raw或html_safe进行内容输出可能会更简单。 关于ruby-on-rails-仅在某些页面上使用rails_xss

随机推荐