应用程序挂起

时间:2013-03-22 13:41:15

标签: android performance bitmap load android-asynctask

我有一个游戏应用程序需要很长时间才能加载。我曾经在说明我的比赛时没有任何延迟,但现在却是,我怀疑有什么事情在悬挂。由于我加载图像的方式,它是否可能悬挂?这就是我所拥有的:

在onCreate()中我有:

v = new SView(this);                      // Creates my new SurfaceView
a = new Accelerator();                    // Adds the accelerometer
new loadPhotos().execute("");             // Initiates the AsyncTask posted below
new handleStuff().execute("");            // starts the generation of the game map
startTime = System.currentTimeMillis();   // Used to generate time lasted
setContentView(v);                        // Sets contentView to the SurfaceView


public class loadPhotos extends AsyncTask<String, Integer, String>
{
@Override

protected String doInBackground(String... arg0) {

    return null;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    Log.d(null, "!!!!!!!!!!!!!----------Starting Load-----------!!!!!!!!!1");
    player = BitmapFactory.decodeResource(getResources(), R.drawable.player);
    playerOpaque = BitmapFactory.decodeResource(getResources(), R.drawable.player_opaque);
    livesBM = BitmapFactory.decodeResource(getResources(), R.drawable.lives);
    playerMissile = BitmapFactory.decodeResource(getResources(), R.drawable.player_missle);
    missileButton = BitmapFactory.decodeResource(getResources(), R.drawable.missile_button);
    playerBullet = BitmapFactory.decodeResource(getResources(), R.drawable.bullet);
    enemyBullet = BitmapFactory.decodeResource(getResources(), R.drawable.enemy_bullet);
    enemyMissile = BitmapFactory.decodeResource(getResources(), R.drawable.enemy_missle);
    enemy = BitmapFactory.decodeResource(getResources(), R.drawable.enemy);
    explosionBig = BitmapFactory.decodeResource(getResources(), R.drawable.explosion);
    lastESpawn = System.currentTimeMillis();
    background = BitmapFactory.decodeResource(getResources(), R.drawable.background_clearsky);
    cloudBM1 = BitmapFactory.decodeResource(getResources(), R.drawable.cloud1);
    cloudBM2 = BitmapFactory.decodeResource(getResources(), R.drawable.cloud2);
    Log.d(null, "!!!!!!!!!!!!!----------Ending Load-----------!!!!!!!!!1");
}

}

这是我的LogCat

03-22 10:03:50.546: D/(1220): !!!!!!!!!!!!!----------Starting Load-----------!!!!!!!!!1
03-22 10:03:50.596: D/(1220): !!!!!!!!!!!!!----------Ending Load-----------!!!!!!!!!1
03-22 10:03:50.616: I/Adreno200-EGLSUB(1220): <ConfigWindowMatch:2087>: Format RGBA_8888.
03-22 10:03:50.646: W/SurfaceView(1220): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=true left=false top=false
03-22 10:03:50.656: D/(1220): !!!!!!!!!!!!!----------Starting Load-----------!!!!!!!!!1
03-22 10:03:50.706: D/dalvikvm(1220): GC_CONCURRENT freed 2361K, 14% free 22198K/25543K, paused 11ms+2ms, total 28ms
03-22 10:03:50.726: D/(1220): !!!!!!!!!!!!!----------Ending Load-----------!!!!!!!!!1
03-22 10:03:50.746: I/Adreno200-EGLSUB(1220): <ConfigWindowMatch:2087>: Format RGBA_8888.
03-22 10:04:18.096: D/dalvikvm(1220): GC_FOR_ALLOC freed 4756K, 28% free 20236K/27975K, paused 16ms, total 18ms
03-22 10:04:18.096: I/dalvikvm-heap(1220): Grow heap (frag case) to 32.131MB for 3686416-byte allocation

2 个答案:

答案 0 :(得分:0)

您正试图通过UI访问您无法访问的doInBackground资源。这需要采用onPostExecute()或其他Async方法,但我甚至不确定您是否需要/ AsyncTask。您只需使用ImageView methods设置images并使用Activity

的方法加载它们

答案 1 :(得分:0)

尝试加载图片如下 在imageview中 iv.setImageResource(R.drawable.imagename);

这比通过decoderesource加载要快得多。每个图像加载都有这样的方法。