从其他线程处理主ui线程上的动画请求太多 - android [英] handling too many requests of animation on the main ui thread from other threads - android
问题描述
我正在创建动画太多的活动,并且在开始此活动时,logcat会在此活动的生命周期内一次又一次地向我显示此消息:
I am working on creating activity that has too many animations, and when starting this activity the logcat shows me this message again and again during the life time of this activity:
I/Choreographer﹕ Skipped 36 frames! The application may be doing too much work on its main thread.
所以我在另一个线程中做了很多东西,但仍然在主UI线程上有大量访问权限。而且,动画在一些高分辨率设备上变得非常慢
so I did many stuff in another threads, but still there is heavy access on the main UI thread. as well as, the animation is becoming really slow on some high resolution devices
处理这个问题的可能解决方案是什么?
what can be possible solution for handling this problem ?
更新:添加代码
所以这里是显示视图的代码(6个imageButtons),
so here is the code of showing the views(which are 6 imageButtons),
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);
}
});
}
}
我在单独的线程中触发它:
I trigger it in seperated thread like this:
new Thread(new Runnable() {
@Override
public void run() {
setupAnimationForAllViews(tempListOfViews,
animationId, isAppearing);
}
}).start();
在同一活动中我有Ken Burns View,它对2张图片实施了ken burns效果,代码这个kenBurnsView就在这个链接中: KenBurnsView
In the same activity i have Ken Burns View, which implement ken burns effect on 2 images, The code of this kenBurnsView is in this link: KenBurnsView
所以这是主要活动xml:
so this is the main 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>
这是java的主要活动部分:
This is the main activity java part:
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和按钮背景图像的尺寸和一点点分辨率,以使其顺利工作。玩线程并没有解决我的滞后问题。
I reduced the sizes and a little bit the resolution of images for Kenburns and the buttons backgrounds in order to get it working smoothly. Playing with threads did not solve the issue of lagging for me.
这篇关于从其他线程处理主ui线程上的动画请求太多 - android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!