android - 如何将surfaceview添加到片段中?

我有一个表面视图和一个片段,我需要在该片段中显示我的表面视图。下面是我的表面视图和片段类。

我正在采取的方法是正确的吗?或者您还有其他建议吗?

片段类:

public class fragment1 extends Fragment{
public static int width,height;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup   container,Bundle savedInstanceState){

            return new GameView(getActivity());
}

片段xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/index"
android:orientation="vertical" >


</LinearLayout>

游戏视图(Surface View)

public class GameView extends SurfaceView {

private Bitmap bmp;

private SurfaceHolder holder;



public GameView(Context context) {

      super(context);

      holder = getHolder();

      holder.addCallback(new SurfaceHolder.Callback() {

             @Override

             public void surfaceDestroyed(SurfaceHolder holder) {

             }


             @Override

             public void surfaceCreated(SurfaceHolder holder) {

                    Canvas c = holder.lockCanvas(null);

                    onDraw(c);

                    holder.unlockCanvasAndPost(c);

             }



             @Override

             public void surfaceChanged(SurfaceHolder holder, int format,

                           int width, int height) {

             }

      });

      bmp = BitmapFactory.decodeResource(getResources(),                    R.drawable.rec01);

}


@Override

protected void onDraw(Canvas canvas) {

      canvas.drawColor(Color.BLACK);

      canvas.drawBitmap(bmp, 10, 10, null);

}

}

最佳答案:

0 个答案:

没有答案
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复