在 Android 中旋转视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1930963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:49:39  来源:igfitidea点击:

Rotating a view in Android

androidviewrotation

提问by Matthew

I have a button that I want to put on a 45 degree angle. For some reason I can't get this to work.

我有一个按钮,我想把它放在 45 度角上。出于某种原因,我无法让它发挥作用。

Can someone please provide the code to accomplish this?

有人可以提供代码来完成这个吗?

回答by Jens Zalzala

API 11 added a setRotation()method to all views.

API 11向所有视图添加了setRotation()方法。

回答by Pete

You could create an animation and apply it to your button view. For example:

您可以创建动画并将其应用于按钮视图。例如:

    // Locate view
    ImageView diskView = (ImageView) findViewById(R.id.imageView3);

    // Create an animation instance
    Animation an = new RotateAnimation(0.0f, 360.0f, pivotX, pivotY);

    // Set the animation's parameters
    an.setDuration(10000);               // duration in ms
    an.setRepeatCount(0);                // -1 = infinite repeated
    an.setRepeatMode(Animation.REVERSE); // reverses each repeat
    an.setFillAfter(true);               // keep rotation after animation

    // Aply animation to image view
    diskView.setAnimation(an);

回答by Ichorus

Extend the TextViewclass and override the onDraw()method. Make sure the parent view is large enough to handle the rotated button without clipping it.

扩展TextView类并覆盖onDraw()方法。确保父视图足够大以处理旋转的按钮而不剪裁它。

@Override
protected void onDraw(Canvas canvas) {
     canvas.save();
     canvas.rotate(45,<appropriate x pivot value>,<appropriate y pivot value>);
     super.onDraw(canvas);
     canvas.restore();

} 

回答by Rudi

I just used the simple line in my code and it works :

我只是在我的代码中使用了简单的一行,它可以工作:

myCusstomView.setRotation(45);

Hope it works for you.

希望对你有效。

回答by Michael

One line in XML

XML 中的一行



<View
    android:rotation="45"
    ... />

回答by DYS

Applying a rotation animation (without duration, thus no animation effect) is a simpler solution than either calling View.setRotation() or override View.onDraw method.

应用旋转动画(没有持续时间,因此没有动画效果)是比调用 View.setRotation() 或覆盖 View.onDraw 方法更简单的解决方案。

// substitude deltaDegrees for whatever you want
RotateAnimation rotate = new RotateAnimation(0f, deltaDegrees, 
    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  

// prevents View from restoring to original direction. 
rotate.setFillAfter(true); 

someButton.startAnimation(rotate);

回答by Dmitry Ryadnenko

Rotating view with rotate()will not affect your view's measured size. As result, rotated view be clipped or not fit into the parent layout. This library fixes it though:

旋转视图rotate()不会影响视图的测量尺寸。结果,旋转的视图被剪裁或不适合父布局。不过这个库修复了它:

https://github.com/rongi/rotate-layout

https://github.com/rongi/rotate-layout

enter image description here

在此处输入图片说明

回答by Michael S

Joininig @Rudi's and @Pete's answers. I have created an RotateAnimation that keeps buttons functionality also after rotation.

加入@Rudi 和@Pete 的回答。我创建了一个 RotateAnimation,它在旋转后也保持按钮功能。

setRotation() method preserves buttons functionality.

setRotation() 方法保留按钮功能。

Code Sample:

代码示例:

Animation an = new RotateAnimation(0.0f, 180.0f, mainLayout.getWidth()/2, mainLayout.getHeight()/2);

    an.setDuration(1000);              
    an.setRepeatCount(0);                     
    an.setFillAfter(false);              // DO NOT keep rotation after animation
    an.setFillEnabled(true);             // Make smooth ending of Animation
    an.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {}

        @Override
        public void onAnimationRepeat(Animation animation) {}

        @Override
        public void onAnimationEnd(Animation animation) {
                mainLayout.setRotation(180.0f);      // Make instant rotation when Animation is finished
            }
            }); 

mainLayout.startAnimation(an);

mainLayout is a (LinearLayout) field

mainLayout 是一个 (LinearLayout) 字段

回答by gauravjain0102

@Ichorus's answer is correct for views, but if you want to draw rotated rectangles or text, you can do the following in your onDraw (or onDispatchDraw) callback for your view:

@Ichorus 的答案对于视图是正确的,但是如果您想绘制旋转的矩形或文本,您可以在 onDraw(或 onDispatchDraw)回调中为您的视图执行以下操作:

(note that theta is the angle from the x axis of the desired rotation, pivot is the Point that represents the point around which we want the rectangle to rotate, and horizontalRect is the rect's position "before" it was rotated)

(请注意,theta 是所需旋转的 x 轴的角度,pivot 是表示我们希望矩形围绕其旋转的点的 Point,而horizo​​ntalRect 是矩形在“之前”旋转的位置)

canvas.save();
canvas.rotate(theta, pivot.x, pivot.y);
canvas.drawRect(horizontalRect, paint);
canvas.restore();

回答by Affa Musaffa

That's simple, in Java

很简单,在Java中

your_component.setRotation(15);

or

或者

your_component.setRotation(295.18f);

in XML

以 XML 格式

<Button android:rotation="15" />