Android api模拟器与设备

时间:2012-03-28 13:14:39

标签: android android-emulator android-canvas

我正在开发一款游戏 我的代码在4.0模拟器中工作正常 但不是我的三星Galaxy W 2.3.6 我没有使用不适合设备版本的代码 但是我的设备怎么也没动 但是在logcat或sth中甚至没有错误。

MainActivity:

public class ControlsActivity extends Activity implements OnTouchListener{

    Button up;
    Button down;
    Button left;
    Button right;

    String view;

    static boolean touch=false;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);

        up = (Button) findViewById(R.id.up);
        up.setOnTouchListener(this);

        down = (Button) findViewById(R.id.down);
        down.setOnTouchListener(this);

        left = (Button) findViewById(R.id.left);
        left.setOnTouchListener(this);

       right = (Button) findViewById(R.id.right);
        right.setOnTouchListener(this);



    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        /*
        view = v.toString();
        if (ControlsActivity.view == "up" && ControlsActivity.touch == true)
        {
            ph--;
        }
        if (ControlsActivity.view == "down" && ControlsActivity.touch == true)
        {
            ph++;
        }
        if (ControlsActivity.view == "left" && ControlsActivity.touch == true)
        {
            pw--;
        }
        if (ControlsActivity.view == "right" && ControlsActivity.touch == true)
        {
            pw++;
        }
        */
        if(event.toString().contains("action=ACTION_UP"))
        {
            touch = false; 
        }

        if(event.toString().contains("action=ACTION_DOWN"))
        {

            touch = true;
        }


        return touch;

    }



}

画布:

public class draw extends View {
    //Canvas ca;
    View v;
    Paint paint;

    int width;
    int height;

    static final int MAX_GAME_SPEED=25;
    static int fps; 



    static int speed=3;
    static int pw=0;
    static int ph=0;





    public draw(Context context, AttributeSet attr){
           super(context, attr);
           Thread myThread = new Thread(new UpdateThread());
           myThread.start();
    }


   /* public draw(Context context) {
            super(context);
            Thread myThread = new Thread(new UpdateThread());
            myThread.start();
        }  */

            @Override
        protected void onDraw(Canvas c){
        super.onDraw(c);
        paint = new Paint(); //Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);


        //get screen size
        WindowManager wm = (WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();


        width = display.getWidth();  // deprecated
        height = display.getHeight();  // deprecated







            // make the entire canvas white
            paint.setColor(Color.WHITE);
            c.drawPaint(paint);


            //ca = c;
            paint.setColor(Color.BLACK);
            c.drawRect(Math.round(width/3),Math.round(height/3),Math.round((width/3)*2),Math.round((height/3)*2), paint); //position width, position height,width,height




            paint.setColor(Color.GREEN);
            c.drawRect(pw,ph, pw+50,ph+50, paint);

            //pw++;
            //ph++;

            /*if(pw >= width)
            {
                pw = 0;
            }
            if(ph >= height)
            {
                ph = 0;
            }
            if(pw <= 0)
            {
                pw = width;
            }
            if(ph <= 0)
            {
                ph = height;
            }*/

            if(ControlsActivity.touch == true)
            {
                pw=pw+speed;
                ph=ph+speed;
            }
            else
            {

            }


    }

            public Handler updateHandler = new Handler(){
                /** Gets called on every message that is received */
                // @Override
                public void handleMessage(Message msg) {

                  invalidate();
                    super.handleMessage(msg);
                }
            };



            public class UpdateThread implements Runnable {

                @Override
                public void run() {
                    while(true){ //Game Loop

                        long startTime = System.currentTimeMillis();
                        draw.this.updateHandler.sendEmptyMessage(0); //veranlassen, dass paint() erneut aufgerufen werden soll 
                        //for (int i=0; i<999999; i++); //Bremse

                        Thread.yield();                 
                        long executionTime = System.currentTimeMillis()-startTime;

                        if (executionTime<MAX_GAME_SPEED){
                            try {
                                Thread.sleep(MAX_GAME_SPEED-(int)executionTime);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            fps=1000/MAX_GAME_SPEED;
                        } else fps=(int) (1000/executionTime);

                    }

                }

            }

    }

1 个答案:

答案 0 :(得分:0)

你检查了

<uses-sdk android:minSdkVersion="10" />
你的Manifest-File中的

http://developer.android.com/guide/appendix/api-levels.html