我正在开展一个项目,要求我同时为2个视图制作动画。
状态0: 2个观看次数位于圈子的x1 = -Radius, y1 = 0 / x2 = Radius, y2 = 0
。
状态1:他们应该同时移动到圆圈的顶部中心并发生碰撞(如钟摆)
状态2:他们应该同时回到圆圈的底部中心并且也会碰撞
状态3 .. N:他们应该到顶部中心碰撞然后再回到底部中心并再次碰撞......
我尝试了多个库,包括ArcAnimator,但似乎没有人支持这个。
有没有办法做到这一点?仅通过XML或以编程方式并不重要。
编辑:
这是我到目前为止所做的:
private void startAnimation () {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager ().getDefaultDisplay ().getMetrics (metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
float scale = metrics.density;
float initLeftX = leftBall.getX ();
float initLeftY = leftBall.getY ();
float targetLeftX = topHelperView.getX (); // helper view on top of circle.
float targetLeftY = topHelperView.getY ();
float initRightX = rightBall.getX ();
float initRightY = rightBall.getY ();
float targetRightX = topHelperView.getX ();
float targetRightY = topHelperView.getY ();
AnimatorSet animSet = new AnimatorSet ();
ObjectAnimator anim1 = ObjectAnimator.ofFloat (leftBall, "x", initLeftX, targetLeftX);
ObjectAnimator anim2 = ObjectAnimator.ofFloat (leftBall, "y", initLeftY, targetLeftY);
ObjectAnimator anim3 = ObjectAnimator.ofFloat (rightBall, "x", initRightX, targetRightX);
ObjectAnimator anim4 = ObjectAnimator.ofFloat (rightBall, "y", initRightY, targetRightY);
animSet.play (anim1).with (anim2).with (anim3).with (anim4);
animSet.setDuration (5000);
animSet.start ();
}
然而问题是动画线性地进行并且不跟随外圆的弯曲路径。我无法想到任何实现钟摆碰撞的概念。
答案 0 :(得分:0)
此示例代码需要进行一些调整,但我相信它会对您有所帮助:
package com.pachacuti.bouncingball;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics displayMetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int windowHeight = displayMetrics.heightPixels;
int windowWidth = displayMetrics.widthPixels;
final RotateAnimation rAnimLeft1 = new RotateAnimation(0, 90,
Animation.ABSOLUTE, windowWidth/2, Animation.ABSOLUTE, 0);
rAnimLeft1.setDuration(4000);
final RotateAnimation rAnimLeft2 = new RotateAnimation(0, -180,
Animation.ABSOLUTE, windowWidth/2, Animation.ABSOLUTE, 0);
rAnimLeft2.setDuration(4000);
rAnimLeft2.setRepeatCount(-1);
rAnimLeft2.setRepeatMode(2);
final RotateAnimation rAnimRight1 = new RotateAnimation(0, -90,
Animation.ABSOLUTE, -windowWidth/2, Animation.ABSOLUTE, 0);
rAnimRight1.setDuration(4000);
final RotateAnimation rAnimRight2 = new RotateAnimation(0, 180,
Animation.ABSOLUTE, -windowWidth/2, Animation.ABSOLUTE, 0);
rAnimRight2.setDuration(4000);
rAnimRight2.setRepeatCount(-1);
rAnimRight2.setRepeatMode(2);
final View bleft = findViewById(R.id.bLeft);
AnimationSet animSetLeft = new AnimationSet(true);
animSetLeft.addAnimation(rAnimLeft1);
animSetLeft.addAnimation(rAnimLeft2);
bleft.startAnimation(animSetLeft);
final View bright = findViewById(R.id.bRight);
AnimationSet animSetRight = new AnimationSet(true);
animSetRight.addAnimation(rAnimRight1);
animSetRight.addAnimation(rAnimRight2);
bright.startAnimation(animSetRight);
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.moahh.bouncingball.MainActivity">
<View
android:id="@+id/bLeft"
android:layout_width="10px"
android:layout_height="10px"
android:background="@color/colorAccent"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
/>
<View
android:id="@+id/bRight"
android:layout_width="10px"
android:layout_height="10px"
android:background="@color/colorAccent"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>