1

这是我的完整代码,,,,

任何人都可以帮助我,,,

public class MainActivity extends Activity {

@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(new Panel(this));

    }
}


class Panel extends SurfaceView implements SurfaceHolder.Callback {


         private Bitmap[] mBitmap;
        private ViewThread mThread;
        public Panel(Context context) {
            super(context);
             mBitmap = new Bitmap[4];
                mBitmap[0] = BitmapFactory.decodeResource(getResources(), R.drawable.x);
                mBitmap[1] = BitmapFactory.decodeResource(getResources(), R.drawable.x);
                mBitmap[2] = BitmapFactory.decodeResource(getResources(), R.drawable.x);
                mBitmap[3] = BitmapFactory.decodeResource(getResources(), R.drawable.x);

            getHolder().addCallback(this);
            mThread = new ViewThread(this);
        }

        public void doDraw(Canvas canvas) {
            canvas.drawColor(Color.BLACK);

            canvas.drawBitmap(mBitmap[0], 0, 0, null);
            canvas.drawBitmap(mBitmap[1], 45, 0, null);
            canvas.drawBitmap(mBitmap[2], 90, 0, null);
            canvas.drawBitmap(mBitmap[3], 135, 0, null);


        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            if (!mThread.isAlive()) {
                mThread = new ViewThread(this);
                mThread.setRunning(true);
                mThread.start();
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            if (mThread.isAlive()) {
                mThread.setRunning(false);
            }
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {

            mX = (int) event.getX() - mBitmap[0].getWidth() / 2;
            mX = (int) event.getX() - mBitmap[1].getWidth() / 2;
            mX = (int) event.getX() - mBitmap[2].getWidth() / 2;
            mX = (int) event.getX() - mBitmap[3].getWidth() / 2;

            return super.onTouchEvent(event);

        }




class ViewThread extends Thread {
    private Panel mPanel;
    private SurfaceHolder mHolder;
    private boolean mRun = false;

    public ViewThread(Panel panel) {
        mPanel = panel;
        mHolder = mPanel.getHolder();
    }

    public void setRunning(boolean run) {
        mRun = run;
    }

    @Override
    public void run() {
        Canvas canvas = null;
            while (mRun) {
                canvas = mHolder.lockCanvas();
                if (canvas != null) {
                    mPanel.doDraw(canvas);
                    mHolder.unlockCanvasAndPost(canvas);
                }
            }
    }

} 

如何将位图一张一张地左右移动我的位图

如果位图移动,则位图相邻

我的代码有什么问题?

如何解决这个我的代码?

请帮我

4

1 回答 1

0

要在表面上移动对象,您必须定期更改对象的位置。

要开始 2D 游戏开发,您可以参考以下链接。

http://www.droidnova.com/2d-tutorial-series-part-i,770.html

在此链接中还有可用的示例代码,因此请下载并在您的 PC 中运行它并检查代码。

于 2012-10-23T13:22:36.087 回答