翻译动画坐标会发出多个设备

时间:2016-04-22 02:34:02

标签: android animation

我想将一个对象从屏幕中心转换到屏幕的左上角。

我希望翻译动画在所有设备上都相同 目前我给出的翻译值在不同的设备上是不同的[在对象的动画位置不同之后]。

有没有办法翻译和传递值,这会导致动画在各个设备上相同?

enter image description here

我的代码

ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(progressWidget, "scaleX", 0.355f);
        ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(progressWidget, "scaleY", 0.355f);
        scaleDownX.setDuration(3000);
        scaleDownY.setDuration(3000);

        ObjectAnimator translateX = ObjectAnimator.ofFloat(progressWidget, "translationX", -435f);
        translateX.setRepeatCount(0);
        translateX.setDuration(3000);

        ObjectAnimator translateY = ObjectAnimator.ofFloat(progressWidget, "translationY", -295f);
        translateY.setRepeatCount(0);
        translateY.setDuration(3000);


        //sequential animation
        AnimatorSet set = new AnimatorSet();
        set.play(scaleDownX).with(scaleDownY).with(translateX).with(translateY);
        //set.start();

3 个答案:

答案 0 :(得分:1)

    View progressWidget=null;

    progressWidget.setPivotX(0);
    progressWidget.setPivotY(0);

    ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(progressWidget, "scaleX", 0.355f);
    ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(progressWidget, "scaleY", 0.355f);
    scaleDownX.setDuration(3000);
    scaleDownY.setDuration(3000);


    //the top left corner of screen location is [0,0], get ProgressBar left top location in screen use getLocationInWindow
    int[] location = new int[2];
    progressWidget.getLocationInWindow(location);

    ObjectAnimator translateX = ObjectAnimator.ofFloat(progressWidget, "translationX", -location[0]);//location[0] is x location, so move -location[0]
    translateX.setRepeatCount(0);
    translateX.setDuration(3000);

    ObjectAnimator translateY = ObjectAnimator.ofFloat(progressWidget, "translationY", -location[1]);//location[1] is y location, so move -location[1]
    translateY.setRepeatCount(0);
    translateY.setDuration(3000);

答案 1 :(得分:1)

第一:你必须创建xml有两个控件

控件1的名称 mControlStart 位于 中心布局

控件2的名称 mControlFinish 位于 左上角 并且具有属性

  android:visibility="invisible"

然后在OnClick

private void setUpAnimation() {
    int[] startPosition = new int[2];
    int[] endPosition = new int[2];
    mControlStart.getLocationInWindow(startPosition);
    mControlFinish.getLocationInWindow(endPosition);
    int offset = endPosition[1] - startPosition[1];
    TranslateAnimation animation = new TranslateAnimation(0, offset, 0, 0);
    animation.setDuration(1000);
    animation.setFillAfter(true);
    mControlStart.startAnimation(animation);
    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mControlStart.setVisibility(View.GONE);
            mControlFinish.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
}

答案 2 :(得分:1)

你可以这样做:

enter image description here

XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    >

    <ImageView
        android:id="@+id/fragment_imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_state_list_drawable"
        android:clickable="true"
        android:padding="20dp"
        android:src="@drawable/ic_launcher"/>

</LinearLayout>

Java代码:

@Override
public void onViewCreated(final View view, Bundle state) {
    super.onViewCreated(view, state);
    final View content = getView();
content.findViewById(R.id.fragment_imageView).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            translateWithScale(v);
        }
    });
}

private final void translateWithScale(View target) {
    if (target == null) {
        return;
    }
    // cancel animator
    if (target.getTag() instanceof Animator) {
        ((Animator) target.getTag()).cancel();
    }

    // params
    final int duration = 300;
    final float scale = 0.355f;

    // scale
    ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(target, "scaleX", scale).setDuration(duration);
    ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(target, "scaleY", scale).setDuration(duration);

    //
    final float transY = target.getTop() + target.getHeight() / 2 - scale * target.getHeight() / 2;
    final float transX = target.getLeft() + target.getWidth() / 2 - scale * target.getWidth() / 2;
    ObjectAnimator translateX = ObjectAnimator.ofFloat(target, "translationX", -transX).setDuration(duration);
    ObjectAnimator translateY = ObjectAnimator.ofFloat(target, "translationY", -transY).setDuration(duration);

    // sequential animator
    AnimatorSet set = new AnimatorSet();
    set.setInterpolator(new AccelerateDecelerateInterpolator());
    set.playTogether(scaleDownX, scaleDownY, translateX, translateY);
    set.start();

    // set the AnimatorSet as a tag, to cancel for animate next time.
    target.setTag(set);
}