我正在尝试使用onTouch事件移动连续旋转的imageview,但是在重新设计了imageview之后。它不会围绕自己的轴旋转,而是围绕一条长圆形路径旋转。
我想要的是如下图所示:
使用onTouch事件重新定位后我得到的是:
到目前为止,我可以使用以下代码围绕自己的轴旋转图像:
RotateAnimation rotateanimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
我认为有些事情与pivotPoints有关,我无法弄清楚。以下是代码。
final ImageView iv = findViewById(R.id.imageView);
final RotateAnimation rotateAnimation;
RotateAnimation rotateanimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateanimation.setDuration(7000);
rotateanimation.setInterpolator(new LinearInterpolator());
rotateanimation.setRepeatCount(Animation.INFINITE);
rotateanimation.setFillAfter(true);
iv.startAnimation(rotateanimation);
iv.setOnTouchListener(new View.OnTouchListener() {
PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down
PointF StartPT = new PointF(); // Record Start Position of 'img'
@Override
public boolean onTouch(View v, final MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_MOVE :
PointF mv = new PointF( event.getX() - DownPT.x -1, event.getY() - DownPT.y -1);
iv.setX((int)(StartPT.x+mv.x));
iv.setY((int)(StartPT.y+mv.y));
iv.setPivotX(StartPT.x+mv.x);
iv.setPivotY(StartPT.y+mv.y);
//something to do here i guess
rotateanimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, iv.getPivotX(),
Animation.RELATIVE_TO_SELF, iv.getPivotY());
iv.startAnimation(rotateanimation);
/**************************************************************/
StartPT = new PointF( iv.getX(), iv.getY() );
break;
case MotionEvent.ACTION_DOWN :
int centerXOnImage= iv.getWidth()/2;
int centerYOnImage= iv.getHeight()/2;
DownPT.x =centerXOnImage;
DownPT.y =centerYOnImage;
StartPT = new PointF( iv.getX(), iv.getY() );
break;
case MotionEvent.ACTION_UP :
// Nothing have to do
break;
default :
break;
}
return true;
}
});
有什么建议吗?
答案 0 :(得分:1)
经过2天的头部刮擦和谷歌搜索后,我找到了方法。
您需要做的就是使用event.getRawX()和event.getRawY()而不是event.getX()和event.getY()。它将在用户手指下保持旋转imageview。
我将RotateAnimation更改为ObjectAnimation,因为当围绕播放动画涉及用户触摸时,标准动画将表现为奇怪。
以下是代码:
ObjectAnimator anim = ObjectAnimator.ofFloat(iv, "rotation",360f);
anim.setDuration(7000);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.start();
iv.setOnTouchListener(new View.OnTouchListener() {
PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down
PointF StartPT = new PointF(); // Record Start Position of 'img'
@Override
public boolean onTouch(View v, final MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_MOVE :
PointF mv = new PointF( event.getRawX() , event.getRawY());
iv.setX((int)event.getRawX() - DownPT.x);
iv.setY((int)event.getRawY() - DownPT.y);
StartPT = new PointF( iv.getX(), iv.getY() );
break;
case MotionEvent.ACTION_DOWN :
int centerXOnImage= iv.getWidth()/2;
int centerYOnImage= iv.getHeight()/2;
DownPT.x =centerXOnImage;
DownPT.y =centerYOnImage;
StartPT = new PointF( iv.getX(), iv.getY() );
anim.setDuration(2000);
anim.start();
break;
case MotionEvent.ACTION_UP :
//do stuff here
break;
default :
break;
}
return true;
}
});
希望将来可以帮助某人:)