我是新来的android编程,我最近搜索了一个对象的简单的淡出动画。我把它放在我的代码中,测试它,它是波涛汹涌。我检查了日志,看到这个:I /编舞师(12759):跳过40帧!该应用程序可能在其主线程上做了太多工作
11-05 19:19:21.319:我/编舞(12759):跳过了40帧!应用程序可能在其主线程上做了太多工作。
这是我在MainActivity类
private MainActivity actMain;
FadeIn fadeClass = new FadeIn();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.view_switcher);
actMain = this;
new Thread() {
@Override
public void run() {
// If there are stories, add them to the table
try {
// code runs in a thread
runOnUiThread(new Runnable() {
@Override
public void run() {
fadeClass.animation(actMain);
}
});
} catch (final Exception ex) {
Log.i("---", "Exception in thread");
}
}
}.start();
}
而且从淡入类代码:
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void animation(Activity context) {
final View view = context.findViewById(R.id.fadeOutLayer);
Animation fadeOut = new AlphaAnimation(1f, 0f);
fadeOut.setDuration(3000);
fadeOut.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
view.setVisibility(View.GONE);
}
});
view.startAnimation(fadeOut);
}
谢谢!
对不起,愚蠢的问题,但我该怎么办运行onActivityCreated?这似乎并没有工作: \t public void onActivityCreated(){ \t \t fadeClass.animation(actMain); \t} – 2014-11-05 17:47:50
在代码中有一些我不明白的地方。为什么你的FadeIn类有一个onCreate方法?为什么它会夸大内容视图?你想在包含在你的R.layout.activity_main中的视图中应用动画吗?您可以将您的动画方法放入MainActivity中,并将视图(或视图的引用)作为参数传递。在onCreate中保存对动画视图的引用(findviewbyId),并在方法animation中使用此引用。关于onActivityCreated,我错了(它在Fragments中使用),抱歉。在onResume或onCreate中使用它。 – Nescafemix 2014-11-06 10:41:11