用这个imageview.animate().rotation(),怎么才能跨越 0f 和 360f 就近旋转,就是比如旋转角度是({0.0f, 24.0f, 48.0f, 72.0f, 96.0f, 120.0f, 144.0f, 168.0f, 192.0f, 216.0f, 240, 264.0f, 288.0f, 312.f, 336.0f,360.0f),当在24.0f时,点击336.0f处,控件逆时针旋转到336.0f处?哪位大哥指导一下,不胜感激
我的代码如下,但是这个只能就近距离同一个方向旋转两圈
public class MainActivity extends AppCompatActivity{
private static final String TAG ="MainActivity" ;
private TableCup ivtableOne;
private ImageView ivtableThere;
private int ivtableThereWidth;
private int ivtableThereHeight;
float temp = 0;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivtableOne = (TableCup) findViewById(R.id.tableOne);
ivtableThere = findViewById(R.id.tableThere);
ivtableThere.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_UP: //抬起的时候
onSingleTap(motionEvent);
break;
}
return true;
}
private void onSingleTap(MotionEvent e) {
float x = e.getX();
float y = e.getY();
float dst[] = new float[2];
//获取ImageView的matrix
Matrix matrix = ivtableThere.getMatrix();
Matrix inverseMatrix = new Matrix();
matrix.invert(inverseMatrix);
inverseMatrix.mapPoints(dst, new float[]{x, y});
float dstX = dst[0];
float dstY = dst[1];
//获取控件的宽高
int w = ivtableThere.getWidth();
int h = ivtableThere.getHeight();
Log.e(TAG, "w ==>" + w + "h ==>" + h);
Log.d(TAG, "dstX == >" + dstX + "***dstY ==>" + dstY);
if (x<350||x>720||y<260||y>610){
float jiaodu = (float) (Math.atan((dstY - h / 2) / (dstX - w / 2)) / Math.PI * 180); //角度
if (dstX <= w / 2) {
jiaodu = jiaodu + 90;
} else if (dstX > w / 2) {
jiaodu = jiaodu + 270;
}
final float hudu = (float) (jiaodu * (Math.PI / 180));
Log.d(TAG, "hudu ==>" + hudu + "===" + "jiaodu ==>" + jiaodu);
float a[] = {0.0f, 24.0f, 48.0f, 72.0f, 96.0f, 120.0f, 144.0f, 168.0f, 192.0f, 216.0f, 240, 264.0f, 288.0f, 312.f, 336.0f,360.0f};
Log.e(TAG,"temp==>"+temp);
float f = jiaodu-temp;
Log.e(TAG,"fff==>"+f);
for (int i = 0; i < 15; i++) {
if (jiaodu - temp >= 180 ) {
if (jiaodu >= a[i] && jiaodu < a[i + 1] && jiaodu <= (a[i] + a[i + 1]) / 2) {
ivtableOne.animate().rotation(temp).rotation(a[i] - 360.0f);
temp = (a[i] - 360.0f);
} else if (jiaodu > a[i] && jiaodu <= a[i + 1] && jiaodu > (a[i] + a[i + 1]) / 2) {
ivtableOne.animate().rotation(temp).rotation(a[i + 1] - 360.0f);
temp = (a[i + 1] - 360.0f);
}
} else if (jiaodu-temp < -180){
if (jiaodu >= a[i] && jiaodu < a[i + 1] && jiaodu <= (a[i] + a[i + 1]) / 2) {
ivtableOne.animate().rotation(temp).rotation(a[i] + 360.0f);
temp = (a[i] + 360.0f);
} else if (jiaodu > a[i] && jiaodu <= a[i + 1] && jiaodu > (a[i] + a[i + 1]) / 2) {
ivtableOne.animate().rotation(temp).rotation(a[i + 1] + 360.0f);
temp = (a[i + 1] + 360.0f);
}
}
else {
if (jiaodu >= a[i] && jiaodu < a[i + 1] && jiaodu <= (a[i] + a[i + 1]) / 2) {
ivtableOne.animate().rotation(a[i]);
temp = (a[i]);
} else if (jiaodu > a[i] && jiaodu <= a[i + 1] && jiaodu > (a[i] + a[i + 1]) / 2) {
ivtableOne.animate().rotation(a[i + 1]);
temp = a[i + 1];
}
}
}
}
}
});
}
}
分情况判断一下,分别计算正方向和逆方向角度,取绝对值小的方向,
1.比如30度到330度,正方向为330-30 = 300度
逆方向为30+(360-330)= 60 度 ,逆方向角度小,直接传入逆方向
2.90度到180度,正方向要90度,逆方向要90+360-180 = 270度,正方向角度小,传入正方向的角度。
RotateAnimation(0f,-60f,Animation.ABSOLUTview.getWidth()/2,Animation.ABSOLUTE,view.getHeight()/2/2) 第一个参数是起步角度,第二个参数是目标角度,负值为逆方向旋转
虽然没做过这类功能,但我觉得你应该单个点去突破。
1、图片旋转单独从某个方向的某个角度 - > 某个方向的某个角度
2、两者取其小的一个角度和方向的判断
3、两个都没问题了再结合两者去实现功能,哪个有问题就暂时突破哪个
要不你上代码,否则只能你自嗨。别人不知道你上下文的。