2012-11-19 44 views
1

我有一個主要活動類,我也有一個SurfaceView類,當我的主要活動啓動時開始。使用搜索欄將主要活動的數據傳遞到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 

} 


} 

,這裏是我的Thread類:

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); 
    } 
    } 
    } 
} 

}

+1

任何人都? :( – user1319668

回答

1

在您的MainActivity中聲明一個靜態變量,該變量將保存seekbar值,並在您的surfaceview代碼中藉助於處理程序調用該靜態變量 (例如,

MainClass.seekValue; 
1

在你的代碼,添加處理程序..

public Handler mhandler; 

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

在上面的代碼中,使用用於發送代碼的Handler對象。

相關問題