Android动画减少口吃/波动/滞后

时间:2012-10-04 11:51:59

标签: android multithreading caching animation

所以我一直有动画问题,特别是当两个动画一次发生或者当一个活动加载时。我理解这可能是一个资源问题,主线程中发生了很多事情,导致动画断断续续。

我发现了一些有趣的建议:
1.线程(ThreadPoolExecutor)
此处: How do I make my animation smoother Android
2. setDrawingCacheEnabled(true)
此处: How does Android's setDrawingCacheEnabled() work?
3. ViewGroup:animationCache = true
此处http://www.curious-creature.org/2010/12/02/android-graphics-animations-and-tips-tricks/

但是我无法找到任何实例来实现这些东西。有什么想法吗?

2 个答案:

答案 0 :(得分:8)

在减少口吃时遵循按重要性排列的规则,我减少了动画上的口吃数量:

  1. 请勿在{{1​​}},onCreateonStart中发起动画。
  2. 启动有关onResume等用户事件的动画,并停止触摸事件,直到动画完成。
  3. 不要同时启动2个以上的动画

答案 1 :(得分:0)

如果您使用动画,则应遵循android docs;实际上,在某些情况下,您可能需要将片段转换延迟一小段时间。例如,在我的情况下,我需要推迟动画,直到我的视图模型返回一些数据:

在输入的片段onViewCreated()方法中使用public class A extends Fragment { @Override public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { ... postponeEnterTransition(); } }

startPostponedEnterTransition()

一旦数据准备好开始过渡,请致电public class A extends Fragment { @Override public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { postponeEnterTransition(); final ViewGroup parentView = (ViewGroup) view.getParent(); // Wait for the data to load viewModel.getData() .observe(getViewLifecycleOwner(), new Observer<List<String>>() { @Override public void onChanged(List<String> list) { // Set the data on the RecyclerView adapter adapter.setData(list); // Start the transition once all views have been // measured and laid out parentView.getViewTreeObserver() .addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { parentView.getViewTreeObserver() .removeOnPreDrawListener(this); startPostponedEnterTransition(); return true; }); } }); } }

struct MyStruct {
    var myVar = "myVar"
    let myLet = "myLet"
}

func testMutateString() {
    //given
    let myStruct = MyStruct()
    
    //Change var
    //when
    var myStructCopy = myStruct
    myStructCopy.myVar = "myVar changed 1"
    
    //then
    XCTAssert(myStructCopy.myVar == "myVar changed 1")
    
    //Change let
    //when
    withUnsafeMutableBytes(of: &myStructCopy) { bytes in
        let offset = MemoryLayout.offset(of: \MyStruct.myLet)!
        let rawPointerToValue = bytes.baseAddress! + offset
        let pointerToValue = rawPointerToValue.assumingMemoryBound(to: String.self)
        pointerToValue.pointee = "myLet changed"
    }
    
    //then
    XCTAssert(myStructCopy.myLet == "myLet changed")
}