2016-04-22 27 views
0

Ich möchte ein Objekt von der Mitte meines Bildschirms in die obere linke Ecke meines Bildschirms zu übersetzen.Translate Animation Koordinaten Probleme mehrere Geräte

Ich möchte die Übersetzung Anim auf allen Geräten gleich sein derzeit die Übersetzung Werte, die ich gebe, sind unterschiedlich auf verschiedenen Geräten [nach der Animation Position des Objekts ist anders].

Gibt es eine Möglichkeit, Werte zu übersetzen und weiterzuleiten, was dazu führt, dass die Animation auf allen Geräten gleich ist?

enter image description here

My-Code

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

Antwort

1

Sie können es links :

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

@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

hey danke, wenn es nicht zu viel von einem stören können Sie erklären, was Ihr Snippet tut? – ViVekH

+0

Erklärung hinzufügen –

+0

funktioniert nicht, geht nicht zur oberen Kante (0,0), warum -location [0]? – ViVekH

1

Erstens: Sie müssen xml erstellen müssen zwei Steuer

Control 1 haben Namen mControlStart haben Lage am Zentrum Layout

Control 2 haben Namen mControlFinish haben Lage am oben und haben Eigenschaften

android:visibility="invisible" 

Dann in OnClick

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

     } 
    }); 
}