2

所以我在编程 android 方面几乎是菜鸟,而且我还在学习它。

我创建了一个小型 2D 宇宙飞船游戏,您必须通过移动手指来躲避迎面而来的星星。这个游戏在我的模拟器上几乎可以完美运行,但是在其他手机(比如我朋友的手机)上玩时,它就搞砸了。

首先,宇宙飞船在他的手机上看起来太大了,星星也是如此,(不像我的模拟器)。而且我的书面文字坐标似乎也关闭了。

我相信这个问题与像素有关,并用每部手机调整它们,但是如何?

感谢你们对我的帮助,

下面是我的长代码,如果你想通过它,虽然它是不必要的......

如果可以的话,请给我一个例子,说明你是如何做到的,所以请告诉我你在绘制一个简单的矩形时在坐标中放了什么......非常感谢

package com.zunairgames.zunair;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;

public class GFXSurface extends Activity implements OnTouchListener {

MyBringBackSurface ourSurfaceView;
float x, y;
boolean testingFinger=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    ourSurfaceView = new MyBringBackSurface(this);
    ourSurfaceView.setOnTouchListener(this);
    x=0;
    y=0;

     requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(ourSurfaceView);//ourSurfaceView
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    ourSurfaceView.pause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    ourSurfaceView.resume();
}

public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub

    x= event.getX();
    y= event.getY();

     if (event.getAction()==MotionEvent.ACTION_DOWN) {
         testingFinger=true;
         return true;
     }
     if(event.getAction()==MotionEvent.ACTION_UP){
         testingFinger=false;
         return false;
     }



    return false;
}

public class MyBringBackSurface extends SurfaceView implements Runnable{
//vertical
SurfaceHolder ourHolder;
Canvas canvas = (Canvas) ourHolder;
Thread ourThread = null;
boolean isRunning=false;
int screenHeight;
int screenWidth;

Random random = new Random ();
boolean loadStuff = false;

int posX=0;
int posY=0;
int posWidth=100;
int posHeight=100;


int numStars=4;
int starX[]=new int[numStars];
int starY[]=new int[numStars];
int starSpeed[]=new int[numStars];


int score=0;
int backgroundY=0;


Bitmap spaceship;
Bitmap background;
Bitmap starPic;


public MyBringBackSurface(Context context) {
    // TODO Auto-generated constructor stub
    super(context);
    ourHolder= getHolder();


}

public void pause(){
    isRunning=false;
    while(true){
        try {
            ourThread.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;
    }
    ourThread=null;
}
public void resume(){
    isRunning=true;
    ourThread=new Thread(this);
    ourThread.start();
}



public int round(double d){
    double dAbs = Math.abs(d);
    int i = (int) dAbs;
    double result = dAbs - (double) i;
    if(result<0.5){
        return d<0 ? -i : i;            
    }else{
        return d<0 ? -(i+1) : i+1;          
    }
}

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();

    int height = bm.getHeight();

    float scaleWidth = ((float) newWidth) / width;

    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation

    Matrix matrix = new Matrix();

    // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);   
    // recreate the new Bitmap

    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

    return resizedBitmap;

    }
public void run() {
    // TODO Auto-generated method stub


    while(isRunning){
        if(!ourHolder.getSurface().isValid())continue;

         canvas = ourHolder.lockCanvas();

         if(loadStuff==false){
                for (int i=0; i <numStars; i++){
                    starY[i]=-random.nextInt(200);
                    starX[i]=random.nextInt(canvas.getWidth()-50);
                    starSpeed[i]=1+random.nextInt(5);
                }
                 spaceship = BitmapFactory.decodeResource(getResources(),R.drawable.spaceship);
                 background  = BitmapFactory.decodeResource(getResources(),R.drawable.background);
                 starPic  = BitmapFactory.decodeResource(getResources(),R.drawable.tile);

                 spaceship = getResizedBitmap(spaceship,100,100);
                 background= getResizedBitmap(background,(canvas.getHeight())*2,(canvas.getWidth()));
                 starPic= getResizedBitmap(starPic,50,50);

                     backgroundY=-(canvas.getHeight());

                     x=canvas.getWidth()/2;
                     y=canvas.getHeight()/2;
                loadStuff=true;
                }


        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setTextSize(40);

        for (int i=0; i <numStars; i++){
            starY[i]+=starSpeed[i];

            if(starY[i]>canvas.getHeight()){
                starY[i]=-random.nextInt(200);
                starX[i]=random.nextInt(canvas.getWidth()-50);
                starSpeed[i]=starSpeed[i]+random.nextInt(2);
            }

            if(x+posWidth>starX[i]&&x<starX[i]+50 && y+posHeight>starY[i]&&y<starY[i]+50){
                starY[i]=-random.nextInt(200);
                starX[i]=random.nextInt(canvas.getWidth()-50);
                starSpeed[i]=1+random.nextInt(10);
                score++;
            }
        }

        backgroundY++;
        if(backgroundY>-10){
            backgroundY=-canvas.getHeight();
    }
        canvas.drawRGB( 3, 120, 12);
        canvas.drawBitmap(background, 0,backgroundY, null);

        for (int i=0; i <numStars; i++){
              canvas.drawBitmap(starPic,starX[i] , starY[i],null);

        }
        canvas.drawText("SCORE : "+ score  + "DENSITY :"+ canvas.getDensity(),0, 100, paint);





        canvas.drawBitmap(spaceship, x,y, null);


        ourHolder.unlockCanvasAndPost(canvas);

    //72
    }
4

2 回答 2

0

当以编程方式设置宽度和像素位置时,您需要确保考虑到因设备而异的屏幕密度。

因此,您认为您在“密度独立像素”(DIP 或 DP)中进行计算,然后使用如下公式将它们转换为实际像素,然后再将其设置在组件上。

public int dpToPx(int dp) {
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));       
    return px;
}

进一步阅读http://developer.android.com/guide/practices/screens_support.html#dips-pels

于 2013-07-31T22:33:33.313 回答
0

您可以覆盖此方法,当您开始活动并正确缩放时,设备的宽度和高度将转到它

 @Override
public void onSizeChanged (int w, int h, int oldw, int oldh)
{
    super.onSizeChanged(w, h, oldw, oldh);
    screenW = w;
    screenH = h;




}
于 2013-07-31T22:37:05.823 回答