如何定位所有在中心点旋转的图像 - android

How to position all images rotating in center point - 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" />

感谢提前。

问题出在你 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);

使用对象动画。设置动画如下。

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();

哦,将 imageView 移动到另一个(底部)视图的中心真的很容易:

rotate_anim.xml:(请注意数据透视参数)

<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
     <rotate
        android:fromDegrees="0"
        android:toDegrees="90"
        android:pivotX="50%"
        android:pivotY="100%"
        android:duration="1000" />
</set>

在你的activity中:

(科特林):

val an = AnimationUtils.loadAnimation(this, R.anim.rotate_anim)
an.fillAfter = true
sector.startAnimation(an)

(java):

Animation an = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
an.fillAfter = true
sector.startAnimation(an)