如何定位在中心点旋转的所有图像 - android
问题描述:
如何在中心点以中心白色旋转时钟方式和反时钟方式放置所有五个图像。我尝试过,但我得到这样的(下图) 截至目前,它不在中心点旋转。如何定位在中心点旋转的所有图像 - android
这是我的代码。
public class StartRotatingActivity extends AppCompatActivity implements Animation.AnimationListener {
private Context mContext;
private ImageView timeBackground_1, timeBackground_2, timeBackground_3, timeBackground_4, timeBackground_5;
Animation animationRight, animationLeft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
if (getSupportActionBar() != null)
getSupportActionBar().hide();
mContext = this;
timeBackground_1 = (ImageView) findViewById(R.id.bgTimer1);
timeBackground_2 = (ImageView) findViewById(R.id.bgTimer2);
timeBackground_3 = (ImageView) findViewById(R.id.bgTimer3);
timeBackground_4 = (ImageView) findViewById(R.id.bgTimer4);
timeBackground_5 = (ImageView) findViewById(R.id.bgTimer5);
animationRight = AnimationUtils.loadAnimation(this, R.anim.rotate_right);
animationRight.setDuration(12000);
animationLeft = AnimationUtils.loadAnimation(this, R.anim.rotate_left);
animationLeft.setDuration(12000);
timeBackground_1.startAnimation(animationRight);
timeBackground_2.startAnimation(animationLeft);
timeBackground_3.startAnimation(animationRight);
timeBackground_4.startAnimation(animationLeft);
timeBackground_5.startAnimation(animationRight);
}
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
startActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center"
android:orientation="vertical">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:id="@+id/bgTimer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
app:srcCompat="@mipmap/bg_time_1" />
<ImageView
android:id="@+id/bgTimer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
app:srcCompat="@mipmap/bg_time_2" />
<ImageView
android:id="@+id/bgTimer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
app:srcCompat="@mipmap/bg_time_3" />
<ImageView
android:id="@+id/bgTimer4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
app:srcCompat="@mipmap/bg_time_4" />
<ImageView
android:id="@+id/bgTimer5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
app:srcCompat="@mipmap/bg_time_5" />
</FrameLayout>
</LinearLayout>
Rorateleft.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="360" />
Rotateright.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="0" />
感谢提前。
答
使用对象动画。设置动画如下。
ObjectAnimator one = ObjectAnimator.ofFloat(timeBackground_1 ,
"rotation", 0f, 360f);
one.setDuration(5000);
one.setRepeatCount(-1);
one.start();
ObjectAnimator two = ObjectAnimator.ofFloat(timeBackground_2 ,
"rotation", 360f, 0f);
two.setDuration(5000);
two.setRepeatCount(-1);
two.start();
ObjectAnimator three = ObjectAnimator.ofFloat(timeBackground_3 ,
"rotation", 0f, 360f);
three.setDuration(5000);
three.setRepeatCount(-1);
three.start();
ObjectAnimator four = ObjectAnimator.ofFloat(timeBackground_4 ,
"rotation", 360f, 0f);
four.setDuration(5000);
four.setRepeatCount(-1);
four.start();
ObjectAnimator five = ObjectAnimator.ofFloat(timeBackground_5 ,
"rotation", 0f, 360f);
five.setDuration(5000);
five.setRepeatCount(-1);
five.start();
+1
它很好用。谢谢 – akk
答
的问题是在你的支点在Rotateleft.xml
android:pivotX="70%"
android:pivotY="30%"
使他们:
android:pivotX="50%"
android:pivotY="50%"
你也可以尝试在代码:
Animation a = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
a.setRepeatCount(Animation.INFINITE);
a.setDuration(12000);
你可以发布你的布局xml吗? –
@PratikPopat添加了布局xml – akk