Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android api仿真器与设备_Android_Android Emulator_Android Canvas - Fatal编程技术网

Android api仿真器与设备

Android api仿真器与设备,android,android-emulator,android-canvas,Android,Android Emulator,Android Canvas,我正在开发一个游戏 我的代码在4.0模拟器中运行良好 但我的2.3.6版三星galaxy W上没有 我没有使用不适合设备版本的代码 但在我的设备上,任何东西都不会移动 但logcat或其他文件中甚至没有错误 主要活动: public class ControlsActivity extends Activity implements OnTouchListener{ Button up; Button down; Button left; Button righ

我正在开发一个游戏 我的代码在4.0模拟器中运行良好 但我的2.3.6版三星galaxy W上没有 我没有使用不适合设备版本的代码 但在我的设备上,任何东西都不会移动 但logcat或其他文件中甚至没有错误

主要活动:

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

                    }

                }

            }

    }
公共类绘图扩展视图{
//帆布ca;
观点五;
油漆;
整数宽度;
内部高度;
静态最终整数最大游戏速度=25;
静态整数fps;
静态积分速度=3;
静态int pw=0;
静态int-ph=0;
公共绘图(上下文、属性集属性){
超级(上下文,attr);
Thread myThread=新线程(new UpdateThread());
myThread.start();
}
/*公共绘图(上下文){
超级(上下文);
Thread myThread=新线程(new UpdateThread());
myThread.start();
}  */
@凌驾
受保护的void onDraw(画布c){
super.onDraw(c);
paint=new paint();//paint=new paint();
绘制.设置样式(绘制.样式.填充);
//获取屏幕大小
WindowManager wm=(WindowManager)this.getContext().getSystemService(Context.WINDOW\u服务);
Display Display=wm.getDefaultDisplay();
width=display.getWidth();//已弃用
height=display.getHeight();//已弃用
//把整个画布变成白色
油漆。设置颜色(颜色。白色);
c、 油漆;
//ca=c;
油漆。设置颜色(颜色。黑色);
c、 drawRect(数学圆(宽度/3)、数学圆(高度/3)、数学圆((宽度/3)*2)、数学圆((高度/3)*2)、绘制);//位置宽度、位置高度、宽度、高度
油漆。设置颜色(颜色。绿色);
c、 drawRect(pw、ph、pw+50、ph+50、油漆);
//pw++;
//ph++;
/*如果(pw>=宽度)
{
pw=0;
}
如果(ph>=高度)
{
ph=0;
}
如果(pw你检查了

<uses-sdk android:minSdkVersion="10" />

在你的清单文件里


我已经看到模拟器返回ACTION\u UP,而设备ACTION=0尝试通过以下方式进行检查:if(event.getAction()==MotionEvent.ACTION\u DOWN){}这是我在上一条评论中写的。