在FrameLayout

时间:2019-05-02 23:21:18

标签: java android

我开始制作一个简单的Android游戏(Tetris),但我无法在屏幕的下部进行绘图,而屏幕下部具有我目前在上面绘制基本形状的SurfaceView。当我尝试运行程序时,除非setContentView(在我的主要活动中)仅设置为SurfaceView对象,否则形状不会绘制。当我将内容视图设置为整个主要活动时,它将显示所有初始视图,但不会绘制形状。运行线程以在SurfaceView上绘制形状时,由于Canvas对象为空,它从不执行绘制。我只是想让我的活动在顶部具有几个TextView对象以及可以在上面绘制对象的SurfaceView。感谢您的帮助。

MainActivity onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

在线程中运行方法:

public void run()
{
    SurfaceHolder sh = bv.getHolder();
    while(!Thread.interrupted())
    {
        Canvas c = sh.lockCanvas();
        try
        {
            synchronized(sh)
            {
                bv.draw();
            }
        }
        catch(Exception e)
        {

        }
        finally
        {
           if(c != null)
           {
               sh.unlockCanvasAndPost(c);
           }
        }
        try
        {
            Thread.sleep(1);
        }
        catch(InterruptedException e)
        {
           return;
        }
    }
}

自定义SurfaceView对象:

public class BoardView extends SurfaceView implements SurfaceHolder.Callback {

private float x = 0, y = 0;
private Paint paint = null;
private Timer timer = null;
TetrisThread th;
public BoardView(Context context) {
    super(context);
    paint = new Paint();
    paint.setColor(Color.RED);
    this.setZOrderOnTop(true);
    getHolder().addCallback(this);
    setFocusable(true);
}
public BoardView(Context context, AttributeSet attrs) {
    super(context, attrs);

}
public BoardView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

}
public void draw() {
    Canvas canvas = getHolder().lockCanvas();
    canvas.drawColor(Color.WHITE);
    canvas.save();
    canvas.translate(x, y);
    canvas.drawRect(850, 850, 950, 950, paint);
    canvas.restore();
    getHolder().unlockCanvasAndPost(canvas);
}

public void surfaceCreated(SurfaceHolder holder)
{
    TetrisThread t = new TetrisThread(this);
    t.start();

}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{

}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
    th.interrupt();
}
@Override
public boolean onTouchEvent(MotionEvent e)
{
    return true;
}
}

主要活动XML

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame">
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="9dp"
    android:text="Tetris"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
    android:textColor="#03A9F4"
    android:textSize="26sp"
    android:textStyle="bold"
    android:typeface="normal" />

<TextView
    android:id="@+id/scoreheader"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="350dp"
    android:layout_marginTop="0dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="9dp"
    android:text="Score"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
    android:textColor="#000000"
    android:textSize="16sp"
    android:textStyle= "bold"
    android:typeface="normal" />
<TextView
    android:id="@+id/score"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="354dp"
    android:layout_marginTop="20dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="9dp"
    android:text="XX"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
    android:textColor="#000000"
    android:textSize="25sp"
    android:textStyle= "normal"
    android:typeface="normal" />

<!--<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="300dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="9dp"
    android:text="New Game"
    android:textSize="12sp" /> -->

<com.example.tetris.BoardView
    android:id="@+id/board"
    android:layout_width="match_parent"
    android:layout_height="680dp"
    android:layout_marginStart="0dp"
    android:layout_marginTop="70dp"
    android:background="#000fff" />

</FrameLayout>    

0 个答案:

没有答案