1

我有一个主要活动类,我还有一个 SurfaceView 类,它在我的主要活动开始时启动。

我在主要活动中有一个搜索栏。我想将搜索栏产生的任何数据发送到 SurfaceView 以在每次更改时显示它。

我怎样才能做到这一点?

谢谢。

这是我的 SurfaceView 类:

public class SurfaceViewGauge extends SurfaceView implements SurfaceHolder.Callback{


 private MySurfaceThread thread;
 private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

 public SurfaceViewGauge(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  init();
 }

 public SurfaceViewGauge(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
  init();
 }

 public SurfaceViewGauge(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // TODO Auto-generated constructor stub
  init();
 }

  private void init(){
    getHolder().addCallback(this);
    thread = new MySurfaceThread(getHolder(), this);

    setFocusable(true); // make sure we get key events

    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(3);
    paint.setColor(Color.WHITE);

   }

 public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  // TODO Auto-generated method stub

 }

 public void surfaceCreated(SurfaceHolder holder) {
  // TODO Auto-generated method stub
  thread.setRunning(true);
  thread.start();

 }

 public void surfaceDestroyed(SurfaceHolder holder) {
  // TODO Auto-generated method stub
  boolean retry = true;
  thread.setRunning(false);
  while (retry) {
   try {
    thread.join();
    retry = false;
   }
   catch (InterruptedException e) {
   }
  }
 }

 @Override
 protected void onDraw(Canvas canvas) {
  // TODO Auto-generated method stub

 }


}

这是我的线程类:

    public class MySurfaceThread extends Thread {
 private SurfaceHolder myThreadSurfaceHolder;
 private SurfaceViewGauge myThreadSurfaceView;
 private boolean myThreadRun = false;

 public MySurfaceThread(SurfaceHolder surfaceHolder, SurfaceViewGauge surfaceView) {
  myThreadSurfaceHolder = surfaceHolder;
  myThreadSurfaceView = surfaceView;
 }

 public void setRunning(boolean b) {
  myThreadRun = b;
 }

 @Override
 public void run() {
  // TODO Auto-generated method stub
  while(myThreadRun){
   Canvas c = null;

   try{
    c = myThreadSurfaceHolder.lockCanvas(null);
    synchronized (myThreadSurfaceHolder){
     myThreadSurfaceView.onDraw(c);
    }
    sleep(0);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   finally{
    // do this in a finally so that if an exception is thrown
    // during the above, we don't leave the Surface in an
    // inconsistent state
    if (c != null) {
     myThreadSurfaceHolder.unlockCanvasAndPost(c);
    }
   }
  }
 }

}

4

2 回答 2

1

在您的代码中,添加 Handler..

public Handler mhandler;

public SurfaceViewGauge(Context context, Handler mhandler) 
{
super(context);
this.mhandler = mhandler;
// TODO Auto-generated constructor stub
init();
}

在上面的代码中,使用 Handler 对象来发送代码。

于 2013-04-10T14:52:46.117 回答
1

在您的 MainActivity 中声明一个静态变量,该变量将保存 seekbar 值,并在您的表面视图代码中借助处理程序调用该静态变量,例如

MainClass.seekValue;
于 2013-04-20T20:58:14.963 回答