Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从不同角度旋转视图?_Android_Boolean_Android Animation_Screen Orientation_Android Orientation - Fatal编程技术网

Android 如何从不同角度旋转视图?

Android 如何从不同角度旋转视图?,android,boolean,android-animation,screen-orientation,android-orientation,Android,Boolean,Android Animation,Screen Orientation,Android Orientation,我试图将ImageButton旋转180度,使其与反向纵向方向相匹配。当我以与其他方向更改相同的方式执行此操作时,结果是完美的,但动画不是这样 public void onOrientationChanged(int DeviceAngle) { float MyButtonCurrentAngle = MyButton.getRotation(); //Gets portrait rotation (0) if(DeviceAngle > 350 ||

我试图将ImageButton旋转180度,使其与反向纵向方向相匹配。当我以与其他方向更改相同的方式执行此操作时,结果是完美的,但动画不是这样

 public void onOrientationChanged(int DeviceAngle) {
      float MyButtonCurrentAngle = MyButton.getRotation(); //Gets portrait rotation (0)
           if(DeviceAngle > 350 || DeviceAngle < 10) { //Portrait
                MyButton.animate().rotationBy(- MyButtonCurrentAngle).setDuration(100).start();
           } else if(DeviceAngle > 80 && DeviceAngle < 100) { //Reverse Landscape
                MyButton.animate().rotationBy(-90 - MyButtonCurrentAngle ).setDuration(100).start();
            } else if(DeviceAngle > 170 && DeviceAngle < 190) { //Reverse Portrait
                MyButton.animate().rotationBy(180 -  MyButtonCurrentAngle).setDuration(100).start();
           } else if(DeviceAngle > 260 && DeviceAngle < 280) { //Landscape
                MyButton.animate().rotationBy(90 - MyButtonCurrentAngle ).setDuration(100).start();
           }          
已更改方向的公共无效(int DeviceAngle){
float MyButtonCurrThreading=MyButton.getRotation();//获取纵向旋转(0)
如果(DeviceAngle>350 | | DeviceAngle<10){//
MyButton.animate().rotationBy(-myButtonCurrengting).setDuration(100.start();
}否则,如果(DeviceAngle>80&&DeviceAngle<100){//反向风景
MyButton.animate().rotationBy(-90-MyButtonCurrengure).setDuration(100.start();
}如果(DeviceAngle>170&&DeviceAngle<190){//反向肖像
MyButton.animate().rotationBy(180-MyButtonCurrengure).setDuration(100.start();
}如果(DeviceAngle>260&&DeviceAngle<280){//
MyButton.animate().rotationBy(90-MyButtonCurrengure).setDuration(100.start();
}          
我认为会发生这种情况,因为float MyButtonCurrengure从350和10(0或360,纵向)之间的DeviceAngle获取MyButton旋转角度值(0或未旋转),并将其用作参考

尽管我仍然怀疑,我还是放弃了前一种情况。浮动似乎与其他方向配合得很好,我认为问题在于反向纵向方向的动画。按钮不应旋转180度,而应旋转90度或-90度。这是因为无法将设备从纵向旋转到反向端口它不经过任何横向选项。(不能直接旋转纵向以反转纵向)

在多次尝试失败后,我得出结论,在旋转按钮并且检测到方向为横向或反向横向后,我不能使用MyButton获取的角度值。我考虑创建另一个浮点数来获取反向纵向MyButton角度值,但此活动的方向设置为t哦,肖像,所以这没有意义

因此,我需要在使用浮点旋转按钮后获得其旋转角度,使用此值作为循环条件,并根据结果,在两个不同的动画中将其旋转90度或-90度。这是我解决此问题的最新方法:

    while (MyButtonCurrentAngle==90) { 
    if (DeviceAngle > 170 && DeviceAngle < 190) {
        MyButton.animate().rotationBy(90 - MyButtonCurrentAngle).setDuration(100).start();
    }
}
while (MyButtonCurrentAngle==270) { 
    if (DeviceAngle > 170 && DeviceAngle < 190) {
        MyButton.animate().rotationBy(-90 - MyButtonCurrentAngle).setDuration(100).start();
    }
}
while(MyButtonCurrEntrange==90){
如果(设备角>170和设备角<190){
MyButton.animate().rotationBy(90-MyButtonCurrengure).setDuration(100.start();
}
}
而(MyButtonCurrEngure==270){
如果(设备角>170和设备角<190){
MyButton.animate().rotationBy(-90-MyButtonCurrengure).setDuration(100.start();
}
}

基本上,这会处理从横向和反向横向到反向纵向的设备方向。这不会触发任何动画,因此MyButtonCurrengude浮动角度值从未更改或无法检测到?为什么if语句不能读取它?我不知道,我想知道我做错了什么。提前感谢。

if您发现此代码正在运行(通过断点调试或Logcat语句),然后尝试在UI线程上运行动画

YourActivity.runOnUiThread(new Runnable() {
    public void run() 
        MyButton.animate().rotationBy(90 - MyButtonCurrentAngle).setDuration(100).start();
    }
});
您可能还需要为此调用start。

解决方案:

  OrientationEventListener OrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) {
        static final int DELTA = 20;

        @Override 
        public void onOrientationChanged(int Angle) {
            float AngleDestination = ButtonDestination;
            if (angle > 90 - DELTA && angle < 90 + DELTA) {
                AngleDestination = -90;
            } else if (Angle > 180 - DELTA && Angle < 180 + DELTA) {
                AngleDestination = 180;
            } else if (Angle > 270 - DELTA && Angle < 270 + DELTA) {
                AngleDestination = 90;
            } else if (Angle > 360 - DELTA || Angle < DELTA) {
                AngleDestination = 0;
            } 
            if (AngleDestination != ButtonDestination) {
                ButtonDestination = AngleDestination;
                MyButton.animate().rotation(ButtonDestination).setDuration(100).start(); 
            } 
        } 
    }; 
    orientationEventListener.enable();
}
OrientationEventListener OrientationEventListener=新的OrientationEventListener(这是SensorManager.SENSOR\u DELAY\u UI){
静态最终积分增量=20;
@凌驾
公共空间onOrientationChanged(内角){
浮动角度目标=按钮目标;
如果(角度>90-增量和角度<90+增量){
目的地=-90;
}否则如果(角度>180-增量和角度<180+增量){
目的地=180;
}否则如果(角度>270-三角形和角度<270+三角形){
目的地=90;
}否则如果(角度>360-三角形| |角度<三角形){
目的地=0;
} 
如果(角度目标!=按钮目标){
按钮目标=角度目标;
MyButton.animate().rotation(ButtonDestination).setDuration(100.start();
} 
} 
}; 
orientationEventListener.enable();
}

发布xml、什么是
MyButtonCurrengure
ButtonsAngle
确保可以访问
ifs
中的代码。尝试在两个
ifs
MyButtonCurrengure中打印一些日志。MyButtons float的一个示例是通过getRotation获取其当前旋转值。ButtonsAngle是onOrientationChanged int,它们都不在xml文件中。一切都很完美,不会崩溃。问题是我想为我的按钮创建一个不同的动画,从90变为180,从270变为180。(换句话说,我希望从横向到反向纵向以及从横向到反向纵向的变化看起来与对应的[纵向])一样好。你确定if中的代码正在执行吗?我不确定,但我知道在我现在面临的反向纵向问题之前,一切都是完美的。(当然,这包括动画和xml文件。加上应用程序从未崩溃)。这将如何解决我的问题?其他动画确实会启动。问题是我无法检查按钮旋转角度值并给出足够的响应。所以我想我对这个问题感到困惑。重新阅读-您正在尝试将图像按钮旋转180度,与屏幕方向无关?是的,我编辑它是为了更好地解释自己。它不仅取决于方向(反向肖像),还取决于它到达该方向的方式。从横向(按钮旋转90度)或反向横向(按钮从原始位置旋转-90度)。