============問題描述============java
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/darker_gray" > <com.ada.MySurfaceView android:id="@+id/surfaceView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout >
public class CartoonActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub setContentView(R.layout.cartoon); //在這裏進行相關運算,最後獲得一值,想把這個值傳到SurfaceView中, //而且SurfaceView構造時要用到這個值,怎麼 實現啊????? } }
public class MySurfaceView extends SurfaceView implements Callback, Runnable { public MySurfaceView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public void surfaceCreated(SurfaceHolder holder) { } @Override public boolean onKeyDown(int key, KeyEvent event) { return super.onKeyDown(key, event); } @Override public boolean onTouchEvent(MotionEvent event) { return true; } @Override public void run() { // TODO Auto-generated method stub } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } }
============解決方案1============android
不用在XML裏面放入了。 你用第一個構造函數就能夠了。 能夠用一個構造函數 public MySurfaceView(Context context){ }