android - FragmentTransaction with slide in/out animation on >4 performance

标签 android android-fragments android-animation fragmenttransaction objectanimator

我正在尝试使用滑入/滑出动画实现 fragment 事务。 我正在开发至少 14 个 sdk,所以 ObjectAnimator 是我唯一的选择(还有其他方法吗? 据我所知,翻译动画不可用)。

代码很简单:

AnimationView.java - 包装类

public class AnimationView extends LinearLayout {

public AnimationView(Context context, AttributeSet attrs) {
    super(context, attrs);
}


public float getYFraction() {
    return getHeight();
}

public void setYFraction(float yFraction) {
    final int height = this.getHeight();
    setY((height > 0) ? (yFraction * height) : -9999);
}
}

滑入滑出 xml

<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
                android:interpolator="@android:anim/accelerate_decelerate_interpolator"
                android:propertyName="yFraction"
                android:valueType="floatType"
                android:valueFrom="-1"
                android:valueTo="0"
                android:duration="600"/>

<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
                android:interpolator="@android:anim/accelerate_decelerate_interpolator"
                android:propertyName="yFraction"
                android:valueType="floatType"
                android:valueFrom="0"
                android:valueTo="1"
                android:duration="600"/>

动画效果完美,如您所见,我使用了自定义属性 yFraction。但是性能非常糟糕......在某些平板电脑上,我注意到动画期间有一条水平白线(大约 3 像素高),在其他智能手机设备上它只是变慢(不是持续时间变慢而是闪烁)。

如果我设置 valueFrom 和 valueTo 以适应 View 维度,我找到了一种修复它的方法,但它不是通用的,需要从 0 到 1 才能对所有 View 通用

如果有人能帮助我或告诉我是否有其他方法可以在 android 14 及更高版本上实现,我将不胜感激。

谢谢。

最佳答案

试试这个:

if (mLastMenuWeight > mMenuWeight) {
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
}else{transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);}

关于android - FragmentTransaction with slide in/out animation on >4 performance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17986887/

相关文章:

android - 如何修复在 OnCreateView() 中变为 null 的 List<>

android - 屏幕旋转时导致跳帧的 fragment

java - Android 监听器在 Activity 中工作,但不在 Fragment 中工作

android - 水平滚动时展开和折叠动画

Android动画围绕其中心点旋转图像

android - 什么是 map 瓦片?

android - PhysicsWorld.setGravity(重力);

android - 如何旋转图像并最终使用动画更改图像

java - Android - 以编程方式创建 View 进度条

android - notifyDataSetChanged() 不更新 ListView