Java 如何将旋转动画添加到单独线程上的背景图像?
我正在构建一个应用程序,我希望在另一个内容下的一个角落中有一个小的旋转图像。我已经创建了一个动画xml文件,目前正在我的主要活动中加载动画 不幸的是,这似乎减慢了应用程序的速度,并且日志输出表明正在跳过帧,因为“应用程序可能在其主线程上做了太多工作”。所以我认为最好的办法是将动画移动到一个新的线程 下面是我当前如何设置图像动画的:Java 如何将旋转动画添加到单独线程上的背景图像?,java,android,multithreading,animation,Java,Android,Multithreading,Animation,我正在构建一个应用程序,我希望在另一个内容下的一个角落中有一个小的旋转图像。我已经创建了一个动画xml文件,目前正在我的主要活动中加载动画 不幸的是,这似乎减慢了应用程序的速度,并且日志输出表明正在跳过帧,因为“应用程序可能在其主线程上做了太多工作”。所以我认为最好的办法是将动画移动到一个新的线程 下面是我当前如何设置图像动画的: ImageView rotatingCog @Override public View onCreateView(LayoutInflater inflater,
ImageView rotatingCog
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rotatingCog = (ImageView)rootView.findViewById(R.id.bgCog);
final Animation animRotate = AnimationUtils.loadAnimation(rootView.getContext(), R.anim.rotate_cog);
rotatingCog.startAnimation(animRotate);
...
}
创建此片段时,如何将此代码移动到新线程并启动该线程?我认为类似的方法可以工作:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Handler mHandler = new Handler();
Thread animationThread = new Thread(new Runnable(){
@Override public void run(){
mHandler.post(animateView);
}
});
animationThread.start();
...
}
Runnable animateView = new Runnable(){
@Override public void run(){
final ImageView rotatingCog = (ImageView) getView().findViewById(R.id.bgCog);
final Animation animRotate = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_cog);
rotatingCog.startAnimation(animRotate);
}
};
}
完美,它第一次运行,真的让动画更加流畅!谢谢你的帮助。我想我发现视图是错误的,更新为调用getView()而不是getActivity(),但很高兴我能帮助其他人记下我只会在onCreateView()中膨胀视图,然后在onViewCreated()中进行所有动画制作,以确保片段在访问UI元素之前确实首先创建了视图