Programmatically rotate drawable or view

前端 未结 3 1634
有刺的猬
有刺的猬 2020-11-30 08:03


        
相关标签:
3条回答
  • 2020-11-30 08:25

    The following code rotates an 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);
    
    0 讨论(0)
  • 2020-11-30 08:25

    here's a nice solution for putting a rotated drawable for an imageView:

    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;
    }
    

    usage:

    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 wish to rotate the bitmap, but afraid of OOM, you can use an NDK solution i've made here

    0 讨论(0)
  • 2020-11-30 08:41

    Since you're trying to use Almero's Android Gesture Detectors, I decided to do the same in order to find an appropriate 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;
            }
        }
    }
    

    It works fine for me (I'm able to rotate the ImageView with two-finger rotation gesture. NOTE: Don't forget to chose appropriate rotation method call. I commented both of them to draw your attention.

    ROTATION_RATIO is just a multiplier to speed-up a rotation response on my fingers movement. You can use any rotation axis (setRotation(), setRotationX() and setRotationY()) methods for a View.

    To enable support of this code on Android devices with API Level lower than 11 (pre-Honeycomb devices) you may want to engage NineOldAndroid library.

    0 讨论(0)
提交回复
热议问题