Android 异步任务需要很长时间才能完成
我要求这些类出现在onCreate()方法中,它们在类的底部都有布尔值,一旦完成,它们将设置为true。只有到那时,我的SurfaceView才能画出它需要的东西。为什么这三门课要花这么长时间才能完成 敌人、云和子弹都是我在调用它们的类之前加载的位图Android 异步任务需要很长时间才能完成,android,bitmap,android-asynctask,Android,Bitmap,Android Asynctask,我要求这些类出现在onCreate()方法中,它们在类的底部都有布尔值,一旦完成,它们将设置为true。只有到那时,我的SurfaceView才能画出它需要的东西。为什么这三门课要花这么长时间才能完成 敌人、云和子弹都是我在调用它们的类之前加载的位图 public class handleBullets extends AsyncTask<String, Integer, String> { @Override protected String
public class handleBullets extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... arg0) {
Random r = new Random();
Log.d(null, "Spawn E-Bulles");
for(int i = 0; i < enemies.size(); i++)
{
Enemy tempE = enemies.get(i);
if(System.currentTimeMillis() - tempE.lastBulletSpawn >= 800 && tempE.x <= SCREEN_WIDTH)
{
EnemyBullet eb = new EnemyBullet(tempE.x, tempE.y, enemyBullet.getWidth(), enemyBullet.getHeight(), enemy.getWidth(), enemy.getHeight());
enemyBullets.add(eb);
tempE.lastBulletSpawn = System.currentTimeMillis();
}
}
Log.d(null, "Spawn Bullets");
if(System.currentTimeMillis() - lastPBSpawn >= 400)
{
Bullet b = new Bullet(x, y, player.getWidth(), player.getHeight());
bullets.add(b);
lastPBSpawn = System.currentTimeMillis();
}
Log.d(null, "Remove Bullets");
for(int i = bullets.size() - 1; i > -1; i--)
{
Bullet tempB = bullets.get(i);
if(tempB.x >= SCREEN_WIDTH)
bullets.remove(i);
}
Log.d(null, "removeEnemy Bullets");
for(int i = enemyBullets.size() - 1; i > -1; i--)
{
EnemyBullet tempEB = enemyBullets.get(i);
if(tempEB.x <= 0 - enemyBullet.getWidth())
enemyBullets.remove(i);
}
Log.d(null, "Explosion Renders");
for(int i = explosions.size() - 1; i > -1; i--)
{
Explosion tempEx = explosions.get(i);
tempEx.update();
if(tempEx.duration <= 0)
explosions.remove(i);
}
initial_bullets = true;
return null;
}
}
public class handleEnemy extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... arg0) {
Log.d(null, "Spawning Enemies...");
if(System.currentTimeMillis() - lastESpawn >= 750 && enemy != null)
{
Enemy x = new Enemy(SCREEN_WIDTH, SCREEN_HEIGHT, enemy.getWidth(), enemy.getHeight());
enemies.add(x);
lastESpawn = System.currentTimeMillis();
}
Log.d(null, "Enemies Spawned");
//
Log.d(null, "Removing Enemies...");
for(int i = enemies.size() - 1; i > -1; i--)
{
Enemy tempE = enemies.get(i);
if(tempE.x <= 0 - enemy.getWidth())
enemies.remove(tempE);
}
Log.d(null, "Enemies Removed");
//
initial_enemies = true;
return null;
}
}
public class handleClouds extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... arg0) {
Log.d(null, "Generating Clouds...");
Random r = new Random();
if(r.nextInt(200) == 0 && clouds.size() <= 6 && cloudBM1 != null && cloudBM2 != null)
{
Cloud c = new Cloud(SCREEN_WIDTH, SCREEN_HEIGHT, cloudBM1.getWidth(), cloudBM2.getHeight());
clouds.add(c);
}
Log.d(null, "Clouds Generated");
//
Log.d(null, "Removing Clouds...");
for(int i = clouds.size() - 1; i > -1; i--)
{
Cloud tempC = clouds.get(i);
if(tempC.x <= 0 - cloudBM1.getWidth() && tempC.dir == 0)
clouds.remove(i);
if(tempC.x >= SCREEN_WIDTH && tempC.dir == 1)
clouds.remove(i);
}
Log.d(null, "Clouds Removed");
//
initial_clouds = true;
return null;
}
}
公共类把手扩展任务
{
@凌驾
受保护的字符串doInBackground(字符串…arg0){
随机r=新随机();
Log.d(null,“繁殖E-Bulles”);
for(int i=0;i=800&&tempE.x=400)
{
Bullet b=新的Bullet(x,y,player.getWidth(),player.getHeight());
项目符号.添加(b);
lastPBSpawn=System.currentTimeMillis();
}
Log.d(空,“删除项目符号”);
对于(int i=bullets.size()-1;i>-1;i--)
{
Bullet tempB=bullets.get(i);
如果(tempB.x>=屏幕宽度)
删除(i);
}
Log.d(空,“removeEnemy子弹”);
对于(int i=enemyBullets.size()-1;i>-1;i--)
{
EnemyBullet tempEB=enemyBullets.get(i);
if(tempEB.x-1;i--)
{
爆炸tempEx=爆炸。获取(i);
update();
if(tempEx.duration=750&&敌方!=null)
{
敌方x=新敌方(屏幕宽度、屏幕高度、敌方.getWidth()、敌方.getHeight());
添加(x);
lastESpawn=System.currentTimeMillis();
}
Log.d(null,“生成的敌人”);
//
Log.d(空,“移除敌人…”);
对于(int i=buzzers.size()-1;i>-1;i--)
{
敌人时间=敌人。获取(i);
如果(tempE.x可能是您的任务是串行执行的。请尝试使用executeOnExecutor(AsyncTask.THREAD\u POOL\u EXECUTOR)
并行执行您的任务,而不是调用execute()
。检查执行顺序是任务实际需要很长时间才能完成,还是活动(或SurfaceView)只是不知道他们已经完成了?Raghunandan-你是什么意思?你是说我不应该把它放在doInBackground中吗?Ted Hopp-我让SurfaceView检查一个布尔值,好像(初始云和…)这做什么云c=新云(SCREEN\u WIDTH,SCREEN\u HEIGHT,cloudBM1.getWidth(),cloudBM2.getHeight());
?它是否访问UI
元素?所有Xxx=new Xxx()引用另一个用来制造敌人/云/子弹的类。。。