-1

我在 youtube 上关注“thenewboston”的 toturial,我在第 74 个 toturial,我在“GFXSurface”应用程序中触摸屏幕后立即获得了 FC。我尝试调试,它停在“canvas.drawBitmap(test, (x - (test.getWidth() / 2)),(y - (test.getHeight() / 2)), null);”行

有任何想法吗?这是教程的链接: http ://www.youtube.com/watch?v= PmOtvJqDfqY

这是我的代码:

public class GFXSurface extends Activity implements OnTouchListener {

    MyBringBackSurface ourSurfaceView;
    float x, y, sX, sY, fX, fY;
    Bitmap test, plus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        ourSurfaceView = new MyBringBackSurface(this);
        ourSurfaceView.setOnTouchListener(this);
        Bitmap test = BitmapFactory.decodeResource(getResources(),
                R.drawable.green_ball);
        Bitmap plus = BitmapFactory.decodeResource(getResources(),
                R.drawable.button_plus);
        x=0;
        y=0;
        sX=0;
        sY=0;
        fX=0;
        fY=0;
        setContentView(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();
    }

    @Override
    public boolean onTouch(View arg0, MotionEvent event) {
        // TODO Auto-generated method stub
        x = event.getX();
        y = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            sX = event.getX();
            sY = event.getY();
            break;
        case MotionEvent.ACTION_UP:
            fX = event.getX();
            fY = event.getY();
            break;
        }
        return true;
    }

    public class MyBringBackSurface extends SurfaceView implements Runnable {

        SurfaceHolder ourHolder;
        Thread ourThread = null;
        Boolean isRunning = false;

        public MyBringBackSurface(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            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();
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (isRunning) {
                if (!ourHolder.getSurface().isValid())
                    continue;

                Canvas canvas = ourHolder.lockCanvas();
                canvas.drawRGB(23, 233, 254);

                if (x != 0 && y != 0) {

                    canvas.drawBitmap(test, (x - (test.getWidth() / 2)),
                            (y - (test.getHeight() / 2)), null);
                }
                if (sX != 0 && sY != 0) {

                    canvas.drawBitmap(plus, (sX - (plus.getWidth() / 2)),
                            (sY - (plus.getHeight() / 2)), null);
                }
                if (fX != 0 && fY != 0) {

                    canvas.drawBitmap(plus, (fX - (plus.getWidth() / 2)),
                            (fY - (plus.getHeight() / 2)), null);
                }

                ourHolder.unlockCanvasAndPost(canvas);
            }
        }

    }
}

}
4

1 回答 1

1
Bitmap test = BitmapFactory.decodeResource(getResources(),
                R.drawable.green_ball);

应该:

test = BitmapFactory.decodeResource(getResources(),
                R.drawable.green_ball);

类中的test变量永远不会被设置,当你尝试使用它时它是空的。

(你的plus变量也是如此)

于 2012-12-11T16:18:05.867 回答