0
回答
Android项目的进程条进程无法触发
华为云数据库免费试用   

本人在弄一个项目,其中有一个片段是想让图片的Y坐标等于某个值是触发进程条的进程减少,但运行之后进程条毫无动静。不知哪位高人能帮我看看是怎么回事,怎么改正。谢谢!

我的代码如下:

 
package com.gsf;

import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.widget.ProgressBar;

/**
 * 
 * @author qinbaoqi
 *
 */
public class MySurfaceView extends SurfaceView implements Callback, Runnable {
 public MySurfaceView(Context context, AttributeSet attrs) {
  super(context, attrs);
  this.setKeepScreenOn(true);
  sfh = this.getHolder();
  sfh.addCallback(this);
  paint = new Paint();
  paint.setColor(Color.WHITE);
  paint.setAntiAlias(true);
  setFocusable(true);
  mediaplayer = MediaPlayer.create(context, R.raw.bgmusic);
  mediaplayer.setLooping(true);
  am = (AudioManager) MainActivity.instance
    .getSystemService(Context.AUDIO_SERVICE);
  MainActivity.instance.setVolumeControlStream(AudioManager.STREAM_MUSIC);
  try {
   if (mediaplayer != null) {
    mediaplayer.stop();
   }
   mediaplayer.prepare();
   mediaplayer.start();
  } catch (IllegalStateException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 // 用于控制SurfaceView
 private SurfaceHolder sfh;
 // 声明一个画笔
 private Paint paint;
 // 文本的坐标
 private int mapY = 0;
 // 声明一条线
 private Thread th;
 // 线程消亡的标识位
 private boolean flag;
 // 声明一个画布
 private Canvas canvas;
 // 声明屏幕的宽高
 private int screenW, screenH;
 private int i=0, z1=0;
 public ProgressBar pb = (ProgressBar) findViewById(R.id.pb);
 private Bitmap bg = BitmapFactory.decodeResource(this.getResources(),
   R.drawable.bachground);
 private MediaPlayer mediaplayer;
 private AudioManager am;
 private Bitmap apple00, apple01, apple02, apple03, apple04;
 private Bitmap apple10, apple11, apple12, apple13, apple14, apple15;
 Bitmap Mapo[] = { apple00, apple01, apple02, apple03, apple04 };
 Bitmap Mapz1[] = { apple10, apple11, apple12, apple13, apple14, apple15 };

 /**
  * SurfaceView视图创建,响应此函数
  */
 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  screenW = this.getWidth();
  screenH = this.getHeight();
  flag = true;
  // 实例线程
  th = new Thread(this);
  // 启动线程
  th.start();
 }

 /**
  * 游戏绘图
  */
 public void myDraw() {
  try {
   canvas = sfh.lockCanvas();
   if (canvas != null) {
    // -----------利用填充画布指定的颜色分量,刷屏
    canvas.drawRGB(0, 0, 0);
    RectF rectF = new RectF(0, 0, screenW, screenH);
    canvas.drawBitmap(bg, null, rectF, null);
    canvas.drawBitmap(Mapo[i], screenW / 3, mapY, paint);
    canvas.drawBitmap(Mapz1[i], screenW / 3, mapY, paint);
   }
  } catch (Exception e) {
   // TODO: handle exception
  } finally {
   if (canvas != null)
    sfh.unlockCanvasAndPost(canvas);
  }
 }

 private void logic() {
  if (i < Mapo.length) {
   if (mapY < 300) {
    mapY += 1;
   } else if (mapY == 300) {
    pb.incrementProgressBy(-10);
    i++;
    Mapo[i] = BitmapFactory.decodeResource(this.getResources(),
      R.drawable.apple00 + i);
    mapY = 0;
   }
   if (z1 < Mapz1.length) {
    if (mapY < 300) {
     mapY += 1;
    } else if (mapY == 300) {
     pb.incrementProgressBy(-10);
     z1++;
     Mapo[z1] = BitmapFactory.decodeResource(
       this.getResources(), R.drawable.apple10 + z1);
     mapY = 0;
    }
   }
  }
 }

 @Override
 public void run() {
  while (flag) {
   long start = System.currentTimeMillis();
   myDraw();
   logic();
   long end = System.currentTimeMillis();
   try {
    if (end - start < 50) {
     Thread.sleep(50 - (end - start));
    }
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }

 }

 /**
  * SurfaceView视图状态发生改变,响应此函数
  */
 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width,
   int height) {
 }

 /**
  * SurfaceView视图消亡时,响应此函数
  */
 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
  flag = false;
 }
}

 

log录得的错误提示信息如下:

11-10 07:01:36.766: E/AndroidRuntime(894): FATAL EXCEPTION: Thread-99
11-10 07:01:36.766: E/AndroidRuntime(894): java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
11-10 07:01:36.766: E/AndroidRuntime(894):  at com.gsf.MySurfaceView.run(MySurfaceView.java:180)
11-10 07:01:36.766: E/AndroidRuntime(894):  at java.lang.Thread.run(Thread.java:856)

 


举报
兰松821203
发帖于4年前 0回/81阅
顶部