在Android中旋转视图

Mat*_*hew 66 android view rotation

我有一个按钮,我想放在45度角.出于某种原因,我不能让这个工作.

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

Jen*_*ala 66

API 11 为所有视图添加了setRotation()方法.

  • @GerhardBarnard setRotation()是要使用的方法,它本身就是答案。链接只是为了方便。 (2认同)

Pet*_*ete 58

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

    // 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);
Run Code Online (Sandbox Code Playgroud)


Ich*_*rus 46

扩展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();

} 
Run Code Online (Sandbox Code Playgroud)

  • 如果要旋转整个视图(及其背景),则应该使用draw()而不是onDraw(). (3认同)

Rud*_*udi 26

我只是在我的代码中使用了简单的行,它的工作原理如下:

myCusstomView.setRotation(45);
Run Code Online (Sandbox Code Playgroud)

希望对你有效.

  • 注意:此方法自API 11(Honeycomb)开始提供 (4认同)

Mic*_*ael 24

XML中的一行


<View
    android:rotation="45"
    ... />
Run Code Online (Sandbox Code Playgroud)


DYS*_*DYS 20

应用旋转动画(没有持续时间,因此没有动画效果)是比调用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);
Run Code Online (Sandbox Code Playgroud)


Dmi*_*nko 7

旋转视图rotate()不会影响视图的测量大小.因此,旋转视图将被剪裁或不适合父布局.这个库修复了它:

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

在此输入图像描述


小智 6

Joininig @Rudi和@ Pete的答案.我创建了一个RotateAnimation,它在旋转后也保持按钮功能.

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

代码示例:

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);
Run Code Online (Sandbox Code Playgroud)

mainLayout是一个(LinearLayout)字段