1

我有一个游戏应用程序需要很长时间才能加载。我以前在说明我的游戏时没有滞后,但现在是这样,我怀疑有些东西挂了。是否有可能由于我加载图像的方式而挂起?这是我所拥有的:

在 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
4

2 回答 2

0

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

这比通过decoderresource加载要快得多。你的每一个图片加载都有这样的方法。

于 2013-03-22T13:50:15.993 回答
0

您正试图通过您无法访问的方式访问您的UI资源doInBackground。这需要 inonPostExecute()或其他Async方法,但我什至不确定你想要/需要一个AsyncTask。您可以设置images使用ImageView 方法并将它们加载到您的方法中Activity

于 2013-03-22T13:48:33.723 回答