2016-04-22 61 views
0

我想从我的屏幕中心到我的屏幕左上角翻译一个对象。翻译动画坐标发布多个设备

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

有没有办法来翻译和传递值,这将导致动画跨设备相同?

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

回答

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); 
} 
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); 
+0

嘿感谢,如果没有太多的麻烦,你能解释一下你的代码段是干什么的? – ViVekH

+0

添加一些解释 –

+0

不工作,doesnt去顶边(0,0),为什么-location [0]? – ViVekH

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

     } 
    }); 
}