Ich habe Android-Animation übersetzen. Ich habe eine ImageView mit zufällig generierter Position (next1, next2). Ich rufe alle 3 Sekunden als ungültig auf. Es erzeugt eine neue Position der Ansicht, erstellt dann eine Animation und bewegt die Ansicht an die Zielposition. Beim Übersetzen der Animation wurde der AnimationListener implementiert. Wenn die Animation beendet ist, verschiebe ich View permanent an die neue Position (in OnAnimationEnd). Mein Problem ist, dass die Animationsposition nicht mit der Einstellung von LayoutParams-Positionen übereinstimmt. Wenn die Animation beendet ist, wird an eine neue Position gesprungen, die etwa 50 bis 100 Pixel weit ist. Ich denke, die Positionen sollten gleich sein, da ich in beiden Situationen dieselben Werte (next1, next2) verwende. Kannst du mir den Weg zeigen, um die Lösung zu finden?
FrameLayout.LayoutParams pozice_motyl = (FrameLayout.LayoutParams) pozadi_motyl.getLayoutParams();
TranslateAnimation anim = new TranslateAnimation(Animation.ABSOLUTE,pozice_motyl.leftMargin, Animation.ABSOLUTE, next1, Animation.ABSOLUTE, pozice_motyl.topMargin, Animation.ABSOLUTE, next2);
anim.setDuration(1000);
anim.setFillAfter(true);
anim.setFillEnabled(true);
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(100, 100);
layoutParams.leftMargin = (int)(next1);
layoutParams.topMargin = (int)(next2);
pozadi_motyl.setLayoutParams(layoutParams);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
pozadi_motyl.startAnimation(anim);
Hier ist das XML-Layout:
<FrameLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
xmlns:ads="http://schemas.Android.com/apk/lib/com.google.ads"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
Android:id="@+id/pozadi0"
Android:gravity="center_vertical"
Android:layout_gravity="center_vertical"
Android:background="@drawable/pozadi2"
>
<LinearLayout
Android:id="@+id/pozadi_motyl"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
>
<ImageView Android:id="@+id/obrazek_motyl"
Android:src="@drawable/motyl"
Android:layout_width="100dip"
Android:layout_height="100dip"
/>
</LinearLayout>
<LinearLayout
Android:orientation="vertical"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
>
<RelativeLayout
Android:id="@+id/obrazek_pozadi0"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
>
<ImageView Android:id="@+id/zaba_obrazek0"
Android:src="@drawable/zaba_obrazek"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_alignParentBottom="true"
Android:layout_centerInParent="true"
/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
>
<cz.zaba.Spojnice
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
/>
</LinearLayout>
</FrameLayout>
Normalerweise arbeite ich lieber mit Deltas in der Übersetzungsanimation, da dadurch viel Verwirrung vermieden wird.
Probieren Sie es aus und sehen Sie, ob es für Sie funktioniert:
TranslateAnimation anim = new TranslateAnimation(0, amountToMoveRight, 0, amountToMoveDown);
anim.setDuration(1000);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationRepeat(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation)
{
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)view.getLayoutParams();
params.topMargin += amountToMoveDown;
params.leftMargin += amountToMoveRight;
view.setLayoutParams(params);
}
});
view.startAnimation(anim);
Stellen Sie sicher, dass Sie amountToMoveRight
/amountToMoveDown
final
festlegen.
Hoffe das hilft :)
Verwenden Sie stattdessen ViewPropertyAnimator . Dadurch wird die Ansicht zu ihrer zukünftigen Position animiert und Sie müssen nach dem Ende der Animation keine Layoutparameter in der Ansicht erzwingen. Und es ist ziemlich einfach.
myView.animate().x(50f).y(100f);
myView.animate().translateX(pixelInScreen)
Hinweis: Dieses Pixel ist nicht relativ zur Ansicht. Dieses Pixel ist das Pixel Position auf dem Bildschirm.
du kannst es einfach gerne machen
myView.animate().y(0f);//to move to the top of the screen.
Das hat bei mir perfekt funktioniert: -
// slide the view from its current position to below itself
public void slideUp(final View view, final View llDomestic){
ObjectAnimator animation = ObjectAnimator.ofFloat(view, "translationY",0f);
animation.setDuration(100);
llDomestic.setVisibility(View.GONE);
animation.start();
}
// slide the view from below itself to the current position
public void slideDown(View view,View llDomestic){
llDomestic.setVisibility(View.VISIBLE);
ObjectAnimator animation = ObjectAnimator.ofFloat(view, "translationY", 0f);
animation.setDuration(100);
animation.start();
}
llDomestic: Die Ansicht, die Sie ausblenden möchten . Ansicht: Die Ansicht, die Sie nach unten oder oben verschieben möchten.
Sie können auf diese Weise versuchen -
ObjectAnimator.ofFloat(view, "translationX", 100f).apply {
duration = 2000
start()
}
Anmerkung - Ansicht ist Ihre Ansicht, in der Sie Animation wünschen.