
我希望我的圆形图像(ImageVIEw)随着用户的触摸旋转并拖动它.如果用户将其拖动到右侧,则应向右旋转,反之亦然.就像你旋转DJ碟片一样,如果你知道我的意思.我用OntouchListener和RotateAnimation玩了一下但是我无处可去.
有任何想法吗?
解决方法:
假设您要旋转ImageVIEw mCircle.您必须使用RotateAnimation旋转它.在Ontouch方法中,确定用户手指的角度.
例如,在您的主要活动中执行以下 *** 作
private ImageVIEw mCircle;private double mCurrAngle = 0;private double mPrevAngle = 0;@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); mCircle = (ImageVIEw) findVIEwByID(R.ID.circle); mCircle.setontouchListener(this); // Your activity should implement OntouchListener}@OverrIDepublic boolean ontouch(VIEw v, MotionEvent event) { final float xc = mCircle.getWIDth() / 2; final float yc = mCircle.getHeight() / 2; final float x = event.getX(); final float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { mCircle.clearanimation(); mCurrAngle = Math.todegrees(Math.atan2(x - xc, yc - y)); break; } case MotionEvent.ACTION_MOVE: { mPrevAngle = mCurrAngle; mCurrAngle = Math.todegrees(Math.atan2(x - xc, yc - y)); animate(mPrevAngle, mCurrAngle, 0); break; } case MotionEvent.ACTION_UP : { mPrevAngle = mCurrAngle = 0; break; } } return true;}private voID animate(double fromdegrees, double todegrees, long durationMillis) { final RotateAnimation rotate = new RotateAnimation((float) fromdegrees, (float) todegrees, RotateAnimation.relative_TO_SELF, 0.5f, RotateAnimation.relative_TO_SELF, 0.5f); rotate.setDuration(durationMillis); rotate.setFillEnabled(true); rotate.setFillAfter(true); mCircle.startAnimation(rotate);} 总结 以上是内存溢出为你收集整理的android – 如何使用RotateAnimation旋转圆圈?全部内容,希望文章能够帮你解决android – 如何使用RotateAnimation旋转圆圈?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)