Android rotation

I have a button that I want to set at an angle of 45 degrees. For some reason I can't get this to work.

Can someone ask for a code for this?

+53
android rotation view
Dec 18 '09 at 22:09
source share
9 answers

API 11 has added the setRotation () method to all views.

+56
Mar 08 2018-12-12T00:
source share
— -

You can create an animation and apply it to the view in the panel. 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 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); 
+55
Sep 28 '11 at 22:04
source share

Extend the TextView class and override the onDraw() method. Make sure that the parent view is large enough to handle the rotary 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(); } 
+43
Dec 19 '09 at 3:15
source share

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

 myCusstomView.setRotation(45); 

Hope this works for you.

+18
Nov 22 '13 at 6:55
source share

Applying rotation animation (no duration, no animation effect) is a simpler solution than calling the View.setRotation () method or overriding the 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); 
+15
Feb 26 '14 at 6:03
source share

One line in XML




 <View android:rotation="45" ... /> 
+11
Sep 14 '15 at 17:36
source share

Answers Joininig @Rudi and @Pete. I created RotateAnimation, which also retains the functions of buttons after rotation.

The setRotation () method preserves the functionality of buttons.

Code example:

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

+6
Jan 04 '14 at 16:16
source share

A rotating view with rotate() will not affect the size of the measured size. There is only a view of the rendering, and as a result, the rotating view will make it clipped or not fit into the parent layout. This library fixes this though:

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

enter image description here

+5
Mar 26 '14 at 11:23
source share

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

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

 canvas.save(); canvas.rotate(theta, pivot.x, pivot.y); canvas.drawRect(horizontalRect, paint); canvas.restore(); 
+1
Dec 26 '11 at 11:03
source share



All Articles