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

时间:2017-02-14 10:54:05

标签: android image-rotation

如何将所有五个图像放置在中心白色中,在中心点旋转时钟和反时钟。我试过了,但我得到这样的结果(下图) enter image description here 截至目前,它不是在中心点旋转。

这是我的代码。

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" />

感谢您提前。

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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

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

[
{
date: "15-05-2018",
transactions: [Transaction]
},
{
date: "16-05-2018",
transactions: [Transaction]
}
]

在您的活动中:

(kotlin):

<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>

(java):

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