我知道在主线程上做太多工作意味着什么。但我无法确定代码中出现这种情况。
我在屏幕变黑之前得到了结果
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f2b3b03d840, error=EGL_SUCCESS
I/Choreographer: Skipped 89 frames! The application may be doing too much work on its main thread.
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f2b3b03d400, error=EGL_SUCCESS
D/OpenGLRenderer: endAllStagingAnimators on 0x7f2b3b1e9800 (RippleDrawable) with handle 0x7f2b43ec6200
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f2b3b03d100, error=EGL_SUCCESS
我在我的布局中使用ImageViews,但必须通过代码设置图像,因为当我通过xml布局设置它们时,还会完成更多工作,并且会跳过更多帧以及其他错误。
我的代码:
public class GameActivity extends AppCompatActivity {
private List<MatchImageCard> gameCards;
private List<Drawable> gameOverCards;
private List<ImageView> staticImages;
private Game mMatchGame;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_game);
mMatchGame = new Game(12); //create game using class
assignStaticImages(); //get access to view normally set through xml layout
setStaticImages(); //set these views accessed
assignCards(); //access location of card view store in gameCards List
}
private void assignStaticImages(){
ImageView redStone = (ImageView) findViewById(R.id.red_img);
ImageView blueStone = (ImageView) findViewById(R.id.blue_img);
ImageView yellowStone = (ImageView) findViewById(R.id.yellow_img);
ImageView skull = (ImageView) findViewById(R.id.skull_img);
ImageView backgroundImage = (ImageView) findViewById(R.id.parchment_background);
ImageView backgroundScoreImage = (ImageView) findViewById(R.id.score_parchment);
ImageView spcl1 = (ImageView) findViewById(R.id.special_1);
ImageView spcl2 = (ImageView) findViewById(R.id.special_2);
ImageView oceanBackground = (ImageView) findViewById(R.id.ocean_image);
staticImages = new ArrayList<>();
staticImages.add(redStone);
staticImages.add(blueStone);
staticImages.add(yellowStone);
staticImages.add(skull);
staticImages.add(backgroundImage);
staticImages.add(backgroundScoreImage);
staticImages.add(spcl1);
staticImages.add(spcl2);
staticImages.add(oceanBackground);
}
private void setStaticImages(){
Drawable img1 = ResourcesCompat.getDrawable(getResources(), R.drawable.mouth, null);
Drawable img2 = ResourcesCompat.getDrawable(getResources(), R.drawable.nose, null);
Drawable img3 = ResourcesCompat.getDrawable(getResources(), R.drawable.eye, null);
Drawable img4 = ResourcesCompat.getDrawable(getResources(), R.drawable.display_skull, null);
Drawable img5 = ResourcesCompat.getDrawable(getResources(), R.drawable.parchment, null);
Drawable img6 = ResourcesCompat.getDrawable(getResources(), R.drawable.parchment, null);
Drawable img7 = ResourcesCompat.getDrawable(getResources(), R.drawable.penguin, null);
Drawable img8 = ResourcesCompat.getDrawable(getResources(), R.drawable.pirate, null);
Drawable img9 = ResourcesCompat.getDrawable(getResources(), R.drawable.island_ocean_1, null);
Drawable img10 = ResourcesCompat.getDrawable(getResources(), R.drawable.pirateship, null);
List<Drawable> images = new ArrayList<>();
images.add(img1);
images.add(img2);
images.add(img3);
images.add(img4);
images.add(img5);
images.add(img6);
images.add(img7);
images.add(img8);
images.add(img9);
for(int x = 0; x < staticImages.size(); x++){
ImageView img = staticImages.get(x);
img.setImageDrawable(images.get(x));
}
gameOverCards = new ArrayList<>();
gameOverCards.add(img10);
}
private void assignCards(){
List<ImageView> images = new ArrayList<>();
List<TextView> texts = new ArrayList<>();
ImageView img1 = (ImageView) findViewById(R.id.card1);
ImageView img2 = (ImageView) findViewById(R.id.card2);
ImageView img3 = (ImageView) findViewById(R.id.card3);
ImageView img4 = (ImageView) findViewById(R.id.card4);
ImageView img5 = (ImageView) findViewById(R.id.card5);
ImageView img6 = (ImageView) findViewById(R.id.card6);
ImageView img7 = (ImageView) findViewById(R.id.card7);
ImageView img8 = (ImageView) findViewById(R.id.card8);
ImageView img9 = (ImageView) findViewById(R.id.card9);
ImageView img10 = (ImageView) findViewById(R.id.card10);
ImageView img11 = (ImageView) findViewById(R.id.card11);
ImageView img12 = (ImageView) findViewById(R.id.card12);
images.add(img1);
images.add(img2);
images.add(img3);
images.add(img4);
images.add(img5);
images.add(img6);
images.add(img7);
images.add(img8);
images.add(img9);
images.add(img10);
images.add(img11);
images.add(img12);
TextView txt1 = (TextView) findViewById(R.id.card1_txt);
TextView txt2 = (TextView) findViewById(R.id.card2_txt);
TextView txt3 = (TextView) findViewById(R.id.card3_txt);
TextView txt4 = (TextView) findViewById(R.id.card4_txt);
TextView txt5 = (TextView) findViewById(R.id.card5_txt);
TextView txt6 = (TextView) findViewById(R.id.card6_txt);
TextView txt7 = (TextView) findViewById(R.id.card7_txt);
TextView txt8 = (TextView) findViewById(R.id.card8_txt);
TextView txt9 = (TextView) findViewById(R.id.card9_txt);
TextView txt10 = (TextView) findViewById(R.id.card10_txt);
TextView txt11 = (TextView) findViewById(R.id.card11_txt);
TextView txt12 = (TextView) findViewById(R.id.card12_txt);
texts.add(txt1);
texts.add(txt2);
texts.add(txt3);
texts.add(txt4);
texts.add(txt5);
texts.add(txt6);
texts.add(txt7);
texts.add(txt8);
texts.add(txt9);
texts.add(txt10);
texts.add(txt11);
texts.add(txt12);
gameCards = new ArrayList<>();
for(int x = 0; x < images.size(); x++){
ImageView img = images.get(x);
TextView txt = texts.get(x);
MatchImageCard card = new MatchImageCard(img, txt, x+1);
gameCards.add(card);
}
}
}
正如你所看到我有9个ImageViews,我需要在开始时访问设置布局。我不知道这可能会做多少工作,因为我访问它们然后忘了它们。同样,我有12个TextViews和12个ImageViews,我需要不断处理。为了节省时间,我创建了一个类MatchImageCard,其中每个对象包含1个textView和1个ImageView,它们存在于xml Layout中。只是听到自己的谈话让我意识到我做了很多工作,但我不知道如何减少主线程上的工作量。如果我必须运行新的线程,WHERE。在9个ImageViews的开头,是我分配它们还是设置它们的地方?我可以提供任何建议。
答案 0 :(得分:2)
你在主线程中做了太多工作。你应该使用 asynctask 并节省你应该使用 Picasso 或 Glide 的时间。
专门针对循环内的代码。例如:
for(int x = 0; x < staticImages.size(); x++){
ImageView img = staticImages.get(x);
Picasso.with(context).load(images.get(x)).into(img);
}
答案 1 :(得分:2)
由于错误消息表明您在主线程(或UI线程)中做了太多工作。使用AsyncTask将代码移到UI线程之外。其他可能的解决方案......你正在使用模拟器,我经常遇到这个问题,只是检查你是否有与真实设备相同的问题。