76

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

Can someone please provide the code to accomplish this?

Pang
  • 9,564
  • 146
  • 81
  • 122
Matthew
  • 761
  • 1
  • 6
  • 3

13 Answers13

74

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

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Jens Zalzala
  • 2,546
  • 1
  • 22
  • 28
  • 3
    @GerhardBarnard setRotation() is the method to use and is the answer itself. The link is only there for convenience. – Jens Zalzala Dec 05 '17 at 18:58
60

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);
Pete
  • 1,191
  • 12
  • 19
  • Nice to see that things that can't be done defining layouts can be achieved with animations and setFillAfter(true). – Andras Balázs Lajtha May 22 '12 at 06:02
  • This will rotate the view all right, but buttons wont have their functionality rotated. Not the holy grail for me :) – Warpzit Jun 04 '12 at 13:13
  • 1
    It turns out that there isn't any way to rotate functionality of a button. I ended up forwarding events to the buttons based on where the screen was touched. – Warpzit Dec 12 '12 at 10:05
47

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

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

} 
Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
Ichorus
  • 4,567
  • 6
  • 38
  • 46
  • Ok that sorta accomplish's what i want. I want the entire view itself to be on a 45 degree angle. Just not the text inside. But thank you for answering my question – Matthew Dec 20 '09 at 16:39
  • 3
    If you want to rotate the entire view (along with its background), you should oveerride draw() instead of onDraw(). – Alexey Jul 10 '12 at 13:33
  • ok it works if using draw()method rotate the whole button but not drawing view correctly – Kailash Dabhi Nov 19 '13 at 08:46
  • There is no need to save whole canvas state, canvas.save(Canvas.MATRIX_SAVE_FLAG) is enough. – Denis Gladkiy Mar 07 '16 at 06:20
30

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

myCusstomView.setRotation(45);

Hope it works for you.

Rudi
  • 4,304
  • 4
  • 34
  • 44
27

One line in XML


<View
    android:rotation="45"
    ... />
Michael
  • 9,639
  • 3
  • 64
  • 69
23

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

// 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);
DYS
  • 2,826
  • 2
  • 23
  • 34
7

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:

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

enter image description here

Dmitry Ryadnenko
  • 22,222
  • 4
  • 42
  • 56
6

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

setRotation() method preserves buttons functionality.

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

Michael S
  • 69
  • 1
  • 3
3

As mentioned before, the easiest way it to use rotation available since API 11:

android:rotation="90"    // in XML layout

view.rotation = 90f      // programatically

You can also change pivot of rotation, which is by default set to center of the view. This needs to be changed programatically:

// top left
view.pivotX = 0f
view.pivotY = 0f

// bottom right
view.pivotX = width.toFloat()
view.pivotY = height.toFloat()

...

In Activity's onCreate() or Fragment's onCreateView(...) width and height are equal to 0, because the view wasn't measured yet. You can access it simply by using doOnPreDraw extension from Android KTX, i.e.:

view.apply {
    doOnPreDraw {
        pivotX = width.toFloat()
        pivotY = height.toFloat()
    }
}
Micer
  • 8,731
  • 3
  • 79
  • 73
2

if you wish to make it dynamically with an animation:

view.animate()
    .rotation(180)
    .start();

THATS IT

Ivan Yulin
  • 794
  • 7
  • 15
1
fun rotateArrow(view: View): Boolean {
    return if (view.rotation == 0F) {
        view.animate().setDuration(200).rotation(180F)
        true
    } else {
         view.animate().setDuration(200).rotation(0F)
         false
    }
}
Job M
  • 3,331
  • 2
  • 19
  • 26
1

@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:

(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)

canvas.save();
canvas.rotate(theta, pivot.x, pivot.y);
canvas.drawRect(horizontalRect, paint);
canvas.restore();
gauravjain0102
  • 257
  • 2
  • 3
0

That's simple, in Java

your_component.setRotation(15);

or

your_component.setRotation(295.18f);

in XML

<Button android:rotation="15" />