2012-08-07 237 views
0

我正在嘗試使用SurfaceView製作遊戲並將其繪製到畫布上,但畫布不能填充手機上的整個屏幕!我正在使用256 dpi和4.3英寸屏幕的Motorola Atrix 2上運行該應用程序。下面我貼我的main.xml佈局文件:Android Canvas不填充屏幕

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

我需要的帆布或surfaceview或什麼都它,填補了整個屏幕。提前致謝。

回答

0

想通了。我只需要補充:

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

在我的清單文件中!