0

我正在尝试使用 SurfaceView 制作游戏并在画布上绘图,但画布并没有填满我手机上的整个屏幕!我在摩托罗拉 Atrix 2 上运行该应用程序,屏幕分辨率为 256 dpi,屏幕分辨率为 4.3。下面我发布了我的 main.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">

    <com.figitaki.pong.MainGamePanel
        android:id="@+id/MainGamePanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/Welcome"
            android:text="@string/welcome_text"
            android:visibility="visible"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="center_horizontal"
            android:textColor="#88FFFFFF"
            android:textSize="24dp" />
    </RelativeLayout>

</FrameLayout>

这是 MainGamePanel.java 类:

package com.figitaki.pong;

import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.util.Log;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;

public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback {

    private MainThread thread;
    private Ball ball;
    private Bitmap background;

    private static final String TAG = MainGamePanel.class.getSimpleName();

    public MainGamePanel(Context context, AttributeSet attribs) {
        super(context, attribs);

        getHolder().addCallback(this);

        ball = new Ball(BitmapFactory.decodeResource(getResources(), R.drawable.droid_1), 50, 50);
        background = BitmapFactory.decodeResource(getResources(), R.drawable.droid_1);

        thread = new MainThread(getHolder(), this);

        setFocusable(true);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        background = Bitmap.createScaledBitmap(background, width, height, true);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        while(retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            ball.handleActionDown((int)event.getX(), (int)event.getY());
            if(event.getY() > getHeight() - 50) {
                thread.setRunning(false);
                ((Activity)getContext()).finish();
            } else {
                Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
            }
        }
        if(event.getAction() == MotionEvent.ACTION_MOVE) {
            if(ball.isTouched()) {
                ball.setX((int)event.getX());
                ball.setY((int)event.getY());
            }
        }
        if(event.getAction() == MotionEvent.ACTION_UP) {
            if(ball.isTouched()) 
                ball.setTouched(false);
        }

        return true;
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawBitmap(background, 0, 0, null);
        ball.draw(canvas);
    }
}

我需要画布或表面视图或任何东西来填满整个屏幕。提前致谢。

4

1 回答 1

0

Figured it out. I just needed to add:

<uses-sdk android:targetSdkVersion=">=4" />

In my manifest file!

于 2012-08-07T16:01:47.680 回答