Android,AsyncTask但UiThread仍然滞后

时间:2017-08-26 19:34:52

标签: java android android-asynctask

我正在为Android制作一款类似Jewel的游戏,不过我的问题是游戏非常迟钝,我尝试创建一个AsyncTask来减轻UIthread上的压力,它有所帮助,但是它仍然太迟,无法播放,任何提示我应该如何更改我的代码?我还尝试创建ImageView的两个不同的arrayList来减少for循环中的时间:liste_rubies和liste_rubies_falling。基本上,一旦他们碰到另一颗红宝石并堆叠在一起,它们就必须停止掉落,它可以工作但是它太过于滞后于板上的一定数量的红宝石。谢谢你的回答

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);
            }
        });
    }

0 个答案:

没有答案