Program rotation with the ability to rotate or view

<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/your_drawable" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360" /> 

I want to rotate the selection programmatically.

How should I do it?

Here is my callback

 private class RotateListener implements RotateGestureDetector.OnRotateGestureListener{ @Override public boolean onRotate(MotionEvent event1, MotionEvent event2, double deltaAngle) { return true; } } 

deltaangle no more than 0.1, I'm not sure what extract is.

+8
java android rotation
Feb 26 '13 at 7:39
source share
3 answers

The following code rotates the ImageView around its center:

 ImageView myImageView = (ImageView)findViewById(R.id.my_imageview); AnimationSet animSet = new AnimationSet(true); animSet.setInterpolator(new DecelerateInterpolator()); animSet.setFillAfter(true); animSet.setFillEnabled(true); final RotateAnimation animRotate = new RotateAnimation(0.0f, -90.0f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f); animRotate.setDuration(1500); animRotate.setFillAfter(true); animSet.addAnimation(animRotate); myImageView.startAnimation(animSet); 
+33
Feb 27 '13 at 8:24
source share

here's a good rotation solution with rotatability for the image:

 Drawable getRotateDrawable(final Bitmap b, final float angle) { final BitmapDrawable drawable = new BitmapDrawable(getResources(), b) { @Override public void draw(final Canvas canvas) { canvas.save(); canvas.rotate(angle, b.getWidth() / 2, b.getHeight() / 2); super.draw(canvas); canvas.restore(); } }; return drawable; } 

using:

 Bitmap b=... float angle=... final Drawable rotatedDrawable = getRotateDrawable(b,angle); root.setImageDrawable(rotatedDrawable); 

another alternative:

 private Drawable getRotateDrawable(final Drawable d, final float angle) { final Drawable[] arD = { d }; return new LayerDrawable(arD) { @Override public void draw(final Canvas canvas) { canvas.save(); canvas.rotate(angle, d.getBounds().width() / 2, d.getBounds().height() / 2); super.draw(canvas); canvas.restore(); } }; } 

also, if you want to rotate the bitmap, but are afraid of OOM, you can use the NDK solution that I made here

+11
Jan 27 '14 at 8:27
source share

Since you are trying to use Almero Android Gesture Detectors, I decided to do the same to find a suitable solution:

 public class MainActivity extends Activity { private RotateGestureDetector mRotateDetector; private float mRotationDegrees = 0.f; private static final float ROTATION_RATIO = 2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRotateDetector = new RotateGestureDetector(getApplicationContext(), new RotateListener()); } @Override public boolean onTouchEvent(MotionEvent event) { mRotateDetector.onTouchEvent(event); return super.onTouchEvent(event); } private class RotateListener extends RotateGestureDetector.SimpleOnRotateGestureListener { @Override public boolean onRotate(RotateGestureDetector detector) { mRotationDegrees -= detector.getRotationDegreesDelta(); ImageView v = (ImageView) findViewById(R.id.imageView); // For NineOldAndroids library only! ViewHelper.setRotation(v, mRotationDegrees * ROTATION_RATIO); // For HONEYCOMB and later only! v.setRotation(mRotationDegrees * ROTATION_RATIO); return true; } } } 

This works fine for me (I can rotate the ImageView with a two-finger rotation gesture. NOTE: Remember to select the appropriate rotation method call. I commented on both of them to get your attention.

ROTATION_RATIO is just a multiplier to accelerate the rotation reaction to finger movement. You can use any rotation axis (setRotation (), setRotationX () and setRotationY () methods for presentation.

To enable support for this code on Android devices with an API level below 11 (pre-cellular devices), you might want to use the NineOldAndroid library.

+3
Feb 27 '13 at 8:19
source share



All Articles