mir*_*ira 33 android android-animation translate-animation
我有android翻译动画.我有一个随机生成位置的ImageView(next1,next2).我每隔3秒就调用一次虚空.它生成视图的新位置,然后制作动画并将视图移动到目标位置.翻译动画已实现AnimationListener.动画完成后,我将View永久移动到新位置(在OnAnimationEnd中).我的问题是动画位置与设置layoutParams位置不对应.当动画结束时,它会跳转到一个新位置,大约50-100像素.我认为位置应该是相同的,因为我在两种情况下都使用相同的值(next1,next2).请问您能找到找到溶剂的方法吗?
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);
Run Code Online (Sandbox Code Playgroud)
这是XML布局:
<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>
Run Code Online (Sandbox Code Playgroud)
Gil*_*yof 50
我通常喜欢在翻译动画中使用增量,因为它避免了很多混乱.
试试这个,看看它是否适合你:
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);
Run Code Online (Sandbox Code Playgroud)
一定要做amountToMoveRight/amountToMoveDown final
希望这可以帮助 :)
bpr*_*r10 38
您应该使用ViewPropertyAnimator.这会将视图设置为其未来位置的动画,并且您不需要在动画结束后强制视图上的任何布局参数.它很简单.
myView.animate().x(50f).y(100f);
myView.animate().translateX(pixelInScreen)
Run Code Online (Sandbox Code Playgroud)
注意:此像素与视图无关.该像素是屏幕中的像素位置.
你可以这样做
myView.animate().y(0f);//to move to the top of the screen.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
95137 次 |
| 最近记录: |