Java Android,AsyncTask,但UiThread仍然滞后
我正在为android创建一个类似宝石的游戏,但是我的问题是游戏非常滞后,我尝试创建一个AsyncTask来减轻UIthread的压力,这很有帮助,但仍然太滞后,无法玩,有没有关于如何更改代码的提示?我还尝试创建ImageView的两个不同的arrayList以减少for循环中的时间:liste_rubies和liste_rubies。基本上,他们必须停止下降,一旦他们接触到另一个红宝石,并堆叠到另一个,这是可行的,但它的方式太落后于一定数量的红宝石在董事会上。谢谢你的回答Java Android,AsyncTask,但UiThread仍然滞后,java,android,android-asynctask,Java,Android,Android Asynctask,我正在为android创建一个类似宝石的游戏,但是我的问题是游戏非常滞后,我尝试创建一个AsyncTask来减轻UIthread的压力,这很有帮助,但仍然太滞后,无法玩,有没有关于如何更改代码的提示?我还尝试创建ImageView的两个不同的arrayList以减少for循环中的时间:liste_rubies和liste_rubies。基本上,他们必须停止下降,一旦他们接触到另一个红宝石,并堆叠到另一个,这是可行的,但它的方式太落后于一定数量的红宝石在董事会上。谢谢你的回答 public boo
public boolean TestColisions(ImageView Ruby){
boolean test = false;
Rect Rect_tempo = new Rect();
Ruby.getHitRect(Rect_tempo);
for(int y=0;y<liste_Rubies.size();y++){
if(liste_Rubies.get(y).getY() != Ruby.getY()){
Rect Rect_tempo2 = new Rect();
liste_Rubies.get(y).getHitRect(Rect_tempo2);
Rect_tempo2.set( Rect_tempo2.left, (int) (Rect_tempo2.top), Rect_tempo2.right, Rect_tempo2.bottom);
if (Rect.intersects(Rect_tempo2, Rect_tempo)) {
test = true;
liste_Rubies.remove(y);
if(Ruby.getY()<H10)
MessageVictoire();
}
}
}
return test;
}
//-----------------------------------------------------------------------------------------------------------------MoveDown V2 --
private class MoveDownV2 extends AsyncTask<String, Void, String> {
ArrayList<ImageView>liste_Rubies, liste_Rubies_falling;
public MoveDownV2(ArrayList<ImageView>liste_Rubies, ArrayList<ImageView>liste_Rubies_falling) {
this.liste_Rubies = liste_Rubies;
this.liste_Rubies_falling = liste_Rubies_falling;
String launch = doInBackground();
}
@Override
protected String doInBackground(String... params) {
MoveDown=new Timer();
MoveDown.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
for(int x = 0; x < liste_Rubies_falling.size(); x++){
float tempo = liste_Rubies_falling.get(x).getY();
if(liste_Rubies_falling.get(x).getY() < H10*7 && !TestColisions(liste_Rubies_falling.get(x))){
liste_Rubies_falling.get(x).setY(tempo+50);}
else{liste_Rubies.add(liste_Rubies_falling.get(x));
liste_Rubies_falling.remove(liste_Rubies_falling.get(x));}
}
LeCounter++;
if(LeCounter%17==0){
RubyPopUp();
}
}
}, 82, 82);
return null;
}
}
public void RubyPopUp() {
SpartanRuby.this.runOnUiThread(new Runnable() {
@Override
public void run() {
ImageView Ruby = new ImageView(context);
Ruby.setX((float) (L10*5));
Ruby.setY(0);
Ruby.setScaleType(ImageView.ScaleType.FIT_XY);
Ruby.setImageResource(R.drawable.rubies_rouge);
RubyTest = Ruby;
liste_Rubies_falling.add(Ruby);
liste_Rubies_total.add(Ruby);
((ViewGroup) layout_spartanruby).addView(Ruby);
SetSize(Ruby);
}
});
}
public boolean testcolion(ImageView Ruby){
布尔检验=假;
Rect Rect_tempo=new Rect();
Ruby.getHitRect(Rect_tempo);
对于(int y=0;y任何可以减少滞后的小提示都会受到赞赏,即使只是一点点!任何可以减少滞后的小提示都会受到赞赏,即使只是一点点!