在主 ui 线程上处理来自其他线程的过多动画请求 - android
handling too many requests of animation on the main ui thread from other threads - android
我正在努力创建动画太多的 activity,当开始这个 activity 时,logcat 在这个 [的生命周期内一次又一次地向我显示这条消息=39=]:
I/Choreographer﹕ Skipped 36 frames! The application may be doing too much work on its main thread.
所以我在另一个线程中做了很多事情,但在主 UI 线程上仍然有大量访问。此外,动画在某些高分辨率设备上变得非常慢
有什么可能的解决方案来处理这个问题?
更新:添加代码
这里是显示视图的代码(有 6 个图像按钮),
private void setupAnimationForAllViews(ArrayList<View> listOfViews,
int animationId,
final boolean isAppearing) {
int startDelay = mDelay; // milliseconds
int numberOfViews = listOfViews.size();
for (int i = 0; i < numberOfViews; i++) {
final Animation animator = AnimationUtils.loadAnimation(mContext, animationId);
animator.setStartOffset(startDelay);
startDelay += mOffSet; // every view will start after 100 milliseconds from the other
final View currentView = listOfViews.get(i);
final int indexOfCurrentCheckedItem = i;
mMainUIThreadHandler.post(new Runnable() {
@Override
public void run() {
currentView.startAnimation(animator);
}
});
}
}
我像这样在单独的线程中触发它:
new Thread(new Runnable() {
@Override
public void run() {
setupAnimationForAllViews(tempListOfViews,
animationId, isAppearing);
}
}).start();
在同一个 activity 我有 Ken Burns View,它在 2 个图像上实现了 ken burns 效果,这个 kenBurnsView 的代码在这个 link: KenBurnsView
所以这是主要的 activity xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#111"
android:orientation="vertical"
android:weightSum="7">
<FrameLayout
android:id="@+id/header"
android:layout_weight="6"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<yasinseyhan.com.yukselirgroup.Activities.GeneralActivities.KenBurnsView
android:id="@+id/header_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/uphill2" />
<ImageView
android:id="@+id/header_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/header_white" />
</FrameLayout>
<LinearLayout
android:id="@+id/dsd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:weightSum="3"
android:orientation="vertical"
>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/white"
android:layout_weight="0.5"/>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/btnKur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/btn_kurumsal_selector"/>
<ImageView
android:id="@+id/btnGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/btn_groups"/>
<ImageView
android:id="@+id/btnSektorelFaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/btn_sektorel_selector"/>
</LinearLayout>
<LinearLayout
android:id="@+id/secondLineLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/btnInsanKayna"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/btn_ik_selector"/>
<ImageView
android:id="@+id/btnGale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/btn_galeri_selector"/>
<ImageView
android:id="@+id/btnIlet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/btn_iletisim_selector"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_weight="0.5"
android:background="@android:color/white"/>
</LinearLayout>
</LinearLayout>
这是主要的 activity java 部分:
public class MainActivity extends Activity {
//Buttons in the MainActivity
AnimationHelper animationHelper;
ArrayList<View> listOfButtons;
Handler mUIThreadHandler = new Handler();
private final int hideOnClickAnimation = R.anim.fade_out;
private final int displayAnimation = R.anim.test_anim;
//For Kenburns View with multible images
private KenBurnsView mKenBurnsView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
initAnimation();
init();
// for testing, we may put those in initAnimation()
animationHelper.setDelay(100);
animationHelper.setOffSet(50);
mKenBurnsView.setResourceIds(R.drawable.test16, R.drawable.uphill2);
}
private void initAnimation() {
//Adding OnClickListeners to the Buttons in the MainActivity - End
// The order is important here
listOfButtons = new ArrayList<>();
listOfButtons.add(btnGroupSirketleri);
listOfButtons.add(btnGale);
listOfButtons.add(btnSektorelFaa);
listOfButtons.add(btnKur);
listOfButtons.add(btnInsanKayna);
listOfButtons.add(btnIlet);
// Adding Animation
animationHelper = new AnimationHelper(listOfButtons, this, mUIThreadHandler);
}
private void initUI() {
//initializing the buttons in the mainactivity
btnKur = (ImageView) findViewById(R.id.btnKur);
btnGroup = (ImageView) findViewById(R.id.btnGroup);
btnSektorelFaa = (ImageView) findViewById(R.id.btnSektorelFaa);
btnInsanKayna = (ImageView) findViewById(R.id.btnInsanKayna);
btnGale = (ImageView) findViewById(R.id.btnGale);
btnIlet = (ImageView) findViewById(R.id.btnIlet);
// KenBurns View
mKenBurnsView = (KenBurnsView) findViewById(R.id.header_picture);
}
private void init() {
//Adding OnClickListeners to the Buttons in the MainActivity - Start
btnKur.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
}
});
btnGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
}
});
btnSektorelFaa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
}
});
btnInsanKayna.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
}
});
btnGale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
}
});
btnIlet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
}
});
}
@Override
protected void onResume() {
super.onResume();
animationHelper.displayViews(displayAnimation, new IDoOnEndOfWholeAnimation() {
@Override
public void doIt() {
if (!mKenBurnsView.isAnimating)
mKenBurnsView.startKenBurnsAnimation();
}
});
}
@Override
protected void onPause() {
super.onPause();
if (mKenBurnsView.isAnimating)
mKenBurnsView.stopKenBurnsAnimation();
}
}
我减小了 Kenburns 和按钮背景的图像尺寸和分辨率,以使其顺利运行。玩线程并没有解决我的滞后问题。
我正在努力创建动画太多的 activity,当开始这个 activity 时,logcat 在这个 [的生命周期内一次又一次地向我显示这条消息=39=]:
I/Choreographer﹕ Skipped 36 frames! The application may be doing too much work on its main thread.
所以我在另一个线程中做了很多事情,但在主 UI 线程上仍然有大量访问。此外,动画在某些高分辨率设备上变得非常慢
有什么可能的解决方案来处理这个问题?
更新:添加代码
这里是显示视图的代码(有 6 个图像按钮),
private void setupAnimationForAllViews(ArrayList<View> listOfViews,
int animationId,
final boolean isAppearing) {
int startDelay = mDelay; // milliseconds
int numberOfViews = listOfViews.size();
for (int i = 0; i < numberOfViews; i++) {
final Animation animator = AnimationUtils.loadAnimation(mContext, animationId);
animator.setStartOffset(startDelay);
startDelay += mOffSet; // every view will start after 100 milliseconds from the other
final View currentView = listOfViews.get(i);
final int indexOfCurrentCheckedItem = i;
mMainUIThreadHandler.post(new Runnable() {
@Override
public void run() {
currentView.startAnimation(animator);
}
});
}
}
我像这样在单独的线程中触发它:
new Thread(new Runnable() {
@Override
public void run() {
setupAnimationForAllViews(tempListOfViews,
animationId, isAppearing);
}
}).start();
在同一个 activity 我有 Ken Burns View,它在 2 个图像上实现了 ken burns 效果,这个 kenBurnsView 的代码在这个 link: KenBurnsView
所以这是主要的 activity xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#111"
android:orientation="vertical"
android:weightSum="7">
<FrameLayout
android:id="@+id/header"
android:layout_weight="6"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<yasinseyhan.com.yukselirgroup.Activities.GeneralActivities.KenBurnsView
android:id="@+id/header_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/uphill2" />
<ImageView
android:id="@+id/header_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/header_white" />
</FrameLayout>
<LinearLayout
android:id="@+id/dsd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:weightSum="3"
android:orientation="vertical"
>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/white"
android:layout_weight="0.5"/>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/btnKur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/btn_kurumsal_selector"/>
<ImageView
android:id="@+id/btnGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/btn_groups"/>
<ImageView
android:id="@+id/btnSektorelFaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/btn_sektorel_selector"/>
</LinearLayout>
<LinearLayout
android:id="@+id/secondLineLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/btnInsanKayna"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/btn_ik_selector"/>
<ImageView
android:id="@+id/btnGale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/btn_galeri_selector"/>
<ImageView
android:id="@+id/btnIlet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/btn_iletisim_selector"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_weight="0.5"
android:background="@android:color/white"/>
</LinearLayout>
</LinearLayout>
这是主要的 activity java 部分:
public class MainActivity extends Activity {
//Buttons in the MainActivity
AnimationHelper animationHelper;
ArrayList<View> listOfButtons;
Handler mUIThreadHandler = new Handler();
private final int hideOnClickAnimation = R.anim.fade_out;
private final int displayAnimation = R.anim.test_anim;
//For Kenburns View with multible images
private KenBurnsView mKenBurnsView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
initAnimation();
init();
// for testing, we may put those in initAnimation()
animationHelper.setDelay(100);
animationHelper.setOffSet(50);
mKenBurnsView.setResourceIds(R.drawable.test16, R.drawable.uphill2);
}
private void initAnimation() {
//Adding OnClickListeners to the Buttons in the MainActivity - End
// The order is important here
listOfButtons = new ArrayList<>();
listOfButtons.add(btnGroupSirketleri);
listOfButtons.add(btnGale);
listOfButtons.add(btnSektorelFaa);
listOfButtons.add(btnKur);
listOfButtons.add(btnInsanKayna);
listOfButtons.add(btnIlet);
// Adding Animation
animationHelper = new AnimationHelper(listOfButtons, this, mUIThreadHandler);
}
private void initUI() {
//initializing the buttons in the mainactivity
btnKur = (ImageView) findViewById(R.id.btnKur);
btnGroup = (ImageView) findViewById(R.id.btnGroup);
btnSektorelFaa = (ImageView) findViewById(R.id.btnSektorelFaa);
btnInsanKayna = (ImageView) findViewById(R.id.btnInsanKayna);
btnGale = (ImageView) findViewById(R.id.btnGale);
btnIlet = (ImageView) findViewById(R.id.btnIlet);
// KenBurns View
mKenBurnsView = (KenBurnsView) findViewById(R.id.header_picture);
}
private void init() {
//Adding OnClickListeners to the Buttons in the MainActivity - Start
btnKur.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
}
});
btnGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
}
});
btnSektorelFaa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
}
});
btnInsanKayna.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
}
});
btnGale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
}
});
btnIlet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationHelper.hideViewsWithClickedView(hideOnClickAnimation, v);
}
});
}
@Override
protected void onResume() {
super.onResume();
animationHelper.displayViews(displayAnimation, new IDoOnEndOfWholeAnimation() {
@Override
public void doIt() {
if (!mKenBurnsView.isAnimating)
mKenBurnsView.startKenBurnsAnimation();
}
});
}
@Override
protected void onPause() {
super.onPause();
if (mKenBurnsView.isAnimating)
mKenBurnsView.stopKenBurnsAnimation();
}
}
我减小了 Kenburns 和按钮背景的图像尺寸和分辨率,以使其顺利运行。玩线程并没有解决我的滞后问题。