Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 点阵列动画_Android_Animation_Android Animation - Fatal编程技术网

Android 点阵列动画

Android 点阵列动画,android,animation,android-animation,Android,Animation,Android Animation,我相信有一个简单的方法可以做到这一点,但我被卡住了。 假设我有一个要点列表: Point[] list = {pointA, pointB, pointC, ...} 我想通过每个点设置ImageView的动画 所以我试了一下: id = 0; AnimatorListenerAdapter animEnd = new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animat

我相信有一个简单的方法可以做到这一点,但我被卡住了。 假设我有一个要点列表:

Point[] list = {pointA, pointB, pointC, ...}
我想通过每个点设置ImageView的动画 所以我试了一下:

id = 0;
AnimatorListenerAdapter animEnd = new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        id++;
        if(id != list.length) {
            iv.animate()
                .translationX(list[id].getX())
                .translationY(list[id].getY())
                .setDuration(200)
                .setListener(this);
        }
    }
};

iv.animate()
    .translationX(list[id].getX()).translationY(list[id].getY())
    .setDuration(200).setListener(animEnd);
它可以工作,但是在每个动画之间都有一点延迟


有什么想法吗?谢谢

您可能会在动画步骤之间获得延迟,因为您总是在从一步到另一步的每次转换中启动新动画。要克服这种情况,您有多种选择

关键帧 您可以找到一种称为关键帧动画的技术,这是一种非常常见的动画技术,可能正是您想要的

关键帧对象由时间/值对组成,可用于在动画的特定时间定义特定状态。每个关键帧还可以具有自己的插值器,以控制上一个关键帧的时间与此关键帧的时间之间的间隔内动画的行为

在本例中,您可以将
列表中的点映射到
关键帧
实例列表,然后

Point[] list = {pointA, pointB, pointC, ...}
List<Keyframe> kfs = new ArrayList<Keyframe>();
foreach (Point p : points) {
  Keyframe kf = new Keyframe.ofFloat(p.x); // or what ever
  kfs.add(kf);
}
第二个参数是变量参数列表,它也接受数组作为输入。因此,应该可以以某种方式将
kfs
作为第二个参数传递给该方法

在您的情况下,我将设计以下方法,因为您是为dalvik VM开发的,所以不能使用java8 lambda表达式:

// maps points to X/Y float values
List<Float> toArrayX(Point[] points) { ... }
List<Float> toArrayY(Point[] points) { ... }
// maps float values to Keyframes
List<Keyframe> toKeyframes(List<Float> floats) { ... }
void createAnimation(Point[] points) {
  List<Keyframe> xs = toKeyframes(toArrayX(points));
  PropertyValuesHolder phvX = PropertyValuesHolder
    .ofKeyframe("translationX", xs);

  List<Keyframe> ys = toKeyframes(toArrayY(points));
  PropertyValuesHolder phvY = PropertyValuesHolder
    .ofKeyframe("translationY", ys);

  linkPropertyValuesHolder(phvX);
  linkPropertyValuesHolder(phvY);
}
void linkPropertyValuesHolder(PropertyValuesHolder phv) {
  // setup target
  ObjectAnimator anim = ObjectAnimator
    .ofPropertyValuesHolder(target, phv)
  anim.setDuration(5000ms);
}
自定义插值器 当给定的插值器不够灵活时,您也可以实现
插值器
接口,然后从中创建一个新实例。接口非常狭窄,它只提供一个使用以下签名调用的方法

public abstract float getInterpolation (float input)    
代码与XML 最后一个选项是将所有动画配置移动到XML中,而不是将这些细节直接烘焙到代码的二进制分布中。但是,如果可能的话,每个给定的选项都需要不同的设置才能通过XML进行管理

希望这有帮助,但这只是伪代码。我没有测试代码。。。因此,不保证正确性

// maps points to X/Y float values
List<Float> toArrayX(Point[] points) { ... }
List<Float> toArrayY(Point[] points) { ... }
// maps float values to Keyframes
List<Keyframe> toKeyframes(List<Float> floats) { ... }
void createAnimation(Point[] points) {
  List<Keyframe> xs = toKeyframes(toArrayX(points));
  PropertyValuesHolder phvX = PropertyValuesHolder
    .ofKeyframe("translationX", xs);

  List<Keyframe> ys = toKeyframes(toArrayY(points));
  PropertyValuesHolder phvY = PropertyValuesHolder
    .ofKeyframe("translationY", ys);

  linkPropertyValuesHolder(phvX);
  linkPropertyValuesHolder(phvY);
}
void linkPropertyValuesHolder(PropertyValuesHolder phv) {
  // setup target
  ObjectAnimator anim = ObjectAnimator
    .ofPropertyValuesHolder(target, phv)
  anim.setDuration(5000ms);
}
Path path = new Path();
path.lineTo(0.25f, 0.25f);
path.moveTo(0.25f, 0.5f);
path.lineTo(1f, 1f);
public abstract float getInterpolation (float input)